当前位置: 首页>>代码示例>>C#>>正文


C# ListBox.SelectedIndex属性代码示例

本文整理汇总了C#中System.Windows.Forms.ListBox.SelectedIndex属性的典型用法代码示例。如果您正苦于以下问题:C# ListBox.SelectedIndex属性的具体用法?C# ListBox.SelectedIndex怎么用?C# ListBox.SelectedIndex使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Windows.Forms.ListBox的用法示例。


在下文中一共展示了ListBox.SelectedIndex属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RemoveTopItems

private void RemoveTopItems()
{
   // Determine if the currently selected item in the ListBox 
   // is the item displayed at the top in the ListBox.
   if (listBox1.TopIndex != listBox1.SelectedIndex)
      // Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex;

   // Remove all items before the top item in the ListBox.
   for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
   {
      listBox1.Items.RemoveAt(x);
   }

   // Clear all selections in the ListBox.
   listBox1.ClearSelected();
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:17,代码来源:ListBox.SelectedIndex

示例2: ListBoxDemo

//引入命名空间
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());
    }

}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:192,代码来源:ListBox.SelectedIndex


注:本文中的System.Windows.Forms.ListBox.SelectedIndex属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。