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


C# ListBox.SelectedIndexChanged事件代码示例

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


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

示例1: listBox1_SelectedIndexChanged

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox.
   string curItem = listBox1.SelectedItem.ToString();

   // Find the string in ListBox2.
   int index = listBox2.FindString(curItem);
   // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
   if(index == -1)
      MessageBox.Show("Item is not available in ListBox2");
   else
      listBox2.SetSelected(index,true);
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:13,代码来源:ListBox.SelectedIndexChanged

示例2: Form1

//引入命名空间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
  private System.Windows.Forms.ListBox lstCustomers;
  public Form1() {
        InitializeComponent();
    lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10)));
    lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100)));
    lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500)));

  }
  private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
  {
    Customer cust = (Customer)lstCustomers.SelectedItem;
    MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString());
  }
  private void InitializeComponent()
  {
    this.lstCustomers = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // lstCustomers
    // 
    this.lstCustomers.FormattingEnabled = true;
    this.lstCustomers.Location = new System.Drawing.Point(12, 12);
    this.lstCustomers.Name = "lstCustomers";
    this.lstCustomers.Size = new System.Drawing.Size(120, 95);
    this.lstCustomers.TabIndex = 0;
    this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(308, 230);
    this.Controls.Add(this.lstCustomers);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

  }


  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}
public class Customer
{
  public string FirstName;
  public string LastName;
  public DateTime BirthDate;

  public Customer() { }

  public Customer(string firstName, string lastName, DateTime birthDate)
  {
    FirstName = firstName;
    LastName = lastName;
    BirthDate = birthDate;
  }

  public override string ToString()
  {
    return FirstName + " " + LastName;
  }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:80,代码来源:ListBox.SelectedIndexChanged


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