Solved

How to modify combo box displayed selection without changing available selections.

Posted on 2014-01-16
6
1,026 Views
Last Modified: 2014-01-20
I have a dialog which contains a combo box.
The combo box has several items in its drop down list.
One item in the drop down list selects an option requiring the user to input a number.
If the user selects this item from the combo box drop down list, a modal pop up will read the number.
At this point, the combo box displays the text corresponding to the user's selection and the application has stored the number read from the pop up into a variable.

We want to change the the text displayed in the combo box selection to contain the number read from the popup WITH OUT changing the text in the drop down selections.

The question is:
Is there some method the C++ application can use to change the displayed combo box selection without modifying the text in the drop down text, just change the text displayed in the edit box area of the combo box?

I can use GetDlgItemText() to read combo box displayed selection, but SetDlgItemText() doesn't change the combo box display.

Being able to do this would keep the user informed of what option was chosen as well as display the value entered without disturbing possible future drop down list choices and would not require extra screen dialog space.

Thanks for your help.
0
Comment
Question by:dfft
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39786203
Sending a 'CB_SETITEMDATA' (http://msdn.microsoft.com/en-us/library/windows/desktop/bb775909%28v=vs.85%29.aspx) to the Combo Box allows you to do that. E.g.

TCHAR buf[32];
_stprintf(buf,_T("New Value: %d", nVal");
int nVal = 42; // the one you got from the user
SendMessage(GetDlgItem(hDlg,IDI_MYCOMBOBOX),CB_SETITEMDATA,INDEX_OF_VALUE,(LPARAM)buf);

Open in new window

0
 

Author Comment

by:dfft
ID: 39786695
I appreciate your prompt response to my question.

Am I incorrect in understanding that CB_SETITEMDATA message will replace the drop down list string enumerated by the value in INDEX_OF_VALUE with the string contained in buf?

That would change the contents of the drop down list and leave the text displayed in the text box unchanged?

If that is correct, I am asking how to do the opposite. After the user has selected an item from the drop down list, I want to change the contents of the text box and leave the contents in the drop down list unchanged.

This would allow the user to see in the combo box's text box which option was chosen from the drop down list AND what value he supplied without modifying future possible selections from the drop down list.

I apologize for wording my question so badly.

Maybe I should be asking if there is a way to obtain the ID of the text box in the combo box control so that I can send commands to it directly without disturbing the drop down list?

Should I be rolling my own combo box? Not sure I have the skills to do that in the time frame I have to work on this.

Thanks
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39786722
>>That would change the contents of the drop down list and leave the text displayed in the
>>text box unchanged?

Wait a minute - if you are changing the value of the selected item the changes should be reflected in both places. Why not giving it a try, since it is basically one line of code...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dfft
ID: 39786908
Thanks
Previously, I was wrong about CB_SETITEMDATA, it will change the value associated with an entry in the drop down list, not change the contents of the drop down list.

>>Wait a minute - if you are changing the value of the selected item the changes should be >>reflected in both places. Why not giving it a try, since it is basically one line of code...

If this is true, then I can't change the contents of the combo box's text box without changing the contents of the drop down list?

I think you are writing that if, say the 5th item in a drop down list is selected and displayed in the text box, that if I change the contents of the 5th item in the drop down list, then the contents of the text box will automatically change to what the 5th (previously selected) item in the drop down list has been changed to? Perhaps when the window is redrawn or updated?

What I have is a drop down list of five items.
0. Use Option A
1. Use Option B
2. Use Option C N times
3. Use Option D
4. Use Option E M times

If the user chooses "Use Option C N times" then "Use Option C N times" will be displayed in the text box when the drop down list rolls up. and the application code pops up a dialog asking for the value of N.

The thought was that if the text box in the combo box control displayed
 "Use Option C 23 times" (if the user responded to the pop up with 23), then the user would see what option was chosen in the main dialog while the user went about selecting other controls.
If the user later came back to the combo box, the drop down list would still display
" Use Option C N times" as item 2 and be a reminder that if item 2 is chosen, a number will be required.

I wasn't considering that the selection in the  text box being displayed in the combo box control might automatically be redrawn some how and overwrite whatever I may have managed to put there.

So, I'll give it up and add another item to the main dialog which is already crowded.

Thanks for your patience and help.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 39787799
you could use an edit field, a button, and a listbox to simulate a combobox where actually you have 3 controls which were independent.

or, probably better, when the user selects the entry 'Use Option n times' you move a former hidden edit field (or spin buttons) directly to the place where the cursor currently is and allow the user to set 'n' that way. after that you could display the 'n' in a separate field. i think that changing the text both in the drop-down list and in the edit part of the combobox also would be an acceptable solution.

Sara
0
 

Author Comment

by:dfft
ID: 39795136
Actual solution was to dynamically add an additional item to the drop down list formatted with the desired text  using CB_ADDSTRING message and then using CB_SETCURSEL message to cause the new text to appear in the combo box test box.

But:
Needed to add code for OnCbnDropdown and OnCbnSelendcancel mesages to handle the logic to restore the original dropdown list if the user selected it again to change values or to reinstate modified the dropdown list if user selected the dropdown list again but chose to cancel.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now