Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2006

Tip: Looking for answers? Try searching our database.

novice help for combo box needed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David C - 09 Aug 2006 07:21 GMT
I am trying to emulate with ComboBox, what I do with a Web DropDownList.

dropdownList.Items.Add(new ListItem("30", "30 days"));
dropdownList.Items.Add(new ListItem("60", "60 days"));

As you can see, 30 and 60 are values and "30 days" and "60 days" for displaying.  Not populating from the database here.  I just want those two items to be in the combo box.  Very simple.

Cannot for the life of me figure out how to do this.  ComboBox.Items is an ObjectCollection, and I have no idea how to emulate the above.  

So I decided to create table like this and populate.

DataTable table = new Table();

table.Columns.Add("Value");
table.Columns.Add("Display");

//and create rows with what I want to populate
DataRow row = table.NewRow();
row["Value"] = 30;
row["Display"] = "30 days";
table.Rows.Add(row);

cmbBox.ValueMember = "Value";
cmbBox.DisplayMember = "Display";
cmbBox.DataSource = table;

Now that seems like a lot of code for something very simple.

And the strangest thing is, when cmbBox.DataSource = table executes, the SelectedIndexChanged event fires!  

PS:  I am still using .NET 1.x.  
Morten Wennevik - 09 Aug 2006 08:08 GMT
Hi David,

You can create your own ListItem class or just put "30" and override the  
drawing algorithm to display the item text + " days".

The first way can be done like this

        ComboBox b = new ComboBox();
        b.Items.Add(new ComboItem("30", "30 days"));
        b.Items.Add(new ComboItem("60", "60 days"));
        this.Controls.Add(b);

        class ComboItem
        {
            public string value1;
            public string value2;
            public ComboItem(string value1, string value2)
            {
                this.value1 = value1;
                this.value2 = value2;
            }
            public override string ToString()
            {
                return value2;
            }
        }

The ComboBox will call ToString to determine what to display.

Another way to do the above can give you more flexibility in what to  
display

        ComboBox b = new ComboBox();
        b.Items.Add(new ComboItem("30", "30 days"));
        b.Items.Add(new ComboItem("60", "60 days"));
        b.DisplayMember = "Display";
        this.Controls.Add(b);

        class ComboItem
        {
            private string value;
            private string display;
            public string Value
            {
                get { return value; }
            }
            public string Display
            {
                get { return display; }
            }
            public ComboItem(string value1, string value2)
            {
                this.value = value1;
                this.display = value2;
            }
        }

Note the DisplayMember usage.  The ComboBox can display any string  
property by setting the DisplayMember to the name of the property.

> I am trying to emulate with ComboBox, what I do with a Web DropDownList.
>
[quoted text clipped - 31 lines]
>
> PS:  I am still using .NET 1.x.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Stoitcho Goutsev (100) - 09 Aug 2006 21:25 GMT
Alternatively, if there is a type already and the ToString doesn't return
desired text, one can use DisplayMember and ValueMemeber properties.

If you use .NET 2.0 you can use the FormatString property declared on the
level of the ListControl (base class for the combobox)

combobox.Items.Add(10);
combobox.Items.Add(15);
combobox.Items.Add(12);
combobox.FormatString = "# 'Days'";

the combobox items will display :
10 Days
15 Days
12 Days

but the values will be the integers 10, 12 ,12

Signature

HTH
Stoitcho Goutsev (100)

Hi David,

You can create your own ListItem class or just put "30" and override the
drawing algorithm to display the item text + " days".

The first way can be done like this

        ComboBox b = new ComboBox();
        b.Items.Add(new ComboItem("30", "30 days"));
        b.Items.Add(new ComboItem("60", "60 days"));
        this.Controls.Add(b);

        class ComboItem
        {
            public string value1;
            public string value2;
            public ComboItem(string value1, string value2)
            {
                this.value1 = value1;
                this.value2 = value2;
            }
            public override string ToString()
            {
                return value2;
            }
        }

The ComboBox will call ToString to determine what to display.

Another way to do the above can give you more flexibility in what to
display

        ComboBox b = new ComboBox();
        b.Items.Add(new ComboItem("30", "30 days"));
        b.Items.Add(new ComboItem("60", "60 days"));
        b.DisplayMember = "Display";
        this.Controls.Add(b);

        class ComboItem
        {
            private string value;
            private string display;
            public string Value
            {
                get { return value; }
            }
            public string Display
            {
                get { return display; }
            }
            public ComboItem(string value1, string value2)
            {
                this.value = value1;
                this.display = value2;
            }
        }

Note the DisplayMember usage.  The ComboBox can display any string
property by setting the DisplayMember to the name of the property.

> I am trying to emulate with ComboBox, what I do with a Web DropDownList.
>
[quoted text clipped - 31 lines]
>
> PS:  I am still using .NET 1.x.

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.