Use of the ComboBox control in VC

Source: Internet
Author: User

This attribute is available in ccombobox after the drop list is set.

Use: Initialize the variable:
M_ctrlcombo.insertstring (3, "manage 112 ");
Use function call:
(Ccombobox *) getdlgitem (idc_combo1)-> insertstring (3, "manage 112 ");

 

The ccombobox control is also called a combo control. It has three forms to choose from: 1. simple box (simple) 2. drop-down box (drop-down) 3. drop-down box (drop-down list ).

Common setting attributes of the ccombobox control:
Type attribute: There are three options in total. there are three forms. We usually use the last two forms. The difference is that the dropdown editing area is an editable control, while the droplist is a static control.
Data properties: when the program is initialized, the drop-down list will display the content in its properties, separated by semicolons. The properties only support the form in the second two,
Sort attribute: Automatically sorts the data added to the drop-down list box. If you do not want it to change your display sequence, set it to false.
The widget is a window. its base class is cwnd. therefore, ccombobox can also use some functions of cwnd. its own functions include 30 types. I wrote a demo program to demonstrate some common functions. You can see its functions by referring to its code.
Note: m_cbox is the associated variable of the ccombobox control, and m_end is the associated variable of the Edit Control.
1. getcount () function: used to obtain the number of options in the list box. int getcount () const; (this is the prototype of the function, And I Will column it after each entry) the following example shows the code in the buttons in the program.
Int I;
Cstring STR;
I = m_cbox.getcount ();
Str. Format ("% d", I );
M_end = "Total" + STR + "data ";
Updatedata (false );
2. getcursel () function: used to obtain the index value of the data in the drop-down list box selected by the user. the returned value starts with 0. If no option is selected,-1 int getcursel () const is returned.
Int I;
Cstring STR;
I = m_cbox.getcursel ();
Str. Format ("% d", I + 1 );
If (I =-1) m_end = "You didn't select anything ";
Else m_end = "you selected the" + STR + "item ";
Updatedata (false );
3. setcursel function: select an option in the list box. The index starts from 0 and-1 is not selected. You can use this function to clear the selected content in the list box;
If (m_ I <3) m_cbox.setcursel (m_ I ++); // m_ I is an int variable to count
Else {
M_cbox.setcursel (-1 );
M_ I = 0;
}
4. seteditsel function: Set the length of the selected blue field in the editing area. bool seteditsel (intnstartchar,
Int nendchar); nstartchar is the starting position. If it is set to-1, no field is selected, and nendchar is the ending position. another corresponding function geteditsel is used to obtain its location.
M_cbox.seteditsel (); // enter a number in the edit box and press this button to see the effect.
// The role is to select 3rd to 5th characters as the hotspot
5. clear () cut () paste () Copy () is a function that is used to edit the selected text in the editing box, many beginners do not understand these four functions. Using these functions is useless. Otherwise, these functions will not be processed in the editing box, what he really deals with is the content you selected with the mouse. many books on the clear () function have incorrect interpretations, and many books have clear the selected text, I think there are differences in this interpretation. The true explanation is to release the text you just selected, that is, not to delete the selected text, the function for deleting selected text is cut (), copy () is to copy the selected text, and paste () is to paste to the cursor. the demo is not written in the program. If you are interested, try it on your own.
6. limittext () function. the function of bool limittext (INT nmaxchars) is to limit the length of the text entered in the editing box. When nmaxchars is 0, it is not impossible to enter, but the maximum length is 65535 bytes.
7. getlbtext () function. its prototype is void getlbtext (INT nindex, cstring & rstring ). nindex is the index value of the data in the list box, And rstring is the string. It is used to put the data whose index number is nindex into the rstring variable. the corresponding getlbtextlen (intnindex) function is used to obtain the data length in the index number nindex.
M_cbox.getlbtext (2, m_end); // getlbtext button
Updatedata (0 );
Cstring STR; // getlbtextlen button
Int I = m_cbox.getlbtextlen (2 );
Str. Format ("% d", I );
M_end = STR;
Updatedata (false );
8. addstring () deletestring () insertstring () resetcontent () functions are put together. They are all processing functions for the drop-down list box. addstring (lpctstrlpszstring) adds a piece of data to the tail of the list box, the parameter is a string. deletestring (uintnindex) is used to delete data whose specified index is nindex. insertstring (intnindex, lpctstrlpszstring) inserts ipszstring content at index nindex. resetcontent () is to clear all content in the list box.
9. setdroppedwidth (uintnwidth) function. this function is used to set the minimum width of the drop-down list box. When the data in the drop-down list box is too long to be fully displayed, we can use this function to increase the width of the list box. similarly, getdroppedwidth () is the width of the returned list box.

The following is a summary of the use of ccombbox controls in various forums and provides a positive solution.
1. There are no drop-down items in the ccombobox dialog box.
A: In the dialog box in the resource editor, the vertical range of the ccombobox component is extended, and the font is displayed. Do you see the downward Black Arrow? Click it and pull it down. It's so easy.
2. How can I enable ccombobox to remember users' input?
A: Remember that user input must be read/written to a file, or add the content to the Registry. I will provide a solution later.
3. How to Make ccombobox read-only and cannot be edited
A: After reading the introduction in this article, you may know that ccombobox has three types. select what you need!
4. I want to display the default data in the combo box as soon as the interface is displayed.
A: You just need to add the setcursel () function I mentioned above in oninitdialog.
5. How to assign the selected content to variables
A: Set a cstring correlation variable for ccombobox first, and then add the event processing function CBN _ selchange. Use updatedata (false.
6. In the ccombobox box, the alignment of entries is left-aligned. Can you set it to align or right-aligned?
A: There is no direct method. You must align yourself. For example, the maximum length is 10.
Int I;
String. Format ("% 10d", I );
Ccombobox-> addstring

 

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.