| | |
Experiments with a ListBox (C#)
Please support our C# advertiser: Intel Parallel Studio Home
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; } } }
0
•
•
•
•
Just a note: What you get from SharpDevelop is actually the IDE written in C# that uses the compiler from the .NET Framework Version 1.1 Redistributable Package called dotnetfx.exe from Microsoft. The IDE works very much like Visual C# or VB. For this program the Form Builder within the IDE generates most of the center portion of the code.
0
•
•
•
•
Hello, I have tried your code and for some reason is not working properly. When I try to select an item in the listbox it suppose to go to the label.
Is there a possible that I may have something wrong with the compiler I am currently using?
I appreciate for your feedback.
Regards,
Desi Bravo
Is there a possible that I may have something wrong with the compiler I am currently using?
I appreciate for your feedback.
Regards,
Desi Bravo
Last edited by adatapost; 4 Days Ago at 12:12 am. Reason: Email Snipped. Emails, fake signatures, and personal information will be snipped out of offending post.
0
•
•
•
•
After all these years there is nothing wrong with this code. I used Visual Studio C# 2008.
0
•
•
•
•
Hi David,
Believe it or not I copy and pasted the above code to visual C# compiler and is not firing. I am not saying there is something wrong with the code. I am saying that is not working. I am working on similar project and I coded in the selectedindexchanged for when a user select a name in the listbox it fires to the textbox with the correct information. So I was wondering it may be my visual C# express edition that is not working properly or something. Can you help?
Thanks!
Regards,
Desi Bravo
Believe it or not I copy and pasted the above code to visual C# compiler and is not firing. I am not saying there is something wrong with the code. I am saying that is not working. I am working on similar project and I coded in the selectedindexchanged for when a user select a name in the listbox it fires to the textbox with the correct information. So I was wondering it may be my visual C# express edition that is not working properly or something. Can you help?
Thanks!
Regards,
Desi Bravo
Similar Threads
- move items from listbox to another listbox button (C#)
- Display values in listbox from db table regarding selected item in listbox? (PHP)
- To drag and drop multiple listbox items from one listbox control to another (ASP.NET)
- listbox to respond to another listbox (Graphics and Multimedia)
- populating a listbox from another listbox dynamically?? (ASP)
| Thread Tools | Search this Thread |




