●难道是微软的经典错误???
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinAPP01
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private ArrayList myListData = new ArrayList();
private System.Windows.Forms.Button btnADD;
private System.Windows.Forms.Button btnDEL;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnADD = new System.Windows.Forms.Button();
this.btnDEL = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(225, 40);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(151, 268);
this.listBox1.TabIndex = 0;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 42);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// btnADD
//
this.btnADD.Location = new System.Drawing.Point(132, 43);
this.btnADD.Name = "btnADD";
this.btnADD.TabIndex = 4;
this.btnADD.Text = "添加";
this.btnADD.Click += new System.EventHandler(this.btnADD_Click);
//
// btnDEL
//
this.btnDEL.Location = new System.Drawing.Point(229, 326);
this.btnDEL.Name = "btnDEL";
this.btnDEL.TabIndex = 5;
this.btnDEL.Text = "删除";
this.btnDEL.Click += new System.EventHandler(this.btnDEL_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(689, 441);
this.Controls.Add(this.btnDEL);
this.Controls.Add(this.btnADD);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btnADD_Click(object sender, System.EventArgs e)
{
myListData.Add(new listData(textBox1.Text,"VALUE"));
listBox1.DataSource=null;
listBox1.DataSource=myListData;
listBox1.DisplayMember="strTXT";
listBox1.ValueMember="strVAL";
}
private void btnDEL_Click(object sender, System.EventArgs e)
{
myListData.Remove(listBox1.SelectedItem);
listBox1.DataSource=null;
listBox1.DataSource=myListData;
listBox1.DisplayMember="strTXT";
listBox1.ValueMember="strVAL";
//listBox1.SelectedIndex=-1;
}
}
//==============================================
public class listData
{
private string myTXT;
private string myVAL;
public listData (string txt,string val)
{
this.myTXT =txt;
this.myVAL=val;
}
public string strTXT
{
get
{
return myTXT;
}
}
public string strVAL
{
get
{
return myVAL;
}
}
}
}
在Windows窗体中有一个listBox1,一个textBox1 和两个按钮,
一个按钮把textBox1中的内容添加到listBox1中(用ArrayList变量);
这个没有问题。
另外一个按钮删除listBox1中所选的当前项,如果删除的项不是最后一项时也
没有任何问题。
可是
当删除最后一项之后(listBox1中有多于1个的项)
再用光标去点击选择listBox1中的项目时
经典错误发生:
未处理的“System.ArgumentOutOfRangeException”类型的异常出现在 mscorlib.dll 中。
其他信息: 索引超出范围。必须为非负值并小于集合大小。
?????????????????????????