CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Aug 2013
    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,
    Jitendra

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

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

    Quote Originally Posted by jg028461 View Post
    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.
    Why do you think you do need it?
    What is the main goal of such a behaviour?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2013
    Posts
    12

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

    Quote Originally Posted by VictorN View Post
    Why do you think you do need it?
    What is the main goal of such a behaviour?
    Thanks for replying.
    I am using ON_CBN_SELCHANGE.
    in that function I want to concatenate my string with string(which I am retrieving by index).
    and after concatenation I am setting it to window using SetWindowText()
    But its not working...can u please tell what I am missing here?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

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

    I don't care which message handler you are going to use.
    What for do you want to edit the content of the edit box before it shows up?
    And what are you going to with the updated text after it will have been displayed?
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    When posting code, please first format and use code tags. Go Advanced, select the code and click '#'.

    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
    You need to set the text in the edit box as part of CBN_SELENDOK message. You also need to handle CBN_SELENDCANCEL message.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Aug 2013
    Posts
    12

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

    Hello Victor,
    i will give you clear vision where i am using it.
    i have a mail app. when i write a new mail, it shows me "To,Subject,Editor" and subject field consist of different-2 pre defined templates for mail. i can choose them from subject combo box(DROPDOWN).

    These steps will make you more clear.
    1.Write something on editor.
    2.i go to subject combo box, it shows me template name highlighted in edit box of combo box.
    3.i write my text next to template name in subject box(suppose template name is "SAMPLE TEMPLATE", i modified it to "SAMPLE TEMPLATE mytext").
    4.now, if i select another template.
    5.according to my program, it asks from user that user wants to cancel it.??
    6.when i press cancel the text with template name and what user wrote should be retained in the subject edit box.. it should perform as cancel button does in general case.

    i am having this cancel dialog in OnSelChange() of this combo box.
    when i check a condition that if its 'CANCEL' then i am using ReplaceSel(), SetWindowText() but these function are not working.

    how to make these function working in OnSelChange() event...??

    Thanks

  7. #7
    Join Date
    Aug 2013
    Posts
    12

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

    hello 2kaud,
    Thanks for replying.
    CBN_SELENDCANCEL-the user clicks an item and then clicks another window or control to hide the list box of a combo box. This notification message is sent.
    This is not working. If il capture this event Changes will take effect only when user click away from combo box.
    and also ON_CBN_SELENDOK occurrence of this event can be before or after ON_CBN_SELCHANGE .
    i am already capturing ON_CBN_SELCHANGE, is it possible to set the text in this event only?
    i am handling ON_CBN_SELCHANGE for some other reasons also.so if i use CBN_SELENDCANCEL or ON_CBN_SELENDOK they will be called before ON_CBN_SELCHANGE and changes will not be effective. so i think these function will not work in this case.
    so i think i will need to set the text after ON_CBN_SELCHANGE returns(because after this function is called, edit box is set to a 'current selection' from list box by framework)

    Thanks
    Last edited by jg028461; August 7th, 2013 at 02:21 AM.

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    As far as I am aware with the testing I have done for my own controls, if you need to alter the text of the edit box after the text has been selected then this needs to be done in response to the CBN_SELENDOK message.

    The way I do this for some of my controls is to SetWindowText() when CBN_SELCHANGE is received and to also SetWindowText() for CBN_SELENDOK. If CBN_SELENDCANCEL is received then I use SetWindowText() to reset the edit text to the initial value.

    PS You are using the right window handle for SetWindowText() ? Use CB_GETCOMBOBOXINFO message to get info about the combo control and hwndItem in the COMBOBOXINFO struct is the required window handle. Just using the window handle for the combo box doesn't work!
    Last edited by 2kaud; August 7th, 2013 at 06:13 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

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

    Quote Originally Posted by jg028461 View Post
    i will give you clear vision where i am using it.
    i have a mail app. when i write a new mail, it shows me "To,Subject,Editor" and subject field consist of different-2 pre defined templates for mail. i can choose them from subject combo box(DROPDOWN).
    I would say that this must be not a plain combobox, but some custom control instead showing distinctly different behavior. Standard combo has standard behavior, and changing it will confuse your customers.
    Best regards,
    Igor

  10. #10
    Join Date
    Aug 2013
    Posts
    12

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

    Hello 2kaud,

    Thanks, i appreciate your help but
    I have checked it when i change item in my subject combo box, For the First time framework is making call to CBN_SELCHANGE before CBN_SELENDOK but after they are called in reverse.
    This is happening because CBN_SELCHANGE is not predictable and can be called before or after CBN_SELENDOK as per my knowledg

  11. #11
    Join Date
    Aug 2013
    Posts
    12

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

    I am sure, im using right window handle.

  12. #12
    Join Date
    Aug 2013
    Posts
    12

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

    Quote Originally Posted by Igor Vartanov View Post
    I would say that this must be not a plain combobox, but some custom control instead showing distinctly different behavior. Standard combo has standard behavior, and changing it will confuse your customers.
    Hello Igor,
    Thanks for showing your interest.

    Its a DropDown box. Its having little complex functionality but whatever i am trying to achieve is easily possible.

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    Quote Originally Posted by jg028461 View Post
    I am sure, im using right window handle.
    I'm not convinced you are.

    Code:
    int index = SendMessage(m_hRecipients, CB_GETCURSEL, (WORD)0, 0L);
    ...
    ComboBox_SetText(m_hRecipients, (LPCWSTR)csRecipients);
    should have different window handle values. SendMessage should be the combo handle and ComboBox_SetText should be the edit handle (assuming that as you state ComboBox_SetText is equivalent to SetWindowText). It might be useful to post the code for ComboBox_SetText. Also you are not checking the return values of ComboBox_SetText to see if an error occured.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

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

    Quote Originally Posted by jg028461 View Post
    ... I want to edit it before it shows up in the Edit Box. So I capture the CBN_SELCHANGE message ...
    Did you consider using other notifications instead (or in addition to), for example CBN_CLOSEUP, CBN_EDITUPDATE?
    Victor Nijegorodov

  15. #15
    Join Date
    Aug 2013
    Posts
    12

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

    Did you consider using other notifications instead (or in addition to), for example CBN_CLOSEUP, CBN_EDITUPDATE?
    Hello victor
    In documentation its written clearly that CBN_CLOSEUP can be called before or after CBN_SELCHANGE.
    When CBN_SELCHANGE returns, the edit box of combo is updated with current selection. At that time it doesn't make call to CBN_EDITUPDATE.

    CBN_EDITUPDATE gets called only when you place cursor and try to change in the edit box. Its not called when you set a text in editbox.

    PS-I am already using CBN_SELCHANGE so i cant replace it with CBN_CLOSEUP. i have to go with current functionality only.

    Thanks
    Last edited by jg028461; August 8th, 2013 at 12:28 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured