Saturday 8 December 2012

programatically add listbox items in visual C#

programatically add listbox items in visual C#

Create ListBox object add items to it, finally add it Controls list,

     ListBox box = new ListBox();
            box.Width= 100;
            box.Height=100;
            box.Items.Add("Jan");
            box.Items.Add("Feb");
            box.Items.Add("Mar");
            box.Items.Add("Apr");
            box.Items.Add("Jun");
            box.Items.Add("Jul");
            box.Items.Add("Aug");
            box.Items.Add("Sep");
            box.Items.Add("Oct");
            box.Items.Add("Nov");
            box.Items.Add("Dec");

            box.SelectedIndexChanged += box_SelectedIndexChanged;
            this.Controls.Add(box);
        }

        void box_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(((ListBox)sender).SelectedItem.ToString());
        }

No comments:

Post a Comment