C# Windows Forms ListBox Examples

Let us look at a simple C# Windows Forms ListBox example.

Example 1: ListBox

This example will comprise the following files:

  • ListBox.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). ListBox.cs

Create a file named ListBox.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "ListBox example";

      StartPosition = FormStartPosition.Manual;
      Location = new System.Drawing.Point(200, 200);
      ClientSize = new System.Drawing.Size(360, 240);

      listBoxActionsLeft.Parent = this;
      listBoxActionsLeft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
      listBoxActionsLeft.Bounds = new System.Drawing.Rectangle(20, 20, 150, 200);
      listBoxActionsLeft.DoubleClick += delegate(object sender, EventArgs e) {
        if (listBoxActionsLeft.SelectedIndex != -1) {
          listBoxActionsRight.Items.Add(listBoxActionsLeft.SelectedItem);
          listBoxActionsRight.SelectedIndex = listBoxActionsRight.Items.Count - 1;
          listBoxActionsLeft.Items.Remove(listBoxActionsLeft.SelectedItem);
        }
      };

      listBoxActionsRight.Parent = this;
      listBoxActionsRight.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
      listBoxActionsRight.Bounds = new System.Drawing.Rectangle(190, 20, 150, 200);
      listBoxActionsRight.DoubleClick += delegate(object sender, EventArgs e) {
        if (listBoxActionsRight.SelectedIndex != -1) {
          listBoxActionsLeft.Items.Add(listBoxActionsRight.SelectedItem);
          listBoxActionsLeft.SelectedIndex = listBoxActionsLeft.Items.Count - 1;
          listBoxActionsRight.Items.Remove(listBoxActionsRight.SelectedItem);
        }
      };

      listBoxActionsLeft.Items.AddRange(new string[] { "draw", "cut", "paste", "delete", "open", "close", "remove", "edit", "find", "increment", "decrement", "write", "read", "post", "build", "make", "release", "create", "choose", "erase"});
    }

    private ListBox listBoxActionsLeft = new ListBox();
    private ListBox listBoxActionsRight = new ListBox();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts