Csharp/CSharp Tutorial/GUI Windows Forms/ListBox

Материал из .Net Framework эксперт
Версия от 12:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add ContextMenu to ListBox

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ListBoxContextMenu : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button btnDone;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button btnAdd;
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.ContextMenu contextMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MenuItem menuItem4;
  private System.ruponentModel.Container components = null;
  public ListBoxContextMenu()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  private void InitializeComponent()
  {
    this.btnDone = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.btnAdd = new System.Windows.Forms.Button();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.menuItem3 = new System.Windows.Forms.MenuItem();
    this.menuItem4 = new System.Windows.Forms.MenuItem();
    this.SuspendLayout();
    // 
    // btnDone
    // 
    this.btnDone.Location = new System.Drawing.Point(208, 16);
    this.btnDone.Name = "btnDone";
    this.btnDone.TabIndex = 3;
    this.btnDone.Text = "Done";
    this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(16, 88);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 5;
    this.textBox1.Text = "";
    // 
    // btnAdd
    // 
    this.btnAdd.Location = new System.Drawing.Point(16, 120);
    this.btnAdd.Name = "btnAdd";
    this.btnAdd.TabIndex = 6;
    this.btnAdd.Text = "Add";
    this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    // 
    // listBox1
    // 
    this.listBox1.ContextMenu = this.contextMenu1;
    this.listBox1.Location = new System.Drawing.Point(16, 8);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(120, 69);
    this.listBox1.TabIndex = 7;
    // 
    // contextMenu1
    // 
    this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
       this.menuItem1,
       this.menuItem2,
       this.menuItem3,
       this.menuItem4});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.Text = "Move Down";
    this.menuItem1.Click += new System.EventHandler(this.ctxtMenuClick);
    // 
    // menuItem2
    // 
    this.menuItem2.Index = 1;
    this.menuItem2.Text = "Move up";
    this.menuItem2.Click += new System.EventHandler(this.ctxtMenuClick);
    // 
    // menuItem3
    // 
    this.menuItem3.Index = 2;
    this.menuItem3.Text = "Delete";
    this.menuItem3.Click += new System.EventHandler(this.ctxtMenuClick);
    // 
    // menuItem4
    // 
    this.menuItem4.Index = 3;
    this.menuItem4.Text = "Duplicate";
    this.menuItem4.Click += new System.EventHandler(this.ctxtMenuClick);
    // 
    // ListBoxContextMenu
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.listBox1,
        this.btnAdd,
        this.textBox1,
        this.btnDone});
    this.Name = "ListBoxContextMenu";
    this.ResumeLayout(false);
  }
  #endregion
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxContextMenu());
  }
  private void btnDone_Click(object sender, System.EventArgs e)
  {
    Application.Exit();
  }
  private void btnAdd_Click(object sender, System.EventArgs e)
  {
    listBox1.Items.Add(textBox1.Text);
    textBox1.Text = "";
  }
  private void ctxtMenuClick(object sender, System.EventArgs e)
  {
    if ( listBox1.SelectedIndex != -1 )
    {
      MenuItem mi = (MenuItem) sender;
      MessageBox.Show(mi.Text + " on " + listBox1.SelectedItem,"Context Menu", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }else{
      MessageBox.Show("Please select an item","Context Menu Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
  }
}

Add Items to ListBox

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ListBoxItemAdd : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.ListBox listBox1;
  private System.ruponentModel.Container components = null;
  public ListBoxItemAdd()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    this.button1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.button1.Location = new System.Drawing.Point(16, 16);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(80, 32);
    this.button1.TabIndex = 0;
    this.button1.Text = "Add";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.AutoSize = false;
    this.textBox1.Location = new System.Drawing.Point(112, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(168, 32);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "textBox1";
    // 
    // listBox1
    // 
    this.listBox1.BackColor = System.Drawing.Color.Red;
    this.listBox1.ForeColor = System.Drawing.SystemColors.HighlightText;
    this.listBox1.Location = new System.Drawing.Point(8, 64);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(272, 238);
    this.listBox1.TabIndex = 2;
    // 
    // ListBoxItemAdd
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(328, 318);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.listBox1,
                                    this.textBox1,
                                    this.button1});
    this.Name = "ListBoxItemAdd";
    this.Text = "ListBoxItemAdd";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxItemAdd());
  }
  private void button1_Click(object sender, System.EventArgs e)
  {
    listBox1.Items.Add(textBox1.Text);
  }
}

