Can't set text in Edit Box of ComboBox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

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

  1. #1
    Join Date
    Oct 2011
    Posts
    12

    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.

    So basically, I'm doing the following:

    case CBN_SELCHANGE:

    {
    /* Get index of current selection and the text of that selection. */
    TCHAR buffer[200];
    int index = SendMessage(m_hRecipients, CB_GETCURSEL, (WORD)0, 0L);
    SendMessage(m_hRecipients, CB_GETLBTEXT, (WPARAM)index, (LPARAM)buffer);

    //Get current contents of Edit Box
    int nLngth = SendMessage(m_hRecipients, WM_GETTEXTLENGTH, NULL, NULL);
    csRecipients = (TCHAR *)CoTaskMemAlloc(nLngth);

    SendMessage(m_hRecipients, WM_GETTEXT, nLngth+1, (LRESULT)csRecipients);

    // concat new selection to contents of Edit Box (current rescipient list)
    if (nLngth > 0) tcscat(csRecipients, L",");
    _tcscat(csRecipients, buffer);

    // set the new contents of the Edit Box
    ComboBox_SetText(m_hRecipients, (LPCWSTR)csRecipients);

    return TRUE;
    }

    Can anyone suggest what I'm doing wrong that the Edit Box doesn't persist the contents I set?

    Thank you very much,
    Rakefet Zdybel

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    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.

    With regards
    PA

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

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

    Quote Originally Posted by rzdybel View Post
    ...
    So basically, I'm doing the following:
    Code:
    case CBN_SELCHANGE:
    		
    {
    		...
    // set the new contents of the Edit Box
    		ComboBox_SetText(m_hRecipients, (LPCWSTR)csRecipients);
    
                    return TRUE;
    }
    l
    Try to
    Code:
    PostMessage(m_hRecipients, WM_SETTEXT, 0, (LPARAM)(LPCWSTR)csRecipients);
    instead.

    PS: please, next time use Code tags around code snippets.
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2011
    Posts
    12

    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.

    Thank you very much for all your help,
    Rakefet Zdybel

  5. #5
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

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

    I tried to modify your code from the original post:

    Code:
    {
    /* Get index of current selection and the text of that selection. */
        TCHAR buffer[200];
        int index = SendMessage(m_hRecipients, CB_GETCURSEL, (WORD)0, 0L);
        SendMessage(m_hRecipients, CB_GETLBTEXT, (WPARAM)index, (LPARAM)buffer);
    
        //Get current contents of Edit Box
        int nLngth = SendMessage(m_hRecipients, WM_GETTEXTLENGTH, NULL, NULL);
        csRecipients = (TCHAR *)CoTaskMemAlloc(nLngth);
    
        SendMessage(m_hRecipients, WM_GETTEXT, nLngth+1, (LRESULT)csRecipients);
    
        // concat new selection to contents of Edit Box (current rescipient list)
        if (nLngth > 0) tcscat(csRecipients, L",");
        _tcscat(csRecipients, buffer);
    
        // set the new contents of the Edit Box
        ComboBox_SetText(m_hRecipients, (LPCWSTR)csRecipients);
        
        SendMessage(m_hRecipients, CB_DELETESTRING, index, 0L);
        SendMessage(m_hRecipients, CB_INSERTSTRING, index, (LPARAM)csRecipients);
        SendMessage(m_hRecipients, CB_SETCURSEL, index, 0L);
    
        return TRUE;
    }
    The only thing I changed (added) are the 3 lines before the return statement.
    I tried this (but with a MFC dialog). It was working for me.
    So can you try out if this helps in your code?

    Regards
    PA

  6. #6
    Join Date
    Oct 2011
    Posts
    12

    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.

    Regards,
    Rakefet

  7. #7
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

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

    Could you explain as clearly as possible what exactly you want to achieve?

    As far as I know now from your posts you want to:

    • Select something in the list box part of the combo box (That's why you intercept the CBN_SELCHANGE notification).
    • Then you want to change the edit box part of the combo box in some way not connected with the entries in the list box part.

    This is counter-intuitive for me (and IMHO for all users too). And it is very very hard to implement since you're trying to bend the combo box to your requests.

  8. #8
    Join Date
    Oct 2011
    Posts
    12

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

    Dear PA,

    Yes, that is exactly what I want to do. My Edit/ComboBox is being used for an e-mail client for the "To" field in which the user can concatenate address with a comma or semi-colon used as a delimiter. Most e-mail clients work like this. This is how the GMail client works as well as Outlook. When the user begins typing an address in the "To" EditBox, I want to dropdown a list of addresses that match what they have typed so the user does not have to type the whole address if it is in the list. The user can select the address they want from the list, I will intercept that address, add a comma and concatenate to what is already in the EditBox. So yes, I am changing the contents of the EditBox portion of the selected item from the ComboBox and resetting it to the new list of receipients in the EditBox. But I can't do that because I can't set the EditBox portion of the ComboBox when I get the CBN_SELCHANGE notification. It's funny because I set the contents of the EditBox ("Please enter recipients") when I first create the ComboBox.

    The documentation says that I can do this but it's not working as you and I have both experienced.

    Thank you for all your help. I appreciate it.

    Rakefet

  9. #9
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

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

    Quote Originally Posted by rzdybel View Post
    Most e-mail clients work like this. This is how the GMail client works as well as Outlook. When the user begins typing an address in the "To" EditBox, I want to dropdown a list of addresses that match what they have typed so the user does not have to type the whole address if it is in the list. The user can select the address they want from the list, I will intercept that address, add a comma and concatenate to what is already in the EditBox.
    No, my Outlook 2002 doesn't use a combo box for this purpose. It uses a combination of an edit box and a button exactly as I did suggest:

    Name:  outlook2002.gif
Views: 1585
Size:  2.7 KB

    (Note: This is a German Outlook. "An" = "To", "Von" = "From", "Betreff" = "Subject")

    With regards
    PA
    Last edited by ProgramArtist; October 10th, 2011 at 03:11 AM.

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,553

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

    Wouldn’t it be easier to handle WM_SETTEXT in that edit control and “massage” the text there?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  11. #11
    Join Date
    Aug 2013
    Posts
    12

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

    hi,

    i am facing same problem.
    do you have any update on this?

    how to set user defined text of combo in ON_CBN_SELCHANGE function.

    Thanks
    Jitendra

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center