Experiments with a ListBox (C#)
Please support our C# advertiser: DiscountASP.NET – 3 Months Free on C# Web Hosting
I am afraid, I am starting to like C#, despite the somewhat bloated .Net Framework requirements. Mister Bill's Microsoft is very supportive though. The language has a nice flow compared to GUI programming in C++. Here we are looking at a standard ListBox, add some items, sort them and select them.
/* * Created with SharpDevelop free C# system from * http://www.icsharpcode.net/opensource/sd/ * User: vegaseat * * Create a ListBox, then add, sort, select items * A Windows Application */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ListBox1 { // Summary description for Form1 // so we got a form (window) with a label, 2 buttons and a listbox ... public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Button sortLBbutton; private System.Windows.Forms.Button LoadLBbutton; private System.Windows.Forms.ListBox listBox1; // Required designer variable private System.ComponentModel.Container components = null; // time to build the form and it's components ... public Form1() { InitializeComponent(); } // clean up any resources being used ... protected override void Dispose( bool disposing ) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } // all the components in detail ... private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.listBox1 = new System.Windows.Forms.ListBox(); this.LoadLBbutton = new System.Windows.Forms.Button(); this.sortLBbutton = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // listBox1 // this.listBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192))); this.listBox1.ItemHeight = 16; this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(168, 244); this.listBox1.TabIndex = 0; this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1SelectedIndexChanged); // // LoadLBbutton // this.LoadLBbutton.Location = new System.Drawing.Point(200, 16); this.LoadLBbutton.Name = "LoadLBbutton"; this.LoadLBbutton.Size = new System.Drawing.Size(128, 23); this.LoadLBbutton.TabIndex = 1; this.LoadLBbutton.Text = "Load ListBox"; this.LoadLBbutton.Click += new System.EventHandler(this.LoadLBbutton_Click); // // sortLBbutton // this.sortLBbutton.Location = new System.Drawing.Point(200, 56); this.sortLBbutton.Name = "sortLBbutton"; this.sortLBbutton.Size = new System.Drawing.Size(128, 23); this.sortLBbutton.TabIndex = 3; this.sortLBbutton.Text = "Sort the ListBox"; this.sortLBbutton.Click += new System.EventHandler(this.sortLBbuttonClick); // // label1 // this.label1.Location = new System.Drawing.Point(8, 264); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(160, 24); this.label1.TabIndex = 2; this.label1.Text = "---"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.BackColor = System.Drawing.Color.WhiteSmoke; this.ClientSize = new System.Drawing.Size(344, 296); this.Controls.Add(this.sortLBbutton); this.Controls.Add(this.label1); this.Controls.Add(this.LoadLBbutton); this.Controls.Add(this.listBox1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.Text = "Load a ListBox and sort ..."; this.ResumeLayout(false); } // // The main entry point for the application // static void Main() { Application.Run(new Form1()); } // // the events, or let's do something with the components ... // // load some data into the ListBox private void LoadLBbutton_Click(object sender, System.EventArgs e) { listBox1.Items.Add("Helmut"); listBox1.Items.Add("Helga"); listBox1.Items.Add("Andreas"); listBox1.Items.Add("Volger"); listBox1.Items.Add("Kurt"); listBox1.Items.Add("Erich"); listBox1.Items.Add("Bjorn"); listBox1.Items.Add("Lena"); listBox1.Items.Add("Kristina"); label1.Text = "Select an item ..."; } // selected ListBox item is transferred to label1 void ListBox1SelectedIndexChanged(object sender, System.EventArgs e) { label1.Text = listBox1.SelectedItems[0].ToString(); } // sort the items of the ListBox void sortLBbuttonClick(object sender, System.EventArgs e) { listBox1.Sorted = true; } } }
vegaseat | DaniWeb's Hypocrite | Jan 1st, 2005