Add text in the TextBox to the ListBox

using System;
using System.Drawing;
using System.Windows.Forms;

public class ListBoxTextBoxButton : Form
{
   private TextBox data;
   private ListBox results;
   public ListBoxTextBoxButton()
   {
      Size = new Size(400, 380);
      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Enter text:";
      label1.AutoSize = true;
      label1.Location = new Point(10, 10);
      data = new TextBox();
      data.Parent = this;
      data.Size = new Size(200, 2 * Font.Height);
      data.Location = new Point(10, 35);
      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(10, 65);
      results.Size = new Size(350, 20 * Font.Height);
      Button checkit = new Button();
      checkit.Parent = this;
      checkit.Text = "test";
      checkit.Location = new Point(235,32);
      checkit.Size = new Size(7 * Font.Height, 2 * Font.Height);
      checkit.Click += new EventHandler(ButtonOnClick);
   }
   void ButtonOnClick(object obj, EventArgs ea)
   {
      results.Items.Add(data.Text);
      data.Clear();
   }
   public static void Main()
   {
      Application.Run(new ListBoxTextBoxButton());
   }
}

Fills a ListBox with data from Database

using System;
using System.Windows.Forms;
using System.Data.SqlClient;
public class DataReaderTest : Form
{
    private ListBox lstNames;
    private string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
  public DataReaderTest()
  {
    lstNames = new ListBox();
    lstNames.Dock = DockStyle.Fill;
    Controls.Add(lstNames);
    Load += new EventHandler(DataReaderTest_Load);
  }
  public static void Main()
  {
    DataReaderTest t = new DataReaderTest();
    Application.Run(t);
  }
    private void DataReaderTest_Load(object sender, System.EventArgs e)
    {
        string SQL = "SELECT ContactName FROM Customers";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(SQL, con);
        SqlDataReader r = null;
        con.Open();
        r = cmd.ExecuteReader();
        while (r.Read()) {
           lstNames.Items.Add(r["ContactName"]);
        }
        con.Close();
    }
}

ListBox Events: SelectedIndexChanged, SelectedValueChanged, DataSourceChanged, DisplayMemberChanged, ValueMemberChanged

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class Employee
  public string ID;
  public string Name ;
  public  Employee(string strName, string strID)
  {
     this.ID = strID;
     this.Name = strName;
  }
  public override string ToString()
  {
     return this.ID + " : " + this.Name;
  }

