if (listBox1.SelectedIndex==listBox1.Items.Count-1)

解决方案 »

  1.   

    在keyup事件里面写同样的代码就好了。
      

  2.   

    jiezhi(西域浪子):
    别瞎说,这样不会显示最后一个元素
      

  3.   

    原本想在SelectedIndexChanged事件做,但发现有问题,因为当走到最后一条时,无法触发此事件,只能给你个临时的方法,最后加一个空的元素,然后在KeyUp事件中做,这样是可以的,但每次加数据,修改会有些麻烦。
    事件的调用顺序为:
    KeyDown
    SelectedIndexChanged//真正变化的地方
    KeyUp
    如果再进行变化,都会有问题。
    我再想想,也许会有更好的方法。
      

  4.   

    为何按down键不触发keypress事件呢?
    要不然也没有这么麻烦了。
      

  5.   

    问题解决了,程序如下:
    需要一个类的成员nIndex
    private void listBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {

    //MessageBox.Show("KeyUp");
    if(nIndex==listBox1.SelectedIndex&&
    nIndex==listBox1.Items.Count-1)
    listBox1.SelectedIndex=0;
    }private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if(e.KeyValue==40)
    {
    //MessageBox.Show("KeyDown");
    nIndex=listBox1.SelectedIndex;
    }}
      

  6.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication11
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private bool sss=false;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // listBox1
    // 
    this.listBox1.ItemHeight = 12;
    this.listBox1.Items.AddRange(new object[] {
      "trtyrt",
      "rgdfs",
      "gdsf",
      "g",
      "sdfg",
      "sdfg",
      "sdf",
      "gdf",
      "sg",
      "df",
      "g"});
    this.listBox1.Location = new System.Drawing.Point(40, 56);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(120, 148);
    this.listBox1.TabIndex = 0;
    this.listBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyDown);
    this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(208, 120);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(160, 32);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 2;
    this.textBox1.Text = "textBox1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.textBox1,
      this.button1,
      this.listBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if(this.listBox1.SelectedIndex==this.listBox1.Items.Count-1)
    {
    this.listBox1.SelectedIndex=0;
    sss=true;
    }
    else
    {
    sss=false;
    }
    } private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    if(this.listBox1.SelectedIndex==1)
    {
    if(sss==true)
    {
    this.listBox1.SelectedIndex=0;
    }
    }
    } private void button1_Click(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  7.   

    谢谢各位,我偶然间试了一种简单的方法,就是if(listBox1.Selecte==listBox1.Items.Count-1)
    {
    listBox1.SelectedIndex=-1;
    listBox1.SelectedIndex=-1;
    }两次赋值,就好了,也不知道为什么,可能是个bug吧