0

我的申请中有两个表格。在我的Form1中,我有一个包含一些项目的列表视图。当我双击一行时,我应该得到一个弹出窗口,允许我编辑行值。为此,我使用了双击事件。现在对于弹出窗口,我创建了新表单Form2。我已将列表视图设置为 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

我哪里错了?

4

3 回答 3

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();
}
于 2012-08-05T06:31:10.797 回答
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 实例,而不是新实例。

于 2012-08-05T06:31:02.620 回答
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;
    }
于 2012-08-05T07:42:26.503 回答