public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.RadioButton rbAuthors;
  private System.Windows.Forms.RadioButton rbEmployees;
  private System.Windows.Forms.ListBox lb;
  private DataTable dataTable;
    private ArrayList Employees = new ArrayList();
  private System.ruponentModel.Container components = null;
  public Form1()
  {
    InitializeComponent();
    lb.Items.Add("A");
    lb.Items.Add("B");
    lb.Items.Add("C");
    lb.Items.Add("D");
    lb.Items.Add("E");
    lb.SelectedIndex = 0;
    // populate the arraylist for later use.
        Employees.Add(new Employee("A", "1"));
        Employees.Add(new Employee("B", "2")); 
        Employees.Add(new Employee("C", "3"));
        Employees.Add(new Employee("D", "4"));
        Employees.Add(new Employee("E", "5"));
        Employees.Add(new Employee("F", "6"));
        Employees.Add(new Employee("G", "7"));
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.lb = new System.Windows.Forms.ListBox();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.rbEmployees = new System.Windows.Forms.RadioButton();
    this.rbAuthors = new System.Windows.Forms.RadioButton();
    this.groupBox1.SuspendLayout();
    this.SuspendLayout();
    // 
    // lb
    // 
    this.lb.Location = new System.Drawing.Point(16, 8);
    this.lb.Name = "lb";
    this.lb.Size = new System.Drawing.Size(232, 212);
    this.lb.TabIndex = 0;
    this.lb.DisplayMemberChanged += new System.EventHandler(this.lb_DisplayMemberChanged);
    this.lb.ValueMemberChanged += new System.EventHandler(this.lb_ValueMemberChanged);
    this.lb.DataSourceChanged += new System.EventHandler(this.lb_DataSourceChanged);
    this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged);
    this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged);
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                    this.rbEmployees,
                                                    this.rbAuthors});
    this.groupBox1.Location = new System.Drawing.Point(16, 240);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "DataSource";
    // 
    // rbEmployees
    // 
    this.rbEmployees.Location = new System.Drawing.Point(24, 56);
    this.rbEmployees.Name = "rbEmployees";
    this.rbEmployees.TabIndex = 1;
    this.rbEmployees.Text = "Employee\"s";
    this.rbEmployees.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
    // 
    // rbAuthors
    // 
    this.rbAuthors.Checked = true;
    this.rbAuthors.Location = new System.Drawing.Point(24, 32);
    this.rbAuthors.Name = "rbAuthors";
    this.rbAuthors.TabIndex = 0;
    this.rbAuthors.TabStop = true;
    this.rbAuthors.Text = "Authors";
    this.rbAuthors.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(264, 389);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.groupBox1,
                                             this.lb});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new Form1());
  }
  private void rb_CheckedChanged(object sender, System.EventArgs e)
  {
    lb.DataSource = Employees;
    lb.DisplayMember = "Name";
    lb.ValueMember = "ID";
  }
  private void lb_SelectedIndexChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.SelectedIndex.ToString()+ "\n" + lb.GetItemText(lb.SelectedItem),"lb_SelectedIndexChanged");    
  }
  private void lb_SelectedValueChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.GetItemText(lb.SelectedItem),"lb_SelectedValueChanged");    
  }
  private void lb_DataSourceChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.DataSource.ToString(), "lb_DataSourceChanged");    
  }
  private void lb_DisplayMemberChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.DisplayMember.ToString(), "lb_DisplayMemberChanged");    
  }
  private void lb_ValueMemberChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.ValueMember.ToString(), "lb_ValueMemberChanged");    
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
    this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged);
    this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged);
  }
}

ListBox Items Add

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxItemAdd : Form{
  ListBox lb;
  public ListBoxItemAdd()
  {
    Size = new Size(300,400);
    lb = new ListBox();
    lb.Parent = this;
    lb.Location = new Point(10,10);
    lb.Size = new Size(ClientSize.Width - 20, Height - 200);
    lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    lb.BorderStyle = BorderStyle.Fixed3D;

    lb.BeginUpdate();
    for (int i = 0; i < 5; i++)
    {
       lb.Items.Add(i);
    }      
    lb.Items.Add("12345");
    lb.Items.Add("67890");
    lb.EndUpdate();
  } 
  static void Main() 
  {
    Application.Run(new ListBoxItemAdd());
  }
}

ListBox Items Add a Range

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxItemAddRange : Form{
  ListBox lb;
  public ListBoxItemAddRange()
  {
    Size = new Size(300,400);
    lb = new ListBox();
    lb.Parent = this;
    lb.Location = new Point(10,10);
    lb.Size = new Size(ClientSize.Width - 20, Height - 200);
    lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    lb.BorderStyle = BorderStyle.Fixed3D;

    lb.BeginUpdate();
    string[] arNames = new string[5];
    for(int i = 0;i<5;i++){
       arNames[i] = "I";    
    }
    lb.Items.AddRange(arNames);
    lb.Items.Add("12345");
    lb.Items.Add("67890");
    lb.EndUpdate();
  } 
  static void Main() 
  {
    Application.Run(new ListBoxItemAddRange());
  }
}

