-
August 6th, 2013, 08:44 AM
#1
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
-
August 6th, 2013, 11:54 AM
#2
Re: Can't set text in Edit Box of ComboBox
Originally Posted by jg028461
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
-
August 6th, 2013, 12:52 PM
#3
Re: Can't set text in Edit Box of ComboBox
Originally Posted by VictorN
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?
-
August 6th, 2013, 01:32 PM
#4
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
-
August 6th, 2013, 11:45 PM
#5
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
-
August 7th, 2013, 06:37 AM
#6
Re: Can't set text in Edit Box of ComboBox
Originally Posted by jg028461
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
-
August 7th, 2013, 08:41 AM
#7
Re: Can't set text in Edit Box of ComboBox
Originally Posted by Igor Vartanov
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.
-
August 9th, 2013, 06:43 AM
#8
Re: Can't set text in Edit Box of ComboBox
Originally Posted by jg028461
Its having little complex functionality but whatever i am trying to achieve is easily possible.
I could do a lot of things that basically possible, but I never do that because it will confuse my customers.
Best regards,
Igor
-
August 6th, 2013, 04:03 PM
#9
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)
-
August 6th, 2013, 11:59 PM
#10
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.
-
August 7th, 2013, 05:59 AM
#11
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)
-
August 7th, 2013, 08:27 AM
#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
-
August 7th, 2013, 08:29 AM
#13
Re: Can't set text in Edit Box of ComboBox
I am sure, im using right window handle.
-
August 7th, 2013, 08:46 AM
#14
Re: Can't set text in Edit Box of ComboBox
Originally Posted by jg028461
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)
-
August 8th, 2013, 12:26 AM
#15
Re: Can't set text in Edit Box of ComboBox
I'm not convinced you are.
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.
hello 2kaud
Lets say this SendMessage is working in CBN_SELCHANGE according to you(I have tried from my side its not working), but after this CBN_SELCHANGE returns, the value in edit box will be set as current selection.
How should i stop it to set it current selection from list box(I know its normal behaviour of combo box but thats not working in my case).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|