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

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » ListBox 
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[nSelectedIndexthis.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.