Solved

Display comboBox "Value" rather than "Text"

Posted on 2014-02-22
11
1,904 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:pclarke7
Comment Utility
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
Comment Utility
:-(
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
Comment Utility
Hi Andy,
did you get anywhere with this ?

regards
Pat
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

9 Experts available now in Live!

Get 1:1 Help Now