Combo Box Question

  • Thread starter Thread starter orekinbck
  • Start date Start date
O

orekinbck

Hi There

I have a combo box with DropDownStyle = DropDownList

The user wants a different string to appear in the display rectangle
than from the strings in the list.

So for example, the drop down list items might say
One
Two
Three

Then when I select 'One' and move focus away from the away from the
combo box, the combo box shows '1' ... (instead of 'one').

Any ideas? At the moment I am covering the combo box with a text box
and using SelectedIndex changed event to populate the text box, but I
was hoping there was a more elegant solution.

TIA
Bill
 
Hey,

This solution is only right if you want the display change on lost focus
event:

Create a class that will be the object added as items to the combo:

public class Class2

{

private int _Value;

private string _StringValue;


public Class2(int numericValue, string stringValue)

{

this._Value = numericValue;

this._StringValue = stringValue;

}

Add getters for the values and override the ToString() method to display
anything you want...

(Check the DisplayMember property of the combo box).

then, set those event handlers..

private void comboBox1_GotFocus(object sender, EventArgs e)

{

((ComboBox)sender).DisplayMember = "StringValue";

}

private void comboBox1_LostFocus(object sender, EventArgs e)

{

((ComboBox)sender).DisplayMember = "Value";

}

Hope this helps,

- Moty -
 
Back
Top