CCombbox

CComboBox:组合框

 

先来看看MSDN:

The CComboBox class provides the functionality of a Windows combo box.

A combo box consists of a list box combined with either a static control or edit control. The list-box portion of the control may be displayed at all times or may only drop down when the user selects the drop-down arrow next to the control.

The currently selected item (if any) in the list box is displayed in the static or edit control. In addition, if the combo box has the drop-down list style, the user can type the initial character of one of the items in the list, and the list box, if visible, will highlight the next item with that initial character.

The following table compares the three combo-box styles.

Style

When is list box visible?

Static or edit control?

Simple

Always

Edit

Drop-down

When dropped down

Edit

Drop-down list

When dropped down

Static

 

 

 CComboBox拥有三种类型,Simple、Drop-down、Drop-down list,这里我们要创建一个Drop-down list

样式的组合框,并且添加消息:

 

 

 

 

添加变量

CComboBox    m_combobox;

在OnCreate消息处理:

m_combobox.Create(

           WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWNLIST,

           CRect(300,10,500,100), this, 2);

      CString str1;

      for (int i=0;i < 10;i++)

      {

           str1.Format(_T("item string %d"), i);

           m_combobox.AddString( str1 );

      }

 

       

      m_combobox.SelectString(0,"item string 1");

 

现在添加消息响应函数:

原型:afx_msg void OnComboBoxClose();

映射:ON_CBN_CLOSEUP(2,&CMyView::OnComboBoxClose)

 

实现:afx_msg void CMyView::OnComboBoxClose()//当下拉列表框关闭时响应此消息

{

      //MessageBox("列表框已经关闭!");

      int nIndex = m_combobox.GetCurSel();

      int nCount =m_combobox.GetCount();

      CString str;

      m_combobox.GetLBText(nIndex,str);

      if ((nIndex != LB_ERR) && (nCount > 1))

      {

           MessageBox(str);

      }

}

这个消息是当组合框关闭时响应的函数。

 

你可能感兴趣的:(mfc)