[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Display comboBox "Value" rather than "Text"

Posted on 2014-02-22
11
Medium Priority
?
2,080 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 45

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 64

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 45

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 45

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 45

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 45

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 64

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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