锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
Note that for these to work, the control must have the "LVS_REPORT" style. For list controls in dialogs, including dialogs for CFormView, this style can be set in the dialog editor. If you are using CListView or you are creating list controls using Create, the style can be set in a PreCreateWindow override, as in:
Getting a Count of Columns
Deleting All Columns
Using GetColumnsCount() from above, it is easy to delete all columns.
Microsoft has made things very confusing because the Platform SDK name for a listbox is "ListBox Control" but MFC programmers usually use "list control" to refer to an instance of the CListCtrl class, which is confusing. Also, the MFC class name for a "List View Control" is CListCtrl and CListView is the view class for it. The MFC class name for a "ListBox Control" is CListBox so we would expect the view class for it to also be CListView except there is not a view class. The following summarizes this.
Platform SDK name:List Boxes
Window class name: ListBox (See under "Predefined Controls" in User Controls and the documentation for CreateWindow and CreateWindowEx)
MFC class name:CListBox
MFC view class name: none
Platform SDK name:ListView Control
Window class name: SysListView32 (use WC_LISTVIEW in source code instead, which is defined as "SysListView32")
MFC class name:CListCtrl
MFC view class name:CListView
聽聽 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.
聽聽 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");
聽聽 Usually 聽聽 To do this, the first step is to find out the index of the selected item inside the combo box control. 聽聽 The string position inside the combo box control is the return value. It returns CB_ERR if it was unsuccessful in finding the string.
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.
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");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);