Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

Display comboBox "Value" rather than "Text"

Hi
I have a comboBox which has the following Text & Values

Text                     Value
Number 1           one
Number 2           two

When I display the comboBox it displays "Number 1" &  "Number 2" . When I Click on Number 1 I want "one" to be returned to the comboBox Text. This seems to happen momentarily but it reverts back to "Number 1". Appreciate any input into what I am doing wrong. See code below:

regards
Pat

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                Item item = comboBox1.Items[comboBox1.SelectedIndex] as Item;
                comboBox1.Text = item.Value.ToString();
         }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new Item("one" , "Number 1"));
            comboBox1.Items.Add(new Item("two" , "Number 2"));
            comboBox1.SelectedIndexChanged += new EventHandler (comboBox1_SelectedIndexChanged);
        }
    }
}

public class Item
{
    public Item(string value, string text) { Value = value; Text = text; }
    public string Value { get; set; }
    public string Text { get; set; }
   
    public override string ToString() { return Text; }
}
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

If you want that to be deisplayed then you need to change the text of the item.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                Item item = comboBox1.Items[comboBox1.SelectedIndex] as Item;
item.Text = item.Value.ToString();
                //comboBox1.Text = item.Value.ToString();
         }
Avatar of pclarke7
pclarke7

ASKER

Hi Andy,
that still does not work. Text is still displayed as "Number 1" rather than "one"

regards
Pat
If you are trying to display in the drop down list one value and have a different value in the ComboBox TextBox then that will not work. Is that what you are trying to do?
It will work (eventually), you just might have to force the combo to redraw.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                Item item = comboBox1.Items[comboBox1.SelectedIndex] as Item;
item.Text = item.Value.ToString();
                comboBox1.Text = item.Value.ToString();  //Force the combo to redraw - rather crudely
         }
ps.  Take note of what I am doing in that code.  I'll repeat part of my initial comment with a highlight:
you need to change the text of the item

If you need to maintain the original item contents then you would have to use some smoke and mirrors, eg. having a boolean flag in the class Item which determines what is shown (Text or Value) by the Item
Hi Andy,
still doesn't work , even with the redraw. When I run the program it pops up a window with the comboBox with two options "Number 1" & "Number 2". When I click on "Number 1" I would expect the text "one" to appear in the comboBox text. However it just returns "Number 1" to the text.

regards
Pat
:-(
You're correct.  I've tested it here.  It looks like the combo is caching the display text even though with single stepping the ToString of the Item class is being called and returning the changed value.
I've played with it by changing the ValueMember property which does change the displayed text for only the first time something is selected - a further hint that caching might be the cause of this not updating the display text.

You could resort to overriding the DrawItem but that seems overkill.  This ought to be simple and is bugging me now.
Hi Andy,
did you get anywhere with this ?

regards
Pat
Sorry, no.  I've been modifying DisplayMember, setting the DataSource and various other tricks that I know of from non .net environment.  No joy.  This ought to be trivial (and is in some other non .net programming languages).  Often one finds things in .net are quicker to code than in a classical language but occasionally one runs into something that has been made very difficult or impossible.  This seems to be one of those cases.  (Or I've not found the right trick).
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Fernando,
I had hoped that there would be some way of displaying one value in the drop down box but returning a different value when selected. However as you have said from the beginning ,this does not seem to be possible.

regards
Pat