ListBox Selection Mode

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxSelectionMode : Form
{
  ListBox lb;
  RadioButton rdoMultiExtended;
  RadioButton rdoMultiSimple;
  RadioButton rdoMultiOne;
  TextBox txtTop;
  Button btnTop;
  public ListBoxSelectionMode()
  {
    int xSize, ySize;
    Size = new Size(300,400);
    lb = new ListBox();
    lb.Parent = this;
    lb.Location = new Point(10,10);
    lb.Size = new Size(ClientSize.Width - 20, Height - 200);
    lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    lb.BorderStyle = BorderStyle.Fixed3D;
    lb.MultiColumn = true;
    lb.ScrollAlwaysVisible = true;
    GroupBox grpMulti = new GroupBox();
    grpMulti.Parent = this;
    grpMulti.Text = "MultiSelect";
    grpMulti.Location = new Point(lb.Left, lb.Bottom + 25);
    grpMulti.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    rdoMultiOne = new RadioButton();
    rdoMultiOne.Parent = grpMulti;
    rdoMultiOne.Text = "One";
    rdoMultiOne.Tag = SelectionMode.One;
    rdoMultiOne.Checked = true;
    rdoMultiOne.Location = new Point(10,15);
    rdoMultiOne.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    rdoMultiSimple = new RadioButton();
    rdoMultiSimple.Parent = grpMulti;
    rdoMultiSimple.Text = "Multi-Simple";
    rdoMultiSimple.Tag = SelectionMode.MultiSimple;
    rdoMultiSimple.Location = new Point(10, rdoMultiOne.Bottom);
    rdoMultiSimple.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    rdoMultiExtended = new RadioButton();
    rdoMultiExtended.Parent = grpMulti;
    rdoMultiExtended.Text = "Multi-Extended";
    rdoMultiExtended.Tag = SelectionMode.MultiExtended;
    rdoMultiExtended.Location = new Point(10, rdoMultiSimple.Bottom);
    rdoMultiExtended.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    xSize = (int)(Font.Height * .75) * rdoMultiExtended.Text.Length;
    ySize = ((int)rdoMultiOne.Height * 3) + 20;
    grpMulti.Size = new Size(xSize, ySize);
    Panel pnlTop = new Panel();
    pnlTop.Parent = this;
    pnlTop.Location = new Point(lb.Left, grpMulti.Bottom + 10);
    pnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    Label lblTop = new Label();
    lblTop.Parent = pnlTop;
    lblTop.Text = "TopIndex: ";
    xSize = ((int)(Font.Height * .5) * lblTop.Text.Length);
    lblTop.Size = new Size(xSize, Font.Height + 10);
    txtTop = new TextBox();
    txtTop.Parent = pnlTop;
    txtTop.Location = new Point(lblTop.Right, lblTop.Top);
    txtTop.Text = lb.TopIndex.ToString();
    txtTop.Size = new Size((int)(Font.Height * .75) * 3, 
                Font.Height + 10);
    btnTop = new Button();
    btnTop.Parent = pnlTop;
    btnTop.Text = "Update";
    btnTop.Location = new Point(txtTop.Right + 10, txtTop.Top);
    btnTop.Click += new System.EventHandler(btnTop_Click);

    lb.Items.Add("12345");
      lb.Items.Add("67890");      
      lb.Items.Add("7890");      
      lb.Items.Add("890");            
  } 
  static void Main() 
  {
    Application.Run(new ListBoxSelectionMode());
  }
  private void rdoMulti_CheckedChanged(object sender, EventArgs e)
  {
    RadioButton rdo = (RadioButton)sender;
    lb.SelectionMode = (SelectionMode)rdo.Tag;
  }
  private void btnTop_Click(object sender, EventArgs e)
  {
    txtTop.Text = lb.TopIndex.ToString();
  }
}

Set the TopIndex property of the ListBox to ensure the most recently added items are visible

using System;
using System.Windows.Forms;
public class ListBoxItemVisibleFormDemo{
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new ListBoxItemVisibleForm());
    }
}
public partial class ListBoxItemVisibleForm : Form
{
    private int counter = 0;
    
    public ListBoxItemVisibleForm()
    {
        InitializeComponent();
    }
    private void cmdTest_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 200; i++)
        {
            counter++;
            listBox1.Items.Add("Item " + counter.ToString());
        }

        listBox1.TopIndex = listBox1.Items.Count - 1;
        listBox1.SelectedIndex = listBox1.Items.Count - 1;
    }
}
partial class ListBoxItemVisibleForm
{
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button cmdTest;
    private System.ruponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.cmdTest = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(8, 8);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(248, 160);
        this.listBox1.TabIndex = 0;
        this.cmdTest.Location = new System.Drawing.Point(12, 184);
        this.cmdTest.Name = "cmdTest";
        this.cmdTest.Size = new System.Drawing.Size(132, 28);
        this.cmdTest.TabIndex = 1;
        this.cmdTest.Text = "Test Scroll";
        this.cmdTest.Click += new System.EventHandler(this.cmdTest_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 246);
        this.Controls.Add(this.cmdTest);
        this.Controls.Add(this.listBox1);
        this.Name = "ListBoxItemVisibleForm";
        this.Text = "ListBoxItemVisibleForm";
        this.ResumeLayout(false);
    }
}

