If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

Can't set text in Edit Box of ComboBox

Hi,

I have a Combo Box and when the user selects an item from the dropdown, I want to edit it before it shows up in the Edit Box. So I capture the CBN_SELCHANGE message and I read the user's selection, edit it a bit, and then set it in the Edit Box using ComboBox_SetText(hwndCtl, lpsz) which is equivalent to SetWindowText((hwndCtl), (lpsz)). After I set it I read it back and it is set. However, it does not show up in the Edit Box to the user. The contents of the Edit Box is the dropdown item the user selected and not what I set. I don't understand what I'm doing wrong.

Re: Can't set text in Edit Box of ComboBox

I played a little bit around with similar code.

What you're trying to achieve seems to be not working since the control resets the contents of the edit box "at the same time" you're changing it.

What happens during debugging (Win XP SP 2):
- you get the correct string in CBN_SELCHANGE (from the list box part)
- you're changing the contents of the edit box part
(Which is working like you've tried. Receiving the contents again seems to indicate that the changes are made.)
- The string(s) in the list box part are unchanged, so the original behaviour overwrites your changes and therefore you don't see any change. Windows simply put the original string again since that was selected by the user.
- To get it to work you must have been changed the string in the list box part too. This is only possible by deleting a string (CB_DELETESTRING) and inserting the edited string at the same position (CB_INSERTSTRING). There is no CB_EDITSTRING message. And that is the problem: I tried to delete and re-insert the string. It is kind of working, the string in the list box part is changed perfectly. But: While doing a delete operation the selection is going away. That results in an empty string in the edit box part, loosing the selection but having correct "recipients" strings in the list box part.

I would suggest:

Add a button to your dialog and do all the the necessary stuff while handling the click of this button. This is straight-forward to program and it is more clear for the user too. Remember that there are more possibilities to change the selection in your combo box: Mouse, keyboard, changing it's contents programmaticaly.
I had to deal with Windows software trying to handle the selection change of list/combo boxes the same way you're trying. It was a nightmare!
The most straigtforward solution IMHO is:

A list box (no combo box) with the entries (recipients).

A "Delete From Recipients" button (deletes the currently selected recipient from the list)

A "Insert new recipient" button (inserts a new recipient, either at the end or at the current selection)

Maybe a "Edit selected recipient" button

The concatenation of the recipients to one string (comma or semicolon separated) can be done at the end.

Re: Can't set text in Edit Box of ComboBox

Thank you very much PA and VictorN for responding. I apologize that I didn't use Code tags. I didn't know what they were but now I do.

Using the PostMessage call rather than the SendMessage call (which is what ComboBox_SetText uses) didn't make any difference. The edit contents still didn't show the updated value.

Also I need to use a ComboBox rather than a ListBox because I need the user to be able to edit the contents. This is an e-mail application and I don't want to add extra buttons that the user has to select before they get an address in the from the ComboBox into the "To:" Edit Box. Once I get the Edit Box contents displayed correctly, I will work on auto completing and address from the ComboBox while the user is typing in addresses. Equivalent to what GMail and Outlook do.

Anyway, I need to get this to work. I noticed that after the CBN_SELCHANGE message, the onReceive also gets a CBN_SELENDCANCEL message. I would think that it would get a CBN_SELENDOK but it doesn't. So when I get the CBN_SELENDCANCEL, I again call ComboBox_SetText with the text I want to appear in the EditBox. What appears is still the item selected from the ComboBox and not my modified contents. But when I click anywhere else afterwards, the EditBox contents changes and my modified string appears in the EditBox. But I can't figure out how to force a click or an equivalent in the CBN_SELCHANGE before I return. I know this sounds convoluted but if I can generate a click equivalent this may just work.

I'm very new to Windows programming even though I have a lot of experience in other languages. That's why I am asking for help.

Re: Can't set text in Edit Box of ComboBox

Dear PA,

Thank you very much for the suggestion. It does indeed work. However, my ComboBox list is then compromised. It replaces the item selected with the current recipient list. I'm not sure where to set it back. I tried to set it back in the CBN_SELENDCANCEL message but that didn't work. I would like to reinstate the ComboBox to what it was originally. Any idea where and when I could do that.

Re: Can't set text in Edit Box of ComboBox

Hi,

Although replying to very old post, here is the solution :

Originally Posted by rzdybel

Thank you very much for the suggestion. It does indeed work. However, my ComboBox list is then compromised. It replaces the item selected with the current recipient list. I'm not sure where to set it back. I tried to set it back in the CBN_SELENDCANCEL message but that didn't work. I would like to reinstate the ComboBox to what it was originally. Any idea where and when I could do that.

Adding to the 'ProgramArtist' solution, you should set the combo list again in 'CBN_DROPDOWN' event. Try this :