Windows Form ListBox


I’m really enjoying programming these days, more than writing blogs. I decide to wrote a blog about programming.

For this project I’m working on, I need to create a windows form that has contains a ListBox control. We’ve seen ListBox a lot, from Windows applications, or from the Web. It’s a long rectangle box with a list of items. Usually an item is selected by the user from the list. At the back end, the value of the selected item will be picked up by the application for processing.

Writing a ListBox in ASP.NET for the web is simple. You can add a ListItem object to the ListBox and a ListItem has a Text and a Value property, and a constructro ListItem(string Text, string Value). So you can write the following code in the aspx page

        Pick the days you don’t want to work.<br />
        <asp:ListBox ID=”lstWeekDay” runat=”server” Rows=7 />

and the following in the aspx.cs file

        ListItem mon = new ListItem(“Monday”, “1”);
        ListItem tue = new ListItem(“Tuesday”, “2”);
        ListItem wed = new ListItem(“Wednesday”, “3”);
        ListItem thu = new ListItem(“Thursday”, “4”);
        ListItem fri = new ListItem(“Friday”, “5”);
        ListItem sat = new ListItem(“Saturday”, “6”);
        ListItem sun = new ListItem(“Sunday”, “0”);
        lstWeekDay.Items.Add(mon);
        lstWeekDay.Items.Add(tue);
        lstWeekDay.Items.Add(wed);
        lstWeekDay.Items.Add(thu);
        lstWeekDay.Items.Add(fri);
        lstWeekDay.Items.Add(sat);
        lstWeekDay.Items.Add(sun);

I was surprised to find that in Windows Form, there is no ListItem object, You can do ListBox.Items.Add(Object item), but the ListBox can only display item.ToString() as text, and there is no value for the item.

I spent a lot of time searching documentations before I decide that it’s impossible to bind an item with both text and value to the Windows ListBox. My eventual solution is to create a data table and bind it to the DataSource property of the ListBox. This method is also available to Web ListBox. I hope I can save the time of someone who’s more familiar with web developement and wanted to do the same thing to a Windows ListBox.

            DataTable dt = new DataTable();
            dt.Columns.Add(“Text”);
            dt.Columns.Add(“Value”);

            DataRow dr = dt.NewRow();
            dr[“Text”] = “Monday”;
            dr[“Value”] = 1;
            dt.Rows.Add(dr);

           ……

            DataRow dr = dt.NewRow();
            dr[“Text”] = “Sunday”;
            dr[“Value”] = 0;
            dt.Rows.Add(dr);

            lstWeekDay.DisplayMember = “Text”;
            lstWeekDay.ValueMember = “Value”;
            lstWeekDay.DataSource = dt;

——————- A Perfection Seeking Divider ——————

I wrote this blog because I just wanted to write something so I can go back to programming, Now I’m so glad that I wrote it. After I hit publish button, I came up with this new ideal: If there is no ListItem for Windows ListBox, why can’t I wrote my own? I tried this:

        public class ListItem
        {
            private string _text;
            private string _value;

            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }

            public string Value
            {
                get { return _value; }
                set { _value = value; }
            }

            public ListItem()
            {
            }

            public ListItem(string text, string value)
            {
                _text = text;
                _value = value;
            }
        }

and then I used exactly the same code I used for the web page:

            ListItem mon = new ListItem(“Monday”, “1”);
            ListItem tue = new ListItem(“Tuesday”, “2”);
            ListItem wed = new ListItem(“Wednesday”, “3”);
            ListItem thu = new ListItem(“Thursday”, “4”);
            ListItem fri = new ListItem(“Friday”, “5”);
            ListItem sat = new ListItem(“Saturday”, “6”);
            ListItem sun = new ListItem(“Sunday”, “0”);
            lstWeekDay.Items.Add(mon);
            lstWeekDay.Items.Add(tue);
            lstWeekDay.Items.Add(wed);
            lstWeekDay.Items.Add(thu);
            lstWeekDay.Items.Add(fri);
            lstWeekDay.Items.Add(sat);
            lstWeekDay.Items.Add(sun);

with the addition of the following two lines of code

            lstWeekDay.DisplayMember = “text”;
            lstWeekDay.ValueMember = “value”;

To access the value of the ListItem, there’s a little extra work, you have to do

            Object obj = lstWeekDay.SelectedItem;
            MessageBox.Show(((ListItem)obj).Value);

As a low carbon emission enthursiast, I’m so glad that I can eliminate the use of the heavy weighted DataTable object. Isn’t that great? I’m going to add a new blog category – low carbon emission programming.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s