我做了我小程序,其中一点是想让在ListBox控件(personlist)中选中的项目中的某一特定内容显示
在新窗体的TextBox控件(txtBox_name)中部分代码如下:但这段代码发生异常,各位大虾帮忙看看是什么原因阿! 谢谢!!!!! using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using concact; namespace ContactManagement
{
public partial class FrmMain : Form
{
private System.Windows.Forms.ListBox personlist;
....
private System.Windows.Forms.Button btn_Upd;
private System.Windows.Forms.Button btn_Search; private static int i;
public FrmMain()
{
InitializeComponent();
}
public string Separate()
{
string strname= null;
if (i > 0)
{
string str = personlist.Items[i].ToString();
string[] strary = str.Split(' ');
strname = strary[0];
}
return strname; }
private void personlist_SelectedIndexChanged(object sender, EventArgs e)
{
i=personlist.SelectedIndex; }
private void btn_Upd_Click(object sender, EventArgs e)
{
Update upd = new Update();
upd.ShowDialog();
}
.....
} namespace ContactManagement
{
public partial class Update : Form
{
...
private System.Windows.Forms.Button btn_OK;
private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.TextBox txtBox_name;
private System.Windows.Forms.RadioButton radbtn_m;
private System.Windows.Forms.RadioButton radbtn_f;
....
public Update()
{
InitializeComponent();
} private void Update_Activated(object sender, EventArgs e)
{
FrmMain fm = new FrmMain();
txtBox_name.Text = fm.Separate();
}
........
}
************** 异常文本 **************
System.ArgumentOutOfRangeException: InvalidArgument=“2”的值对于“index”无效 。
参数名: index
在 System.Windows.Forms.ListBox.ObjectCollection.get_Item(Int32 index)
在 ContactManagement.FrmMain.Separate() 位置 D:\教程 \C#\ex\ContactManagement\ContactManagement\FrmMain.cs:行号 36
在 ContactManagement.Update.Update_Activated(Object sender, EventArgs e) 位 置 D:\教程\C#\ex\ContactManagement\ContactManagement\Update.cs:行号 24
在 System.Windows.Forms.Form.OnActivated(EventArgs e)
在新窗体的TextBox控件(txtBox_name)中部分代码如下:但这段代码发生异常,各位大虾帮忙看看是什么原因阿! 谢谢!!!!! using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using concact; namespace ContactManagement
{
public partial class FrmMain : Form
{
private System.Windows.Forms.ListBox personlist;
....
private System.Windows.Forms.Button btn_Upd;
private System.Windows.Forms.Button btn_Search; private static int i;
public FrmMain()
{
InitializeComponent();
}
public string Separate()
{
string strname= null;
if (i > 0)
{
string str = personlist.Items[i].ToString();
string[] strary = str.Split(' ');
strname = strary[0];
}
return strname; }
private void personlist_SelectedIndexChanged(object sender, EventArgs e)
{
i=personlist.SelectedIndex; }
private void btn_Upd_Click(object sender, EventArgs e)
{
Update upd = new Update();
upd.ShowDialog();
}
.....
} namespace ContactManagement
{
public partial class Update : Form
{
...
private System.Windows.Forms.Button btn_OK;
private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.TextBox txtBox_name;
private System.Windows.Forms.RadioButton radbtn_m;
private System.Windows.Forms.RadioButton radbtn_f;
....
public Update()
{
InitializeComponent();
} private void Update_Activated(object sender, EventArgs e)
{
FrmMain fm = new FrmMain();
txtBox_name.Text = fm.Separate();
}
........
}
************** 异常文本 **************
System.ArgumentOutOfRangeException: InvalidArgument=“2”的值对于“index”无效 。
参数名: index
在 System.Windows.Forms.ListBox.ObjectCollection.get_Item(Int32 index)
在 ContactManagement.FrmMain.Separate() 位置 D:\教程 \C#\ex\ContactManagement\ContactManagement\FrmMain.cs:行号 36
在 ContactManagement.Update.Update_Activated(Object sender, EventArgs e) 位 置 D:\教程\C#\ex\ContactManagement\ContactManagement\Update.cs:行号 24
在 System.Windows.Forms.Form.OnActivated(EventArgs e)
{
...
private void fillperson()
{
personlist.Items.Clear();
ArrayList list = AddressList.GetList();
foreach (string str in list)
{
personlist.Items.Add(str);
} }
...
private void FrmMain_Load(object sender, EventArgs e)
{
fillperson();
}
...
private void FrmMain_Activated(object sender, EventArgs e)
{
fillperson();
}
....
} 在线等待解决阿!!!
if (i > 0)
{
string str = personlist.Items[i].ToString();
再加个判断,更安全一些嘛:
if (i > 0 && i < personlist.Items.Count)
{
string str = personlist.Items[i].ToString();