Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

No ListItem in Windows Forms
Date 12/18/2009    Tags C#    (4)

After working in .Net web development for years, I'm not spending some time doing Windows Forms development. Today I ran into something rather surprising.

When adding values to a drop down list in ASP.net webpages, I would typically create a ListItem with the Name/Value pare and add that to the dropdown. Very simple to do. Windows Forms does not seem to have an equivalent for adding to comboboxes (there is a ListItem object, but it is not the same thing). There for if you want to add an item to the combobox that has differing name/values, you need to implement your own ListItem-like class.

Thankfully it's pretty easy to do just that (you can just copy the code below if you like)...

public class ComboBoxItem
{
    private string name;
    private int value;

    public ComboBoxItem(string initialName, int initialValue)
    {
        this.name = initialName;
        this.value = initialValue;
    }

    public override string ToString()
    {
        return this.name;
    }
}
Now you can add items to your combobox like so:

this.myComboBox.Items.Add(new ComboBoxItem("Item 1", 1));
this.myComboBox.Items.Add(new ComboBoxItem("Item 2", 2));
this.myComboBox.Items.Add(new ComboBoxItem("Item 3", 3));
Notice that this class also includes an override for ToString(), which allows the display on the combobox to be the Name instead of the Value.

I think you may also be able to use the web (ASP.net) version of this object to do this (System.Web.UI.WebControls.ListItem) but something about mixing Web and Windows Forms elements makes me feel dirty.

I should also note that the code above is mostly borrowed from another website but for the life of me, I cannot find that site again to give them the credit they deserve. If you happen to know which/where that site is, please let me know via email or the comments below.

Wellplayed.net
This article has been view 32 times.

Advertisement:

Comments

Jim Perry

Avatar

12/21/2009 5:00:36 AM

Why not just use a KeyValuePair for the items in this case? Why reinvent the wheel? src
Kris

Avatar

12/21/2009 8:20:36 AM

Where were you last week when I was working on this?

Something like that would probably make more sence :Display
Rohan

Avatar

7/20/2010 4:59:05 AM

Jim,

You will need to override the ToString() method, otherwise just using KeyValuePair will display the Key and Value in the Combobox.h="
Rohan

Avatar

7/20/2010 10:02:04 AM

Jim,

You will need to override the ToString() method, otherwise just using KeyValuePair will display the Key and Value in the Combobox.h="


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.