VC++ combobox control usage summary

  • 2020-04-02 02:33:16
  • OfStack

In VC++ programming, combobox control is common form element control, and for the novice, master combobox all kinds of usage is particularly important, this article to summarize some practical combobox control usage, for your reference.

I. add/delete the contents of Combo Box:

1. Generally, it is added in the Data label of ComboBox control property. One line represents one line in the ComboBox drop-down list.

2. Add dynamically when the program is initialized, such as:


CString strTemp;
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->ResetContent();//Clear the original content
for(int i=1;i<=100;i++)
{
strTemp.Format("%d",i);
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->AddString(strTemp);
}

3. Drop down to add content items, such as: CString strTemp;


int iCount=((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetCount();//Gets the number of existing rows
if(iCount<1)//Prevent multiple additions
{
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->ResetContent();
for(int i=1;i<=100;i++)
{
strTemp.Format("%d",i);
((CComboBox*)GetDlgItem(IDC_COMBO_CF))->AddString(strTemp);
}
}

4. Delete the content. It's even easier.


DeleteString(UINT nIndex)

5. Insert the content item, which is also easy to do in one line: insert the row into the specified location


InsertString( int nIndex, LPCTSTR lpszItem )

6. Find, as the case may be, such as:


FindString( int nStartAfter, LPCTSTR lpszItem )//The position of the specified character can be found on all current lines, and nStartAfter indicates that the search begins on that line.
int SelectString( int nStartAfter, LPCTSTR lpszItem )//You can select a line that contains the specified string

Ii. Control the length of the drop-down box of ComboBox:

The first thing to know is two things:

One is that in the design interface, click the drop-down arrow in Combo Box, and the adjustment Box that appears is the drop-down adjustment Box in Combo Box.

Second, there is No integral height hook option in the attribute, which means that the maximum length is the design length. If the actual content is more than the design length, the scroll bar will appear, and if there is less, it will be displayed as the actual length.

Choose a row

1. The selected:


int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetCurSel();//The currently selected row

2. The Settings:


((CComboBox*)GetDlgItem(IDC_COMBO_CF))->SetCurSel(n)//Set the NTH line to be the displayed content

Iv. Obtain ComboBox contents:

1. Get the current content of ComboBox:


((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetWindowText(strTemp);

2. Get other lines:


((CComboBox*)GetDlgItem(IDC_COMBO_CF))->GetLBText(n,strTemp);

V. get focus:

You can use the GetFocus() function. Such as:


if(GetFocus()==GetDlgItem(IDC_EDIT_VALUE2))// Determines if the focus is in the edit box IDC_EDIT_VALUE2 Inside. 

But combobox has a different focus because it is made up of edit and listbox. So get focus by using GetParent() :


if ((GetFocus()->GetParent())==GetDlgItem(IDC_COMBO_CF))

I hope this article described combobox control usage can help you learn VC++ to a certain extent.


Related articles: