韌館-LearnHouse

[C/C++]MFC ComboBox 使用方法

Step1 首先當然是要在工具箱拉Combo Box出來
在Data屬性中可以放入你想要放的值或資料,只要用分號『;』隔開,例如:10;20;30;40

Step2 再來設定變數
CComboBox *m_sTimeout;  //宣告Combo Box物件位址變數
CString strCBTimeout; //宣告所選的值或資料變數
int SetTimeout = 0;    //宣告轉換字串成整數值變數

Step3取得Combo Box物件資料
m_sTimeout = (CComboBox *)GetDlgItem(IDC_COMBOTIMEOUT);

if(m_sTimeout->GetCurSel() >=0)  //當有取到值的時候執行會回傳選取的編號
{
m_sTimeout->GetLBText(m_sTimeout->GetCurSel(), strCBTimeout); //剛選取的編號值放入字串
SetTimeout = atoi(strCBTimeout); //將選取的字串值轉成整數值
}
else
SetTimeout = 8000;

可參考下面解說http://www.codersource.net/mfc/mfc-tutorials/ccombobox-example.aspx

CComboBox Example

Minimize

Combo box controls are space savers. Wherever there is no need for a multi-select from a list of items, combo box is a good choice in such places. This article " CComboBox Example" explains how to use the MFC CComboBox class for manipulation of a list of strings.

CComboBox Example - Initializing a Combo Box:

It is assumed that the readers of the sample have already created a dialog box (either in a dialog based application or SDI/MDI application) and placed a combo box control from the controls toolbox on the Resource Editor.

After placing the combo box control on the dialog box, open the class wizard by pressing Ctrl + W keys or Menu --> View --> ClassWizard. In the Member Variables tab, Add a Variable for the CComboBox class. This CComboBox example assumes that the variable name is,

CComboBox  m_cbExample;

This m_cbExample will be used further in our CComboBox example MFC code.

CComboBox Example - Adding Items to a Combo Box:

The function AddString is used for adding items to a combo box. If there is a constant set of data, these values can also be added in the Resource Editor itself. The Combo Box control properties dialog has a tab for adding data. Otherwise the data can be added as follows.

m_cbExample.AddString("StringData1");
m_cbExample.AddString("StringData2");
m_cbExample.AddString("StringData3");

CComboBox Example - Retrieving Items from a Combo Box:

Usually a requirement for retrieving items from the combo box will arise from selecting the data. This article also assumes the same. Now the data selected in a combo box needs to be retrieved.

To do this, the first step is to find out the index of the selected item inside the combo box control. Then the item at the corresponding position needs to be pulled out as follows.

int nIndex = m_cbExample.GetCurSel();
CString strCBText;
m_cbExample.GetLBText( nIndex, strCBText);

In the above CComboBox example code, the value will be retrieved and stored in strCBText variable. There is another overloaded version for GetLBText. But the version which uses CString is the easiest one.

CComboBox Example - Finding Items inside a Combo Box:

This kind of Find operations on a Combo box will most probably be useful in programs that dynamically modify the values in a combo box. The function FindStringExact is used to find the exact string match inside a combo box.

int nIndex = m_cbExample.FindStringExact(0, "Value to be found");

The string position inside the combo box control is the return value. It returns CB_ERR if it was unsuccessful in finding the string.

CComboBox Example - Deleting Items from a Combo Box:

This operation can be done by using the CCombobox member function DeleteString. This function needs the index of the item inside the combo box.

m_cbExample.DeleteString(nIndex);

posted by admin in 程式&軟體 and have Comment (1)

One Response to “[C/C++]MFC ComboBox 使用方法”

  1. 色情电影表示:

    Everything psted wwas actually very reasonable.
    However, what about this? suppoee yoou added a little content?
    I mean, I don't wijsh tto tell you howw to runn yolur blog, but what if you added a
    headlinne too maybee gget folk's attention? I ean 韌館-LearnHouse »
    Blog Archive » [C/C++]MFC ComboBox 使用方法 is a little boring.

    You ought to look at Yahoo's front page and note how they create article headlines to
    get viewers interested. You might add a video or a pic
    or two to grab readers excited about everything've written. Just my opinion, it would
    make your posts a little bit more interesting.

Place your comment

Please fill your data and comment below.
名稱:
信箱:
網站:
您的評論: