MFC-combox usage

Source: Internet
Author: User
MFC-combox usage

The combo box control is simple and can save space. From the user's perspective, this control is composed of a text input control and a drop-down menu. You can select an option from a pre-defined list or enter text in the text box. The following example briefly describes how to use
MFC ccombobox class to operate the string list.
1. Define control variables

Assume that a dialog has been created, and the combo box control is dragged from the control toolbox to the top. Open Class Wizard and add control variables, such as ccombobox m_cbexamble;

This variable will be continuously used in subsequent code.

2. add items to the control

1) Add a row in the data label of the combo box control property, indicating a row in the combo box drop-down list. Press Ctrl + enter to wrap the text.

2) use the addstring () function to add items to the combo box control, for example:

M_cbexample.addstring ("stringdata1 ");

M_cbexample.addstring ("stringdata2 ");

M_cbexample.addstring ("stringdata3 ");

3) You can also call the insertstring () function to insert the item into the specified position nindex, for example:

M_cbexample.insertstring (nindex, "stringdata ");

3. Obtain the selected item from the control.

Assume that an item has been selected in the Control List. To get the content of the selected item, first obtain the position of the item and then obtain the content of the corresponding position. Two functions are used here, for example:

Int nindex = m_cbexample.getcursel ();

Cstring strcbtext;

M_cbexample.getlbtext (nindex, strcbtext );

In this way, the obtained content is saved in strcbtext.

To select the current content, call the getwindowtext (strcbtext) function ).

4. Search for a given item in the control

This operation is generally used to dynamically modify the value of the item in the control in the program. You can use the findstringexact () function for exact matching, such:

Int nindex = m_cbexample.findstringexact (nstartafter, "value to be found ");

Nstartafter specifies the row from which to start searching. If the search is successful, the position of the item is returned; otherwise, the cb_err is returned.

You can also select an item that contains the specified string, for example:

Int nindex = m_cbexample.selectstring (nstartafter, "value to be selected ");

5. Delete the item in the control.

You can use the deletestring () function to specify the location of the deleted item, for example:

M_cbexample.deletestring (nindex );

You can also use the resetcontent () function to clear all current items, such:

M_cbexample.resetcontent ();

6. display an item in the control

Int nindex = m_cbexample.getcursel (); // the currently selected item

M_cbexample.setcursel (nindex); // you can specify nindex as the displayed content.

7. Get or set the selected character position in the input box

DWORD geteditsel ()/bool seteditsel (INT nstartchar, int nendchar );

Bool limittext (INT nmaxchars); specifies the maximum number of characters that can be entered in the input box.

8. list box common message ing macros

On_cbn_dblclk double-click

The on_cbn_dropdown list box is displayed.

On_cbn_killfocus/on_cbn_setfocus is generated when the input box loses/gets the input focus

The row selected in the on_cbn_selchange list box is changed.

The content in the on_cbn_editupdate input box is updated.

Supplement:

1. How to add/delete combo box content

1. Add a row in the data label of the combo box control property, indicating a row in the combo box drop-down list. Press Ctrl + enter to wrap the text.

2. Dynamic addition during program Initialization

For example: // control content Initialization

Cstring strtemp;

(Ccombobox *) getdlgitem (idc_combo_cf)-> resetcontent (); // eliminate all existing content

For (INT I = 1; I <= 100; I ++)

{

Strtemp. Format ("% d", I );

(Ccombobox *) getdlgitem (idc_combo_cf)-> addstring (strtemp );

}

3. Add

For example, cstring strtemp;

Int icount = (ccombobox *) getdlgitem (idc_combo_cf)-> getcount (); // obtain the number of existing rows

If (icount <1) // prevents repeated addition

