Re: Data in ComboBoxes

From: Tom Clement (TomElideThis_at_Apptero.com)
Date: 08/17/04


Date: Mon, 16 Aug 2004 17:28:02 -0700

The way I've handled this sort of thing is to create a Class (or Struct)
with a ToString() method that gives you the text you want. If you store
objects of this class in the ComboBox, it will display the text you want,
but when you get the selected item, you can cast it to your Class and access
member variables containing the underlying data. It's a cool feature of
DotNet that the item type is object and the control uses the ToString()
method to figure out how to display it.

Generally speaking, you probably want to write some code that determines how
to display a particular value (number of minutes). The only place to put
this is inside of a class, so this works out fine. Off the top of my head,
you could use something like this:

public class MyTimeSpan
{
    private int m_iMinutes;
    public MyTimeSpan(int minutes)
   {
       m_iMinutes = Math.Max(minutes, 0);
   }
   public string ToString()
   {
        string sTime = "";
        int iMinutes = m_iMinutes;

        int iWeeks = iMinutes \ 10080;
        sTime += GetFormattedString(iWeeks, "week", "weeks");
        iMinutes -= iWeeks * 10080;

        int iDays = iMinutes \ 1440;
        sTime += GetFormattedString(iDays, "day", "days");
        iMinutes -= iDays * 1440;

        int iHours = iMinutes \ 60;
        sTime += GetFormattedString(iHours, "hour", "hours");
        iMinutes -= iHours * 60;

        sTime += GetFormatttedString(iMinutes, "minute", "minutes");
        return sTime;
   }
   private string GetFormattedString(int count, string singular, string
plural)
   {
        if( iCount <= 0 )
            return "";
        return string.Format("{0} {1}", iCount, ( iCount == 1 )? singular :
plural);
   }
}

"David Meier" <DavidMeier@discussions.microsoft.com> wrote in message
news:7783CBF4-B09C-49AD-B945-9BE3B1A11E8D@microsoft.com...
> How can data be stored in comboboxes similar to html pulldowns? I know I
> can
> access the data of the combobox either by the items text or by its index.
> However, I would like to have a pulldown representing something like:
>
> Text: 1 minute; Value: 1
> Text: 5 minutes; Value: 5
> Text: 1 hour; Value: 60
>
> Thanks. Dave.



Relevant Pages

  • Re: Arraylist question
    ... ToString virtual method in your PERSON class to return: ... public override string ToString() ... Now you can add the instances of person directly to the combobox which will ... > public int CompareTo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Combobox display und value member
    ... Die Werte für Display und Valuemember sind Eigenschaftsnamen, welche jedes Object in der DataSource-Auflistung anbieten muss. ... Dadurch dass deine eingefügten Werte strings sind, kann deine Data oder Value Eigenschaft nur eine Property des String datentyps sein. ... Deine ComboBox weiss nichts von deinem Reader, damit kannst du auch keine Felder des Readers als Member angeben. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: newbie help for winform combo box needed
    ... You can create your own ListItem class or just put "30" and override the drawing algorithm to display the item text + " days". ... public string value1; ... The ComboBox will call ToString to determine what to display. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Combo Box
    ... I usually use a helper class that overrides the ToString() method. ... that overrides the ToString() method with a "useful" string will work. ... public ComboBoxItem(string display, object data) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: help - populating a combox box from a text file and updating a listbox automatically
    ... The item in the ComboBox have some text that should be displayed in the TextBox. ... This text is taken from another file and can be shared among several items in the ComboBox. ... you could also put it in the same file as the ComboBox values and store the text along with the display value. ... public string Display ...
    (microsoft.public.dotnet.languages.csharp)