1、让ListBox中某一个指定的索引值的项获得焦点(选中)
ListBox.SelectedIndex=索引值;
ListBox.SelectedIndex=索引值;
2、把全部选中项都清除
ListBox.ClearSelected();
ListBox.ClearSelected();
3、使其中指定索引值的某项不被选中
ListBox.SetSelected(0,False);
ListBox.SetSelected(0,False);
4、把listBox1中的所有项填入listBox2中
foreach(string item in listBox1.Items)
this.listBox2.Items.Add (item.ToString());
this.listBox2.Items.Add (item.ToString());
5、listBox2中的选中项下移
Object obj = new object ();
int index;
index = listBox2.SelectedIndex;
obj = listBox2.SelectedItem;
if(index < listBox2.Items.Count -1 )
{
listBox2.Items.RemoveAt (listBox2.SelectedIndex );
listBox2.Items.Insert (index+1 , obj);
listBox2.SelectedItem = obj;
}
else
MessageBox.Show("已经是最后一行了!");
int index;
index = listBox2.SelectedIndex;
obj = listBox2.SelectedItem;
if(index < listBox2.Items.Count -1 )
{
listBox2.Items.RemoveAt (listBox2.SelectedIndex );
listBox2.Items.Insert (index+1 , obj);
listBox2.SelectedItem = obj;
}
else
MessageBox.Show("已经是最后一行了!");
6、listBox2中的选中项上移
Object obj = new object ();
int index;
index = listBox2.SelectedIndex;
obj = listBox2.SelectedItem;
if(index > 0 )
{
listBox2.Items.RemoveAt (listBox2.SelectedIndex );
listBox2.Items.Insert (index-1 , obj);
listBox2.SelectedItem = obj;
}
else
MessageBox.Show("已经是第一行了!");
int index;
index = listBox2.SelectedIndex;
obj = listBox2.SelectedItem;
if(index > 0 )
{
listBox2.Items.RemoveAt (listBox2.SelectedIndex );
listBox2.Items.Insert (index-1 , obj);
listBox2.SelectedItem = obj;
}
else
MessageBox.Show("已经是第一行了!");
7、把listBox1中选中的项填入listBox2中
int i;
string select1;
for(i=0;i<listBox1.SelectedItems.Count ;i++)
{
select1 = listBox1.SelectedItems[i].ToString();
for(i=0;i<listBox1.SelectedItems.Count ;i++)
{
select1 = listBox1.SelectedItems[i].ToString();
foreach(string select2 in listBox2.Items)
if(select1 == select2.ToString())
flag++;
if(flag!=0)
{
MessageBox.Show("已经包含"+select1+"了!");
flag=0;
}
else
this.listBox2.Items.Add(select1);
flag1=true;
}
if(i>0)
{
int sele=this.listBox1.SelectedIndex;
this.listBox1.ClearSelected();
this.listBox1.SetSelected(sele+1,true);
}
if(select1 == select2.ToString())
flag++;
if(flag!=0)
{
MessageBox.Show("已经包含"+select1+"了!");
flag=0;
}
else
this.listBox2.Items.Add(select1);
flag1=true;
}
if(i>0)
{
int sele=this.listBox1.SelectedIndex;
this.listBox1.ClearSelected();
this.listBox1.SetSelected(sele+1,true);
}
8、拖动某项上下移动
(注:MouseDown 会和 click 产生冲突)
private void listBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
indexofsource = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
if (indexofsource != ListBox.NoMatches)
{
((ListBox)sender).DoDragDrop(((ListBox)sender).Items[indexofsource].ToString(), DragDropEffects.All);
}
}
{
indexofsource = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
if (indexofsource != ListBox.NoMatches)
{
((ListBox)sender).DoDragDrop(((ListBox)sender).Items[indexofsource].ToString(), DragDropEffects.All);
}
}
private void listBox2_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{ //拖动源和放置的目的地一定是一个ListBox
if (e.Data.GetDataPresent(typeof(System.String)) && ((ListBox)sender).Equals(listBox2))
{
e.Effect = DragDropEffects.Move;
}
else
e.Effect = DragDropEffects.None;
{ //拖动源和放置的目的地一定是一个ListBox
if (e.Data.GetDataPresent(typeof(System.String)) && ((ListBox)sender).Equals(listBox2))
{
e.Effect = DragDropEffects.Move;
}
else
e.Effect = DragDropEffects.None;
}
private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
ListBox listbox=(ListBox)sender;
indexoftarget=listbox.IndexFromPoint(listbox.PointToClient(new Point(e.X, e.Y)));
if(indexoftarget!=ListBox.NoMatches)
{
string temp=listbox.Items[indexoftarget].ToString();
listbox.Items[indexoftarget]=listbox.Items[indexofsource];
listbox.Items[indexofsource]=temp;
listbox.SelectedIndex=indexoftarget;
}
}
{
ListBox listbox=(ListBox)sender;
indexoftarget=listbox.IndexFromPoint(listbox.PointToClient(new Point(e.X, e.Y)));
if(indexoftarget!=ListBox.NoMatches)
{
string temp=listbox.Items[indexoftarget].ToString();
listbox.Items[indexoftarget]=listbox.Items[indexofsource];
listbox.Items[indexofsource]=temp;
listbox.SelectedIndex=indexoftarget;
}
}
9、
选择模式:(SelectionMode)(在属性窗口设置)
None:不可选 one:单选
MultiSimple:能多选,点一下就选中一个,再点一下就不选中
MultiExtended:多选的常用模式,可拖动多选,也可用shift和ctrl多选