Use RadioButton to control ListBox selection mode

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxSelectionMode : Form
{
  ListBox lb;
  RadioButton rdoMultiExtended;
  RadioButton rdoMultiSimple;
  RadioButton rdoMultiOne;
  TextBox txtTop;
  Button btnTop;
  public ListBoxSelectionMode()
  {
    int xSize, ySize;
    Size = new Size(300,400);
    lb = new ListBox();
    lb.Parent = this;
    lb.Location = new Point(10,10);
    lb.Size = new Size(ClientSize.Width - 20, Height - 200);
    lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    lb.BorderStyle = BorderStyle.Fixed3D;
    lb.MultiColumn = true;
    lb.ScrollAlwaysVisible = true;
    GroupBox grpMulti = new GroupBox();
    grpMulti.Parent = this;
    grpMulti.Text = "MultiSelect";
    grpMulti.Location = new Point(lb.Left, lb.Bottom + 25);
    grpMulti.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    rdoMultiOne = new RadioButton();
    rdoMultiOne.Parent = grpMulti;
    rdoMultiOne.Text = "One";
    rdoMultiOne.Tag = SelectionMode.One;
    rdoMultiOne.Checked = true;
    rdoMultiOne.Location = new Point(10,15);
    rdoMultiOne.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    rdoMultiSimple = new RadioButton();
    rdoMultiSimple.Parent = grpMulti;
    rdoMultiSimple.Text = "Multi-Simple";
    rdoMultiSimple.Tag = SelectionMode.MultiSimple;
    rdoMultiSimple.Location = new Point(10, rdoMultiOne.Bottom);
    rdoMultiSimple.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    rdoMultiExtended = new RadioButton();
    rdoMultiExtended.Parent = grpMulti;
    rdoMultiExtended.Text = "Multi-Extended";
    rdoMultiExtended.Tag = SelectionMode.MultiExtended;
    rdoMultiExtended.Location = new Point(10, rdoMultiSimple.Bottom);
    rdoMultiExtended.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
    xSize = (int)(Font.Height * .75) * rdoMultiExtended.Text.Length;
    ySize = ((int)rdoMultiOne.Height * 3) + 20;
    grpMulti.Size = new Size(xSize, ySize);
    Panel pnlTop = new Panel();
    pnlTop.Parent = this;
    pnlTop.Location = new Point(lb.Left, grpMulti.Bottom + 10);
    pnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    Label lblTop = new Label();
    lblTop.Parent = pnlTop;
    lblTop.Text = "TopIndex: ";
    xSize = ((int)(Font.Height * .5) * lblTop.Text.Length);
    lblTop.Size = new Size(xSize, Font.Height + 10);
    txtTop = new TextBox();
    txtTop.Parent = pnlTop;
    txtTop.Location = new Point(lblTop.Right, lblTop.Top);
    txtTop.Text = lb.TopIndex.ToString();
    txtTop.Size = new Size((int)(Font.Height * .75) * 3, 
                Font.Height + 10);
    btnTop = new Button();
    btnTop.Parent = pnlTop;
    btnTop.Text = "Update";
    btnTop.Location = new Point(txtTop.Right + 10, txtTop.Top);
    btnTop.Click += new System.EventHandler(btnTop_Click);

    lb.Items.Add("12345");
      lb.Items.Add("67890");      
      lb.Items.Add("7890");      
      lb.Items.Add("890");            
  } 
  static void Main() 
  {
    Application.Run(new ListBoxSelectionMode());
  }
  private void rdoMulti_CheckedChanged(object sender, EventArgs e)
  {
    RadioButton rdo = (RadioButton)sender;
    lb.SelectionMode = (SelectionMode)rdo.Tag;
  }
  private void btnTop_Click(object sender, EventArgs e)
  {
    txtTop.Text = lb.TopIndex.ToString();
  }
}