ListBox.SelectedIndex : ListBox « System.Windows.Forms « C# / C Sharp by API






ListBox.SelectedIndex

  

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


public class ListBoxDemo : System.Windows.Forms.Form {
    private System.ComponentModel.Container container;
    private System.Windows.Forms.Button buttonAdd;
    private System.Windows.Forms.Button buttonClose;
    private System.Windows.Forms.Button buttonModify;
    private System.Windows.Forms.Button buttonDelete;
    private System.Windows.Forms.Button buttonMoveUp;
    private System.Windows.Forms.Button buttonMoveDown;
    private System.Windows.Forms.ListBox listbox;
    private System.Windows.Forms.TextBox textbox;
    private System.Windows.Forms.Label label;
    private int nSelectedIndex;
    //*********SIZE & LOCATION******************//
    // COMPONENT - BUTTON(s) aligned along X-axis.
    const int BUTTON_LENGTH = 50;
    const int BUTTON_HEIGHT = 20;
    const int FIRSTBUTTON_XPOS = 20;
    const int FIRSTBUTTON_YPOS =220;
    const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)
    const int YSPACING =  0;
    //COMPONENT - MOVE BUTTONS
    const int MBUTTON_LENGTH = 20;
    const int MBUTTON_HEIGHT = 20;
    const int FIRSTMBUTTON_XPOS = 220;
    const int FIRSTMBUTTON_YPOS =70;
    const int SECONDMBUTTON_XPOS = 220;
    const int SECONDMBUTTON_YPOS =100;
    // COMPONENT - LISTBOX
    const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;
    const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;
    const int LISTBOX_XPOS = 50;
    const int LISTBOX_YPOS = 50;
    // COMPONENT - LABEL
    const int LABEL_LENGTH = 50;
    const int LABEL_HEIGHT = 50;
    const int LABEL_XPOS = 20; // align it with first button
    const int LABEL_YPOS = 173;
    // COMPONENT - TEXTBOX
    const int TEXTBOX_LENGTH = 120;
    const int TEXTBOX_HEIGHT = 50;
    const int TEXTBOX_XPOS =  70;
    const int TEXTBOX_YPOS = 170;

    public ListBoxDemo() : base() {
        InitializeComponent();
    }

    private void InitializeComponent() {
        // this
        this.container = new System.ComponentModel.Container();
        this.Text="List Box";
        // buttonAdd
        this.buttonAdd = new System.Windows.Forms.Button();
        buttonAdd.Location = new
            System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);
        buttonAdd.Text = "&Add";
        buttonAdd.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
        buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
        buttonAdd.Enabled = false;
        this.Controls.Add(this.buttonAdd);
        //buttonModify
        this.buttonModify = new System.Windows.Forms.Button();
        buttonModify.Location = new
            System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);
        buttonModify.Text = "&Modify";
        buttonModify.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
        buttonModify.Click += new System.EventHandler(this.buttonModify_Click);
        buttonModify.Enabled = false;
        this.Controls.Add(this.buttonModify);
        //buttonDelete
        this.buttonDelete = new System.Windows.Forms.Button();
        buttonDelete.Location = new
            System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING);
        buttonDelete.Text = "&Delete";
        buttonDelete.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
        buttonDelete.Enabled = false;
        buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
        this.Controls.Add(this.buttonDelete);
        // buttonClose
        this.buttonClose = new System.Windows.Forms.Button();
        buttonClose.Location = new
            System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING);
        buttonClose.Text = "&Close";
        buttonClose.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
        buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
        this.Controls.Add(this.buttonClose);
        // listbox
        this.listbox = new System.Windows.Forms.ListBox();
        listbox.Location = new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS);
        listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT);
        listbox.Click += new 
        
        System.EventHandler(this.listbox_SelectedIndexChanged);
        listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;
        this.Controls.Add(this.listbox);
        // label
        this.label = new System.Windows.Forms.Label();
        label.Location = new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);
        label.Size = new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);
        label.Text = "Enter:";
        this.Controls.Add(this.label);
        // textbox
        this.textbox = new System.Windows.Forms.TextBox();
        textbox.Location = new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS);
        textbox.Click += new System.EventHandler(this.textbox_Click);
        textbox.Size = new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT);
        this.Controls.Add(this.textbox);
        // buttonMoveUp
        this.buttonMoveUp = new System.Windows.Forms.Button();
        buttonMoveUp.Location = new
            System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);
        buttonMoveUp.Text = "<";
        buttonMoveUp.Size = new 
        
        System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
        buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);
        buttonMoveUp.Enabled = false;
        this.Controls.Add(this.buttonMoveUp);
        // buttonMoveDown
        this.buttonMoveDown = new System.Windows.Forms.Button();
        buttonMoveDown.Location = new
            System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);
        buttonMoveDown.Text = ">";
        buttonMoveDown.Size = new 
        
        System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
        buttonMoveDown.Click += new 
        System.EventHandler(this.buttonMoveDown_Click);
        buttonMoveDown.Enabled = false;
        this.Controls.Add(this.buttonMoveDown);
    }
    protected void textbox_Click(Object sender, System.EventArgs e) {
        this.buttonAdd.Enabled = true;
        if (this.listbox.Items.Count>0)
            EnableAllListBoxButtons();
    }
    protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e) {
        nSelectedIndex = this.listbox.SelectedIndex;
        string szSelected = (string)this.listbox.SelectedItem;
        this.textbox.Text = szSelected;
    }
    protected void buttonAdd_Click(Object sender, System.EventArgs e) {
        if (this.textbox.Text !="") {
        this.listbox.Items.Add(this.textbox.Text);
        this.textbox.Text = "";
        EnableAllListBoxButtons();
        }
    }
    protected void buttonModify_Click(Object sender, System.EventArgs e) {
        this.listbox.Items[nSelectedIndex] = this.textbox.Text;
    }
    protected void buttonDelete_Click(Object sender, System.EventArgs e) {
        nSelectedIndex = this.listbox.SelectedIndex;
        this.listbox.Items.Remove(nSelectedIndex);
        System.Console.WriteLine("Remove fn does not work...");

    }
    protected void buttonClose_Click(Object sender, System.EventArgs e) {
        this.Close();
    }
    protected void buttonMoveUp_Click(Object sender, System.EventArgs e) {
        if (this.listbox.SelectedIndex >0)
            this.listbox.SelectedIndex--;
    }
    protected void buttonMoveDown_Click(Object sender, System.EventArgs e) {
        if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)
            this.listbox.SelectedIndex++;

    }
    private void EnableAllListBoxButtons() {
        this.buttonAdd.Enabled = true;
        this.buttonModify.Enabled = true;
        this.buttonDelete.Enabled = true;
        this.buttonMoveUp.Enabled = true;
        this.buttonMoveDown.Enabled = true;
    }

    [STAThread]
    public static void Main(string[] args) {
        Application.Run(new ListBoxDemo());
    }

} 

   
    
  








Related examples in the same category

1.ListBox.BeginUpdate()
2.ListBox.ContextMenu
3.ListBox.DataBindings
4.ListBox.DataSource
5.ListBox.DataSourceChanged
6.ListBox.DisplayMember
7.ListBox.DisplayMemberChanged
8.ListBox.EndUpdate()
9.ListBox.FindString
10.ListBox.Items
11.ListBox.Items.Add
12.ListBox.Items.AddRange
13.ListBox.Items.Clear()
14.ListBox.Items.RemoveAt
15.ListBox.ScrollAlwaysVisible
16.ListBox.SelectedIndexChanged
17.ListBox.SelectedValueChanged
18.ListBox.SelectionMode
19.ListBox.TopIndex
20.ListBox.ValueMemberChanged