if (listBox1.SelectedIndex==listBox1.Items.Count-1)
解决方案 »
- 一道C#面试题
- 字典序排序
- 一个asp:RadioButtonList的问题
- DataTable 中如何按行的内容删除 如像SQLSERVER 中的"delete from mytable where id=23"
- C#调用VB写的一个写Word文档的类库,出现异常:不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))
- Dll组建引用问题,急~~
- 多线程的锁定
- 请问一组radioButton怎样与一个值实现绑定?
- 请问C#如何做到:让控件的坐标,随着窗体的大小变化而改变
- 網頁間參數傳遞問題
- 主从表多表更新时如何控制事物?高手帮忙
- 如何用线程来实现将datatable中的数据填到listView中或者TreeView(不用数据绑定)
别瞎说,这样不会显示最后一个元素
事件的调用顺序为:
KeyDown
SelectedIndexChanged//真正变化的地方
KeyUp
如果再进行变化,都会有问题。
我再想想,也许会有更好的方法。
要不然也没有这么麻烦了。
需要一个类的成员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;
}}
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)
{
}
}
}
{
listBox1.SelectedIndex=-1;
listBox1.SelectedIndex=-1;
}两次赋值,就好了,也不知道为什么,可能是个bug吧