Experiments with a ListBox (C#)

vegaseat 0 Tallied Votes 704 Views Share

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()
    // clean up any resources being used ...
    protected override void Dispose( bool disposing )
      if (disposing)
        if (components != null) 

    // 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();
      // 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.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.Name = "Form1";
      this.Text = "Load a ListBox and sort ...";
    // 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)
      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 1,735 DaniWeb's Hypocrite Team Colleague

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.

bravo659 0 Light Poster

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.


Desi Bravo

ddanbe 2,724 Professional Procrastinator Featured Poster

After all these years there is nothing wrong with this code. I used Visual Studio C# 2008.

bravo659 0 Light Poster

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?


Desi Bravo

baeltazor 0 Newbie Poster

I would just like to point something out, I hope you don't take any offense. :) There is a quicker way to add items to the listbox. :)

String[] items = { "item1", "second item", "another item", "and another..." };

private void LoadLBbutton_Click(object sender, System.EventArgs e)
foreach (String item in items)

label1.Text = "Select an item...";
Diamonddrake 397 Master Poster

baeltazor, I see you are accustom to the .net baby sitter as much as I am. :). C and C++ people don't tend to use foreach loops often when the migrate to .net because its only possible in programming languages that use virtual machines like .net's CLR. But it actually adds an overhead, slowing down the execution of the code. Plus your version of the code requires creating an addition object, that takes time to initialize, takes more system resource, and twice the memory.

Not that its a bad way to do it. computers these days are plenty fast and for each loops and they are very fun and easy to use.

But even though its less typing, technically the OP's way of doing it was "faster" by terms of the program execution.

vinay53 0 Newbie Poster
anucom 0 Newbie Poster

Thank U very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.