`

C# ListBox的使用和操作

 
阅读更多
实现选项的选择(单选,多选,全选(移除))

初始化ListBox

protectedvoidInitListBox()

{

ArrayListarrlist =newArrayList();

arrlist.Add("星期一");

arrlist.Add("星期二");

arrlist.Add("星期三");

arrlist.Add("星期四");

arrlist.Add("星期五");

arrlist.Add("星期六");

arrlist.Add("星期日");

LBoxSourseWeek.DataSource = arrlist;

LBoxSourseWeek.DataBind();

}

选择所有

protectedvoidbtnAddAll_Click(objectsender,EventArgse)

{

intcount = LBoxSourseWeek.Items.Count;

intindex = 0;

//循环移动,( 每次都移动最新的第一个)

for(inti = 0; i < count; i++)

{

ListItemitem = LBoxSourseWeek.Items[index];

LBoxSourseWeek.Items.Remove(item);

LBoxDestWeek.Items.Add(item);

}

index++;

}

//移除所有

protectedvoidbtnDelAll_Click(objectsender,EventArgse)

{

//全部从目标listbox一出(法一)

intcount = LBoxDestWeek.Items.Count;

intindex = 0;

//循环移动,( 每次都移动最新的第一个)

for(inti = 0; i < count; i++)

{

ListItemitem = LBoxDestWeek.Items[index];

LBoxDestWeek.Items.Remove(item);

LBoxSourseWeek.Items.Add(item);

}

index++;

//重新初始化控件(法二)

//InitListBox();

//LBoxDestWeek.Items.Clear();

}

选择一个或者多个

protectedvoidbtnAddSel_Click(objectsender,EventArgse)

{

//支持选中多个的移动

intcount = LBoxSourseWeek.Items.Count;

intindex = 0;

//循环,先判断选中,再移动,( 每次都移动最新的第一个)

for(inti = 0; i < count; i++)

{

ListItemitem = LBoxSourseWeek.Items[index];

if(LBoxSourseWeek.Items[index].Selected ==true)

{

LBoxSourseWeek.Items.Remove(item);

LBoxDestWeek.Items.Add(item);

//将当前索引值减1

index--;

}

//获取下一个索引值

index++;

}

index++;

}

移除一个或者多个

protectedvoidbtnDelSel_Click(objectsender,EventArgse)

{

intcount = LBoxDestWeek.Items.Count;

intindex = 0;

//循环,先判断选中,再移动,( 每次都移动最新的第一个)

for(inti = 0; i < count; i++)

{

ListItemitem = LBoxDestWeek.Items[index];

if(LBoxDestWeek.Items[index].Selected ==true)

{

LBoxDestWeek.Items.Remove(item);

LBoxSourseWeek.Items.Add(item);

//将当前索引值减1

index--;

}

//获取下一个索引值

index++;

}

index++;

}

将选择的选项向上移动一次

protectedvoidbtnUpOne_Click(objectsender,EventArgse)

{

if(LBoxSourseWeek.SelectedIndex > 0 && LBoxSourseWeek.SelectedIndex <= LBoxSourseWeek.Items.Count - 1)

{

//记录当前选项的值

stringname = LBoxSourseWeek.SelectedItem.Text;

stringvalue = LBoxSourseWeek.SelectedItem.Text;

//获取当前选项的索引号

intindex = LBoxSourseWeek.SelectedIndex;

//交换当前选项和其前一项的索引号

LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[index - 1].Text;

LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[index - 1].Value;

LBoxSourseWeek.Items[index - 1].Text = name;

LBoxSourseWeek.Items[index - 1].Value = value;

//设定上一行为选中行

LBoxSourseWeek.SelectedIndex--;

}

else

{

//记录当前选项的值

stringname = LBoxSourseWeek.SelectedItem.Text;

stringvalue = LBoxSourseWeek.SelectedItem.Text;

//获取当前选项的索引号

intindex = LBoxSourseWeek.SelectedIndex;

//交换当前选项和其前一项的索引号

LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count - 1].Text;

LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count - 1].Value;

LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count - 1].Text = name;

LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count - 1].Value = value;

//设定上一行为选中行

LBoxSourseWeek.SelectedIndex = LBoxSourseWeek.Items.Count - 1;

}

}

将选择的选项向下移动一次

protectedvoidbtnDownOne_Click(objectsender,EventArgse)

{

if(LBoxSourseWeek.SelectedIndex >= 0 && LBoxSourseWeek.SelectedIndex< LBoxSourseWeek.Items.Count - 1)

{

//记录当前选项的值

stringname = LBoxSourseWeek.SelectedItem.Text;

stringvalue = LBoxSourseWeek.SelectedItem.Text;

//获取当前选项的索引号

intindex = LBoxSourseWeek.SelectedIndex;

//交换当前选项和其前一项的索引号

LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[index + 1].Text;

LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[index + 1].Value;

LBoxSourseWeek.Items[index + 1].Text = name;

LBoxSourseWeek.Items[index + 1].Value = value;

//设定上一行为选中行

LBoxSourseWeek.SelectedIndex++;

}

else

{

//记录当前选项的值

stringname = LBoxSourseWeek.SelectedItem.Text;

stringvalue = LBoxSourseWeek.SelectedItem.Text;

//获取当前选项的索引号

intindex = LBoxSourseWeek.SelectedIndex;

//交换当前选项和其前一项的索引号

LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[0].Text;

LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[0].Value;

LBoxSourseWeek.Items[0].Text = name;

LBoxSourseWeek.Items[0].Value = value;

//设定上一行为选中行

LBoxSourseWeek.SelectedIndex = 0;

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics