24课时VC之思考>列表框与组合框

                               列表框和组合框


             

一、添加

listBox.AddString("Gwen"); InsertString 和 Addstring函数都返回新加入的列表项的位置。若在整加列表项时候 发生错误,InsertString 和 Addstring函数将返回LB_ERR。如果列表框满了,将返回 LB_ERRSPACE.

二、删除

listBox.DeleteString(8); 这一行代码删除了列表框中第9个位置的列表框。记住,所有的列表框的位置索引从零 开始。从DeleteString成员函数中返回的值是列表框中所剩列表项的数字,如果发生 错误,则为LB_ERR。返回值可如下使用:

int nItems=listBox.GetCount();

while(nItems>3&&nItems!=LB_ERR)

nItems=listBox.DeleteString(nItems-1);

此代码删除前3个列表框之后的所有列表项。若要完全删除列表项,需要使用ResetCount函数: listBox.ResetCount(); ResetCount返回位Void(空)。

LBN_,是List BOX Notification的简称。 ClistBox::GetCursel成员函数决定列表框当前选择的列表项。

1.给组合框增加列表项 :

comboBox.AddString("Riley"); 或 comboBox.InsertString(0,"Mitch");

如果有错误发生,将被返回CB_ERR而不是LB_ERR 同理,CB_ERRSPACE nItems=comboBox.GetCount();

2.从组合框中获取收入:

m_combo.GetLBText(1,szChoice);

3.在组合框中查找列表项:

FindString和FindStringExact成员函数查找一个特定的列表项。

int index=m_combo.FinString(-1,szSearch); 搜索索引指定第一个被找到的列表项索引之前的列表项,因此在开始搜寻索引时必须对索引 减一作为搜索索引。

FindStringExact函数在组合框中搜索与搜索字符串精确匹配的列表项,它与FindString函数 使用相同的参数: int index=m_combo.FindStringExact(-1,szSearch);

CComboListDlg::OnCloseupCombo函数收集组合框编辑控件部分的内容和组合框列表框中所选择 的列表项。


1.

void CListBoxDlg::OnDblclkList() {  // TODO: Add your control notification handler code here  

int nSelection=m_listBox.GetCurSel();

 if(nSelection!=LB_ERR)  {     

CString szSelection;  

m_listBox.GetText(nSelection,szSelection);  

AfxMessageBox(szSelection);

 }

}


2.

void CListBoxDlg::OnCloseupCombo() {  // TODO: Add your control notification handler code here  

CString szChoice;  

CString szResult;  

int nChoice;

 m_comboList.GetWindowText(szChoice);

 nChoice=m_comboList.GetCurSel();  

if(nChoice!=CB_ERR)  {    

m_comboList.GetLBText(nChoice,szChoice);

 szResult="Closing after selecting"+szChoice;

 }      

else if(szChoice.IsEmpty()==TRUE)  {     

szResult="No choice selected";  }

 else if(

m_comboList.FindStringExact(-1,szChoice)!=CB_ERR)

{    

szResult="Closing after selecting"+szChoice;

 }

 else  {    

m_comboList.AddString(szChoice);

 szResult="Adding "+szChoice+ " to list";

 }  

CWnd *pWnd=GetDlgItem(ID_RESULT);  

ASSERT(pWnd);  

if(pWnd){    

pWnd->SetWindowText(szResult);

 }

}


3.

void CListBoxDlg::OnEditupdateCombo() {  // TODO: Add your control notification handler code here  

CString szChoice;  

CString szResult;  

m_comboList.GetWindowText(szChoice);    

szResult="Choice changed to "+szChoice;  

CWnd *pWnd=GetDlgItem(ID_RESULT);  

ASSERT(pWnd);

 if(pWnd)   

pWnd->SetWindowText(szResult);

}

 
 
  
 
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 



  
  
  
  
  
  
  
  
  
  
  
 
 
 


原文地址:https://www.cnblogs.com/fengbo/p/2629003.html