|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Changing ComboBox textThis may seem a bit odd, but I'd like to set the text in a ComboBox control when an item is selected from the drop down list. I want the text to be different than the display text of the selected item. For example, if "x" is the display text of the selected item, I want the text in the text box to be set to "y". I tried changing the text on SelectedIndexChanged event, but the text is set (by the control) to the display text of the selected item *after* this event. I've also tried TextChanged (same problem as SelectedItemChanged) and TextUpdate (but this only fires when the user enters text directly in the text box). In Win32, I do this at WM_COMMAND/CBN_SELCHANGE time, which I would have thought equates to SelectedIndexChanged in the .NET Framework. Any ideas? Hey Michael,
Strange user experience, but you may use a timer to do the job, if the event model does not help. Let's say that all of your items in the combobox have a list value, displayed in the drop down list, and a display value that you want to show when an item is selected. class MyComboBoxItem { string _listValue; string _displayValue; public MyComboBoxItem(string listValue, string displayValue) { _listValue = listValue; _displayValue = displayValue; } public override ToString() { return _listValue; } public string DisplayValue { get { return _displayValue; } } } Each item added to a combobox or listbox will use the ToString() method of the object passed to Items.Add, so you can create an object MyComboBoxItem("x - the listed value", "y - the value to be displayed"), add it to the combox.Item collection and the combobox will display "x - the listed value" in it's list. Now add a timer control to the form hosting the combobox. It will be a performance hit, but you only need to enable it in the SelectedIndexChanged event. In timer_tick check if (combobox.SelectedItem as MyComboBoxItem).DisplayValue != comboBox.Text. If yes, set combobox.Text = combobox.(SelectedItem as MyComboBoxItem).DisplayValue. I'm not recommending this in any way, but it may do what you're looking for. Michael Show quote "Michael J. Salamone" <mikesa#at#entrek#dot#com> schrieb im Newsbeitrag news:%23d7nLsVBGHA.240@TK2MSFTNGP11.phx.gbl... > Reposting to a larger audience - hope someone can help! > > > This may seem a bit odd, but I'd like to set the text in a ComboBox > control when an item is selected from the drop down list. I want the text > to be different than the display text of the selected item. > > For example, if "x" is the display text of the selected item, I want the > text in the text box to be set to "y". > > I tried changing the text on SelectedIndexChanged event, but the text is > set (by the control) to the display text of the selected item *after* this > event. I've also tried TextChanged (same problem as SelectedItemChanged) > and TextUpdate (but this only fires when the user enters text directly in > the text box). > > In Win32, I do this at WM_COMMAND/CBN_SELCHANGE time, which I would have > thought equates to SelectedIndexChanged in the .NET Framework. > > Any ideas? > > -- > Michael Salamone [eMVP] > Entrek Software, Inc. > www.entrek.com > > > Yes, seems a strange user experience, but I think my users will actually
appreciate it. But, I can't even test it to see if I like it myself! I do use this "technique" in some Win32 code, and the user experience is actually pretty good. Now I've got some new UI in .net code where I want to try it. Some things are still easier (and work!) in native! I'll hope a better solution comes along, but I'll try the timer idea to see how it works in this case. Thanks. Show quote "Michael Höhne" <michael.hoehne@nospam.nospam> wrote in message news:%23CQAbFcBGHA.4016@TK2MSFTNGP11.phx.gbl... > Hey Michael, > > Strange user experience, but you may use a timer to do the job, if the > event model does not help. Let's say that all of your items in the > combobox have a list value, displayed in the drop down list, and a display > value that you want to show when an item is selected. > > class MyComboBoxItem { > string _listValue; > string _displayValue; > > public MyComboBoxItem(string listValue, string displayValue) { > _listValue = listValue; > _displayValue = displayValue; > } > > public override ToString() { > return _listValue; > } > > public string DisplayValue { > get { > return _displayValue; > } > } > } > > > Each item added to a combobox or listbox will use the ToString() method of > the object passed to Items.Add, so you can create an object > MyComboBoxItem("x - the listed value", "y - the value to be displayed"), > add it to the combox.Item collection and the combobox will display "x - > the listed value" in it's list. > > Now add a timer control to the form hosting the combobox. It will be a > performance hit, but you only need to enable it in the > SelectedIndexChanged event. In timer_tick check if (combobox.SelectedItem > as MyComboBoxItem).DisplayValue != comboBox.Text. If yes, set > combobox.Text = combobox.(SelectedItem as MyComboBoxItem).DisplayValue. > > I'm not recommending this in any way, but it may do what you're looking > for. > > Michael > > > > "Michael J. Salamone" <mikesa#at#entrek#dot#com> schrieb im Newsbeitrag > news:%23d7nLsVBGHA.240@TK2MSFTNGP11.phx.gbl... >> Reposting to a larger audience - hope someone can help! >> >> >> This may seem a bit odd, but I'd like to set the text in a ComboBox >> control when an item is selected from the drop down list. I want the >> text to be different than the display text of the selected item. >> >> For example, if "x" is the display text of the selected item, I want the >> text in the text box to be set to "y". >> >> I tried changing the text on SelectedIndexChanged event, but the text is >> set (by the control) to the display text of the selected item *after* >> this event. I've also tried TextChanged (same problem as >> SelectedItemChanged) and TextUpdate (but this only fires when the user >> enters text directly in the text box). >> >> In Win32, I do this at WM_COMMAND/CBN_SELCHANGE time, which I would have >> thought equates to SelectedIndexChanged in the .NET Framework. >> >> Any ideas? >> >> -- >> Michael Salamone [eMVP] >> Entrek Software, Inc. >> www.entrek.com >> >> >> > > Hi Michael,
Show quote "Michael J. Salamone" wrote: Handle the win32 events you mention from .NET code. Override the > Reposting to a larger audience - hope someone can help! > > This may seem a bit odd, but I'd like to set the text in a ComboBox control > when an item is selected from the drop down list. I want the text to be > different than the display text of the selected item. > > For example, if "x" is the display text of the selected item, I want the > text in the text box to be set to "y". > > I tried changing the text on SelectedIndexChanged event, but the text is set > (by the control) to the display text of the selected item *after* this > event. I've also tried TextChanged (same problem as SelectedItemChanged) > and TextUpdate (but this only fires when the user enters text directly in > the text box). > > In Win32, I do this at WM_COMMAND/CBN_SELCHANGE time, which I would have > thought equates to SelectedIndexChanged in the .NET Framework. > > Any ideas? > -- > Michael Salamone [eMVP] > Entrek Software, Inc. > www.entrek.com Control.WndProc method and handle the events you mention (in that case don't delegate to base class implementation). http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasswndproctopic.asp Kind regards, -- Tom Tempelaere. Very promising...
Uh, but it doesn't work :( I get the same behavior. In the WndProc, I set the combobox Text property. But the text is then switched back. Below is my code. I correctly enter the block where I change the text and can view that the text was actually changed (using the debugger). After I set the text, the system is setting it. [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] protected override void WndProc(ref Message m) { // Listen for operating system messages. if (m.Msg == Constants.WM_COMMAND) { IntPtr handle = (IntPtr) m.LParam; if (handle == MyComboBox.Handle) { if (((int) m.WParam & ~0xffff) == Constants.CBN_SELCHANGE) { MyComboBox.Text = "Hello!"; return; } } } base.WndProc(ref m); } Show quote "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_> wrote in message news:1B61B436-C47A-425C-A2B2-4EE2969912BB@microsoft.com... > Hi Michael, > > "Michael J. Salamone" wrote: > >> Reposting to a larger audience - hope someone can help! >> >> This may seem a bit odd, but I'd like to set the text in a ComboBox >> control >> when an item is selected from the drop down list. I want the text to be >> different than the display text of the selected item. >> >> For example, if "x" is the display text of the selected item, I want the >> text in the text box to be set to "y". >> >> I tried changing the text on SelectedIndexChanged event, but the text is >> set >> (by the control) to the display text of the selected item *after* this >> event. I've also tried TextChanged (same problem as SelectedItemChanged) >> and TextUpdate (but this only fires when the user enters text directly in >> the text box). >> >> In Win32, I do this at WM_COMMAND/CBN_SELCHANGE time, which I would have >> thought equates to SelectedIndexChanged in the .NET Framework. >> >> Any ideas? >> -- >> Michael Salamone [eMVP] >> Entrek Software, Inc. >> www.entrek.com > > Handle the win32 events you mention from .NET code. Override the > Control.WndProc method and handle the events you mention (in that case > don't > delegate to base class implementation). > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasswndproctopic.asp > > Kind regards, > -- > Tom Tempelaere. > |
|||||||||||||||||||||||