Changing Text in ComboBox Based Upon Selection

O

O.B.

I have a System.Windows.Forms.ComboBox (called bandwidthComboBox) in
my application with the following dropdown items:
T1 (1.544 Mbps)
T2 (6.312 Mbps)
T3 (44.736 Mbps)

When the user selects an item from the list, I want the text box to
only display the numerical value. I have added a callback for
SelectedIndexChanged, SelectedValueChanged, and
SelectionChangeCommitted as follows but the ComboBox is still showing
the selected value rather than just the number. Help?

private void OnSelectBandwidth(object sender, EventArgs e)
{
double value = 0.0;
switch(bandwidthComboBox.SelectedItem.ToString())
{
case "T1 (1.544 Mbps)":
value = 1.544;
break;
case "T2 (6.312 Mbps)":
value = 6.312;
break;
case "T3 (44.736 Mbps)":
value = 44.736;
break;
}
bandwidthComboBox.Text = value.ToString();
}
 
P

proxyuser

I agree that the attempt is probably misguided. You might want to look into
a different control such as a grid combo of some sort.

Having said that, I don't know why you can't do what you want to do. It
might have to do with some tangle of messages that the control responds to
in some mysterious order, or it could even be a bug in the .NET code (but
probably not.)
 
P

proxyuser

Peter Duniho said:
You mean the TextBox that's part of the ComboBox? I don't think you can
do that, not with the built-in ComboBox. To the control, what's displayed
in the TextBox area has to be in the same format as what's displayed in
the ListBox area. You would have to change what's in the ListBox area to
allow that, and then of course your ListBox area wouldn't be what you want
(if I understand the question).

That's not really true though (with a combo box with the default
DropDownStyle of DropDown). The user can type in whatever he wants, so you
would think the programmer could too. (The programmer can, of course. If
you just set the text during initialization of the program, it would work
fine. I think the problem has something to do with the order of events that
are happening once the selected item/selected index changes.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top