using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ApplyListBox
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ListBox ListBox1;
		protected System.Web.UI.WebControls.ListBox ListBox2;
		protected System.Web.UI.WebControls.Button Upbtn;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Button Movebtn;
		protected System.Web.UI.WebControls.Button Downbtn;
	
		private void Page_Load(object sender, System.EventArgs e)
		{

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Upbtn.Click += new System.EventHandler(this.Upbtn_Click);
			this.Movebtn.Click += new System.EventHandler(this.Movebtn_Click);
			this.Downbtn.Click += new System.EventHandler(this.Downbtn_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Movebtn_Click(object sender, System.EventArgs e)
		{		
			int Count = ListBox1.Items.Count;
			int Index = 0;
			for(int i=0;i<Count-1;i++)
			{
				ListItem Item = ListBox1.Items[Index];
				if(ListBox1.Items[Index].Selected == true)
				{
					ListBox1.Items.Remove(Item);
					ListBox2.Items.Add(Item);
					Index--;
				}
				Index++;
			}
		}

		private void Upbtn_Click(object sender, System.EventArgs e)
		{
			//若不是第一行则上移
			if( ListBox1.SelectedIndex > 0 )
			{
				string name = ListBox1.SelectedItem.Text;
				string ID = ListBox1.SelectedItem.Value;
				int index = ListBox1.SelectedIndex;
				ListBox1.SelectedItem.Text = ListBox1.Items[index-1].Text;
				ListBox1.SelectedItem.Value = ListBox1.Items[index-1].Value;
				ListBox1.Items[index-1].Text = name;
				ListBox1.Items[index-1].Value = ID;
				ListBox1.SelectedIndex --;
			}
		}

		private void Downbtn_Click(object sender, System.EventArgs e)
		{
			//若不是最后一行则下移
			if( ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex < ListBox1.Items.Count-1 )
			{
				string name = ListBox1.SelectedItem.Text;
				string ID = ListBox1.SelectedItem.Value;
				int index = ListBox1.SelectedIndex;
				ListBox1.SelectedItem.Text = ListBox1.Items[index+1].Text;
				ListBox1.SelectedItem.Value = ListBox1.Items[index+1].Value;
				ListBox1.Items[index+1].Text = name;
				ListBox1.Items[index+1].Value = ID;
				ListBox1.SelectedIndex ++;
			}
		}
	}
}
