Click here to Skip to main content
15,030,320 members
Articles / Desktop Programming / MFC
Posted 19 Feb 2004


38 bookmarked

Combo Box with Both Read-only Items and Editable Items

Rate me:
Please Sign up or sign in to vote.
4.52/5 (17 votes)
19 Feb 2004CPOL3 min read
A combo box that consists of both read-only items as well as editable items that depending on selection, locks and unlocks the edit field of the combobox

Sample screenshot

Figure 1: The first editable item is selected. The edit field of the combo box can be edited.

Sample screenshot

Figure 2: A read-only item is selected. The edit field of the combo box is now read-only.


I was working on a trading application where it was possible to enter orders to buy shares in various stocks. When clicking on a stock, the window for entering orders was opened. There the trader, among other parameters, could enter a quantity. My client requested, it should be possible to have a default value there. Default value could either be whatever is available in the market, last order's quantity or some fixed number. Setting this default value could have been done by using several standard controls together, which would be used to enter a default quantity. For example, a couple of radio buttons to decide which kind of default quantity, and if fixed number was selected, another edit field could be enabled where an integer could be entered. Since screen space is valuable, several coupled controls are more complicated for users and developing custom controls are really fun, we decided to go for a custom control.

User Guide

The read only combo box has one editable (non read-only item), the first item. When this item is selected, the edit field of the combo box becomes active and can be updated by the user. Whenever the text in the edit field is changed, the first item is changed as well. The other items are read only. When selected, the edit field of the combo box will become read only. The user can now either select one of the predefined values or select the editable item and enter any value.


Add a normal combo box resource to your window. Set the following properties on the resource:

  • Type: Dropdown
  • Owner Draw: Fixed
  • Has Strings: checked

Next, add a control variable for the combo box resource using class wizard. In the h file, change the data type from CComboBox to CReadOnlyComboBox. Then, after the control been created, call the AddString-method which will add items, making the first item editable. The combo box is initialized in the method CComboTestDlg::OnInitDialog() in this example. We use a data member called m_combo belonging to CComboTestDlg for the combo box.

CReadOnlyComboBox inherits publicly from CComboBox, so all functionality of a normal combo box is available. Items should however be added with CReadOnlyComboBox::AddString().

The following three methods in CReadOnlyComboBox do all the work. Below is a short explanation of each method and usage.

void CReadOnlyComboBox::OnSelchange()

OnSelchange is called when the user changes the current selection in the list box of the combo box. Depending on if the user selected a read-only item or not, the edit field of the combo box is set to be read only or set to be active.

void CReadOnlyComboBox::OnEditupdate()

OnEditupdate is called after the user has taken an action that may have altered the text in the edit control portion of a combo box. This means that item zero was previously selected since this is the only way to enable the edit field, otherwise the edit field is read-only. We update the first item in the list box of the combo box to reflect the change.

void CReadOnlyComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

Called by the framework when a visual aspect of an owner-draw combo box changes. This is called once per item that needs to be drawn. This function will draw the text of the item as well as change the color of the item's background. Different colors are used for the read only item and the editable item. Also, if the user selects an item, we need to highlight the item to display the selection.


  • 20th February, 2004: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
I'm a software developer from Sweden who got tired of snow and cold weather and moved to USA. I choose New York City, so I wouldn't totally miss out on snow and cold weather. I work on Wall Street with financial systems (not much else to do in this neighborhood). I primarily use Visual C++/MFC or C#/.NET as development tool.

The picture is of my wife and me in Cannes, France, drinking the most expensive Coke we ever had.

Comments and Discussions

GeneralMy vote of 1 [modified] Pin
batati7-May-09 9:24
Memberbatati7-May-09 9:24 
Generalc# Pin
MrAntonD18-Sep-08 1:59
MemberMrAntonD18-Sep-08 1:59 
GeneralGetCurSel() Question... Pin
rbid13-Apr-04 23:26
Memberrbid13-Apr-04 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.