MFC--List列表控件_单选

MFC--List列表控件_单选_02

  

添加数据

mylist.AddString(_T("海南"));//添加数据
//排序属性为False时往尾部添加
mylist.AddString(_T("黑龙江"));
mylist.AddString(_T("浙江"));
mylist.AddString(_T("北京"));


mylist.InsertString(2, TEXT("天津"));//插入数据
//参数1:索引
//不受[排序]属性的影响

 

获取项目数

int n = mylist.GetCount();//获取项目数
CString str;
str.Format(_T("%d"), n);
MessageBox(str);

 

删除数据 

mylist.DeleteString(1);//删除数据
////参数:索引号

mylist.ResetContent();//全部删除

 

选中数据

 

mylist.SetCurSel(2);//选中数据
//参数:索引号
//注意:选择属性必须是单选不能多选

mylist.SetSel(0,true);//选中数据
//参数1:索引号
//参数2:true[默认] 选中,false 不选中
//注意:选择属性必须是多选不能单选,选择n项就写n次
mylist.SetSel(3);

//mylist.SetCurSel(-1);//不选中数据

 

 

获取选中项索引

注意选择属性

MFC--List列表控件_数据_03

MFC--List列表控件_单选_04

     只能单选 

MFC--List列表控件_添加数据_05

    多选     用户可以用鼠标点击进行多选,不支持ctrl和shift键的多项 

MFC--List列表控件_数据_06

  支持ctrl和shift键的多项,不支持鼠标点击进行多选 

MFC--List列表控件_单选_07

 

int n = mylist.GetCurSel();//获取选中项的索引号
//如果没有选中项返回-1
//注意:选择属性必须是单选不能多选

int h = mylist.GetSelCount();//获取被选中的项目数量
//注意:选择属性必须是多选不能单选

int nn = -1;
if (h > 0) {
int* p = new int[h];

mylist.GetSelItems(h, p);//获取所有被选中的项目索引
//参数1:想要获取的项目数
//参数2:保存选中项目索引号的int类型的数组指针
//注意:选择属性必须是多选不能单选

for (int idx = 0; idx < h; idx++) {
nn = p[idx];
}
}

 

获取指定项文本长度 

int n=mylist.GetTextLen(2);//获取指定项文本长度
//参数:索引
CString str;
str.Format(_T("%d"), n);
MessageBox(str);

 

获取指定项文本

CString str;
mylist.GetText(2,str);
//参数1:索引号 CString和TCHAR
MessageBox(str);