MFC控件:listbox控件⽤法详解
获取选中项的:
CString strText;
int nIndex = m_listbox.GetCurSel();
m_listbox.GetText(nIndex, strText);
ListBox的操作⽐较简单
1添加数据
声明控件变量的类别为Control,变量类型为CListBox,变量名为m_ListBox_Content.
m_ListBox_Content.AddString(_T("123"));
m_ListBox_Content.AddString(_T("汉字"));
m_ListBox_Content.AddString(_T("English"));
m_ListBox_Content.AddString(_T("!@#$%^&*()"));
2获取数据
CString s;
m_ListBox_Content.GetText(1,s);
MessageBox(s,_T("取得第2⾏数据"),MB_OK);
s.ReleaseBuffer();
将会得到"汉字"这个字符串,如果没有得到"汉字"这个字符串,是因为ListBox的Sort属性设为True了.设为False之后就按照你编写的顺序写⼊(原来是这样 之前都没注意过)
3获取选择的数据
⾸先要将ListBox的Selection属性设置为Multiple;
int nSel;
nSel=m_ListBox_Content.GetCurSel();
CString s;
m_ListBox_Content.GetText(nSel,s);
MessageBox(s,_T("您选择的是"),MB_OK);
s.ReleaseBuffer();
4获取选择ListBox项的多个数据
⾸先要将ListBox的Selection的属性设置为Multiple
int nSel = m_ListBox_Content.GetSelCount();
CArray< int,int& > arrayListSel;
arrayListSel.SetSize(nSel);
m_ListBox_Content.GetSelItems(nSel,arrayListSel.GetData());
CString s = _T("");
for( int i=0; i< nSel; i++ )
{undefined
m_ListBox_Content.GetText( arrayListSel[i], s);
MessageBox(s,_T("您选择的是"),MB_OK);
}
5双击删除所选项
添加⼀个ListBox的双击事件
m_ListBox_Content.DeleteString(m_ListBox_Content.GetCurSel());
//例⼦:
CListBox *List;
List=(CListBox*)GetDlgItem(IDC_LIST1);
List -> AddString("AAA");
List -> SetCurSel(0);
1. 属性列表:
SelectionMode    组件中条⽬的选择类型,即多选(Multiple)、单选(Single)    Rows            列表框中显⽰总共多少⾏
Selected        检测条⽬是否被选中
SelectedItem    返回的类型是ListItem,获得列表框中被选择的条⽬
Count            列表框中条⽬的总数
SelectedIndex    列表框中被选择项的索引值
Items            泛指列表框中的所有项,每⼀项的类型都是ListItem
2. 取列表框中被选中的值
ListBox.SelectedValue
3. 动态的添加列表框中的项:
ListBox.Items.Add("所要添加的项");
4. 移出指定项:
//⾸先判断列表框中的项是否⼤于0
If(ListBox.Items.Count > 0 )
{undefined
//移出选择的项
ListBox.Items.Remove(ListBox.SelectedItem);
}
5. 清空所有项:
//⾸先判断列表框中的项是否⼤于0
If(ListBox.Items.Count > 0 )
{undefined
//清空所有项
ListBox.Items.Clear();
}
6. 列表框可以⼀次选择多项:
只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选
7. 两个列表框联动,即两级联动菜单
//判断第⼀个列表框中被选中的值
switch(ListBox1.SelectValue)
{undefined
/
/如果是"A",第⼆个列表框中就添加这些:
case "A"
ListBox2.Items.Clear();
ListBox2.Items.Add("A1");
ListBox2.Items.Add("A2");
ListBox2.Items.Add("A3");
//如果是"B",第⼆个列表框中就添加这些:
case "B"
ListBox2.Items.Clear();
ListBox2.Items.Add("B1");
ListBox2.Items.Add("B2");
ListBox2.Items.Add("B3");
}
8. 实现列表框中项的移位
即:向上移位、向下移位
具体的思路为:创建⼀个ListBox对象,并把要移位的项先暂放在这个对象中。
如果是向上移位,就是把当前选定项的的上⼀项的值赋给当前选定的项,然后
把刚才新加⼊的对象的值,再附给当前选定项的前⼀项。
具体代码为:
//定义⼀个变量,作移位⽤
index = -1;
//将当前条⽬的⽂本以及值都保存到⼀个临时变量⾥⾯
ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
//被选中的项的值等于上⼀条或下⼀条的值
ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
//被选中的项的值等于上⼀条或下⼀条的值
ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;      //把被选中项的前⼀条或下⼀条的值⽤临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
//把被选中项的前⼀条或下⼀条的值⽤临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
//把⿏标指针放到移动后的那项上
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移动指针到指定位置:
(1).移⾄⾸条
//将被选中项的索引设置为0就OK了
ListBox.SelectIndex=0;
(2).移⾄尾条
//将被选中项的索引设置为ListBox.Items.Count-1就OK了
ListBox.SelectIndex=ListBox.Items.Count-1;
(3).上⼀条
//⽤当前被选中的索引去减 1
ListBox.SelectIndex=ListBox.SelectIndex - 1;
(4).下⼀条
//⽤当前被选中的索引去加 1
ListBox.SelectIndex=ListBox.SelectIndex + 1;
this.ListBox1.Items.Insertat(3,new  ListItem("插⼊在第3⾏之后项",""));  this.ListBox1.Items.Insertat(index,ListItem)
ListBox1.Items.Insert(0,new  ListItem("text","value"));
1、让ListBox中某⼀个指定的索引值的项获得焦点(选中)
ListBox.SelectedIndex=索引值;
2、把全部选中项都清除
ListBox.ClearSelected();
3、使其中指定索引值的某项不被选中
ListBox.SetSelected(0,False);
4、把listBox1中的所有项填⼊listBox2中
foreach(string item in listBox1.Items)
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("已经是最后⼀⾏了!");
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("已经是第⼀⾏了!");
7、把listBox1中选中的项填⼊listBox2中
int i;
string select1;
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);
typeof的用法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);  }
}
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;
}
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;
}
}