{

(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

Deletestring (uint nindex) // delete a specified row,

5. Insert

Insertstring (INT nindex, lpctstr lpszitem) // insert the row to the specified position

6. Search

Findstring (INT nstartafter, lpctstr lpszitem) // you can find the specified character transfer position in all current rows. nstartafter indicates to start searching from that row.

Int selectstring (INT nstartafter, lpctstr lpszitem) // You can select a row that contains the specified string.

Ii. How to control the combo box drop-down Length

1. First, you need to know two points: 1. On the design page, click the drop-down arrow of the combo box. The box that appears is the drop-down box of the combo box.

2. There is a no integral height hook option in the attribute, indicating that the maximum length is the design length. If the actual content is longer than the design length, a scroll bar appears and the actual length is displayed if the content is smaller.

3. Select a line

1. Select:

Int IPOs = (ccombobox *) getdlgitem (idc_combo_cf)-> getcursel (); // the currently selected row.

2. Set

(Ccombobox *) getdlgitem (idc_combo_cf)-> setcursel (n) // you can specify the content of row N as the displayed content.

4. Get the combo box content

1. Retrieve the current content

(Ccombobox *) getdlgitem (idc_combo_cf)-> getwindowtext (strtemp );

2. Retrieve other row content

(Ccombobox *) getdlgitem (idc_combo_cf)-> getlbtext (n, strtemp );

Iv. Focus

You can use the getfocus () function to determine whether the control obtains the focus.

For example, if (getfocus () = getdlgitem (idc_edit_value2) // checks whether the focus is in the idc_edit_value2 edit box.

But the focus of ComboBox is different, because it is composed of edit and ListBox.

Therefore, getparent (): If (getfocus ()-> getparent () = getdlgitem (idc_combo_cf) is used to obtain the focus ))

Control the combo box drop-down Length

1. First, you need to know two points: 1. On the design page, click the drop-down arrow of the combo box. The box that appears is the drop-down box of the combo box.

2. There is a no integral height hook option in the attribute, indicating that the maximum length is the design length. If the actual content is longer than the design length, a scroll bar appears and the actual length is smaller.

VC ++ combo box/Combo box ex Control

A combination window consists of an input box and a list box. You can use the member functions to create a combination window:

Bool clistbox: Create (lpctstr lpsztext, DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint nid = 0 xFFFF );

Dwstyle indicates the style of the window. In addition to the commonly used style ws_child and ws_visible of the sub-window, you can specify a special style for the list control.

Cbs_dropdown drop-down box

Cbs_dropdownlist drop-down box, but cannot be input in the input box

The cbs_simple input box and the list box are both displayed.

All lbs_sort rows are sorted alphabetically.

Because a list box contains a list box, the function of the list box can be used, such:

Int addstring (lpctstr lpszitem) to Add rows,

Int deletestring (uint nindex) deletes a specified row,

Int insertstring (INT nindex, lpctstr lpszitem) inserts the row into the specified position.

Void resetcontent () can delete all rows in the list box.

Call int getcount () to obtain the number of rows in the current list box.

To obtain/set the position of the currently selected row, call int getcursel ()/INT setcursel (INT iindex ). Call int getlbtext (INT nindex, lptstr lpsztext) to obtain the string of the specified row in the list box.

In addition, you can call int findstring (INT nstartafter, lpctstr lpszitem) to find the specified character transfer position in all current rows. nstartafter indicates to start searching from that row.

Int selectstring (INT nstartafter, lpctstr lpszitem) can be used to select rows containing the specified string.

In addition, the functions of the input box can be used, such:

DWORD geteditsel ()/bool seteditsel (INT nstartchar, int nendchar) to obtain or set the position of the selected character in the input box.

Bool limittext (INT nmaxchars) sets the maximum number of characters that can be entered in the input box.

The clipboard function of the input box is copy, clear, cut, and paste.

At last, we will introduce several common message ing Macros in the list box:

On_cbn_dblclk double-click

The on_cbn_dropdown list box is displayed.

On_cbn_killfocus/on_cbn_setfocus is generated when the input box loses/gets the input focus

The row selected in the on_cbn_selchange list box is changed.

The content in the on_cbn_editupdate input box is updated.

The preceding message ing methods are used to define the function of the prototype afx_msg void memberfxn (); and the message ing in the form of on_notification (ID, memberfxn. If a combo box is used in the dialog box, class wizard automatically lists related messages and generates message ing code.

The combo box is enhanced in MFC 4.2. You can use imagelist In the combo box. A new class ccomboboxex (derived from ccombobox) is provided to implement this function. Added some new member functions in the ccomboboxex class to implement new functions: first, you need to call cimagelist * setimagelist (cimagelist * pimagelist) to set imagelist, and then call

Int insertitem (const comboboxexitem * pcbitem); To Add rows. The comboboxexitem is defined as follows:

Typedef struct {uint mask; int iItem; lptstr psztext; int cchtextmax; int iimage; int character; int ioverlay; int iindent; lparam;} comboboxexitem, * pcomboboxexitem;

You need to set mask = cbeif_image cbeif_text, set iItem to insert location, set psztext to display string, and set iimage to display icon index. The following code demonstrates how to insert:

M_list.create (idb_img, 16, 4, RGB (0, 0 ));

M_cbewnd.setimagelist (& m_list );

Comboboxexitem insitem; insitem. mask = cbeif_image cbeif_text; insitem. iItem = 0; insitem. iimage = 0; insitem. psztext = "Line 1"; m_cbewnd.insertitem (& insitem); insitem. iItem = 1; insitem. iimage = 1; insitem. psztext = "Line 2"; m_cbewnd.insertitem (& insitem );

Call int deleteitem (INT iindex); to delete a row and specify its position.

GET/set row data by calling bool getitem (comboboxexitem * pcbitem)/bool setitem (const comboboxexitem * pcbitem );

**************************************** **************************************** **********************

Tonyswe (Tong) replied to 13:31:10 score 0 this friend may be a newbie.
Is it added? No option is displayed after running.
After adding it, select "simple" under "styles->" types "in the combo box attribute, and enlarge the" combo box "box,
That is, the size of the drop-down range you want. Return to styles-> types and select drop down.

MFC combox resetcontent ()
MFC, combox, resetcontent
In the editexchange event of the combox control, after the resetcontent () function is added to clear the list, data cannot be entered. How can this problem be solved?
Void ctestdlg: oneditchangecombo1 ()
{
(Ccombobox *) getdlgitem (idc_combo1)-> resetcontent ();
// Other .....

Enable the default value of combox:

(Ccombobox *) getdlgitem (idc_combo1)-> addstring ("male ");

(Ccombobox *) getdlgitem (idc_combo1)-> addstring (" ");

(Ccombobox *) getdlgitem (idc_combo1)-> setcurlsel (1 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.