?
Solved

Display comboBox "Value" rather than "Text"

Posted on 2014-02-22
11
Medium Priority
?
2,054 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 
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
 

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 63

Accepted Solution

by:
Fernando Soto earned 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

800 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