Changing ComboBox text

  • Thread starter Thread starter Michael J. Salamone
  • Start date Start date
M

Michael J. Salamone

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?
 
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
 
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.
 
Hi Michael,

Michael J. Salamone said:
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?

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/d...ystemwindowsformscontrolclasswndproctopic.asp

Kind regards,
 
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);
}
 
Back
Top