Solved

Display comboBox "Value" rather than "Text"

Posted on 2014-02-22
11
1,923 Views
Last Modified: 2014-03-16
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; }
}
0
Comment
Question by:pclarke7
  • 5
  • 4
  • 2
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39879388
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();
         }
0
 

Author Comment

by:pclarke7
ID: 39879699
Hi Andy,
that still does not work. Text is still displayed as "Number 1" rather than "one"

regards
Pat
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39879703
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?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39879752
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
         }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39879757
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:pclarke7
ID: 39879759
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
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39880267
:-(
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.
0
 

Author Comment

by:pclarke7
ID: 39888739
Hi Andy,
did you get anywhere with this ?

regards
Pat
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39891109
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).
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39891677
Hi Pat.

As I stated in the prior post, 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.
0
 

Author Closing Comment

by:pclarke7
ID: 39932494
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access Web appliction 7 47
Why Does This Page Not Show Up? 4 22
Change Format on Data when exporting to Excel 4 23
SQL Login 17 38
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now