我的申请表中有两种表格。在我的 Form1 中,我有一个包含一些项目的列表视图。当我双击一行时,我应该弹出一个窗口,允许我编辑行值。为此,我使用了doubleclick事件。现在,对于弹出窗口,我创建了新表单 - Form2 。我已将listview设置为Form1中的内部,以便访问form2中的选定行值。在form2加载中,我正在检索要在文本框中显示的所选行的值,但这给了我错误。这是我的代码:
//this is in form1
private void bufferedListView1_DoubleClick(object sender, EventArgs e)
{
form2 obj = new form2();
obj.ShowDialog();
}
//in form2
Form1 o = new Form1();
private void form2_Load(object sender, EventArgs e)
{
txt_editname.Text = o.bufferedListView1.SelectedItems[0].SubItems[0].Text;
txt_editno.Text = o.bufferedListView1.SelectedItems[0].SubItems[1].Text;
}
我得到的错误是:InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
我哪里错了?
答案 0 :(得分:1)
在form2的构造函数中传递所需的数据
public form2(string text1, string text2)
{
//work with values
}
将调用代码更改为:
private void bufferedListView1_DoubleClick(object sender, EventArgs e)
{
form2 obj = new form2(bufferedListView1.SelectedItems[0].SubItems[0].Text,
bufferedListView1.SelectedItems[0].SubItems[1].Text);
obj.ShowDialog();
}
答案 1 :(得分:0)
Form1 o = Application.OpenForms["Form1"] as Form1;
private void form2_Load(object sender, EventArgs e)
{
txt_editname.Text = o.bufferedListView1.SelectedItems[0].SubItems[0].Text;
txt_editno.Text = o.bufferedListView1.SelectedItems[0].SubItems[1].Text;
}
您应该检索已创建的Form1实例,而不是新实例。
答案 2 :(得分:0)
您的代码应该是这样的:
//this is in form1
private void bufferedListView1_DoubleClick(object sender, EventArgs e)
{
form2 obj = new form2
{
Name = o.bufferedListView1.SelectedItems[0].SubItems[0].Text,
No = o.bufferedListView1.SelectedItems[0].SubItems[1].Text,
};
obj.ShowDialog();
}
//in form2
public String Name;
public String No;
Form1 o = new Form1();
private void form2_Load(object sender, EventArgs e)
{
txt_editname.Text = Name;
txt_editno.Text = No;
}