首页 文章

如何将列表视图从一个表单传递到另一个表单?

提问于
浏览
0

我有两种形式 . form1和form2 . 在form1上有一个按钮,我可以访问form2,在form2中,我有一个listview2和一些文本框 . 我设法将项目输入listview2 . 然后当我单击form2中的OK按钮时,form1中的listview1应该与listview2完全一样 . 那么伙计们,有人能建议我这样做吗?谢谢

以下是我的代码 . 我希望我不要混淆你们所有人 .

Form1代码=>

namespace MainServerPage

{public partial class MainServerPage:Form {public ListView LV;
public MainServerPage(){InitializeComponent(); }

private void btnAdd_Click(object sender, EventArgs e)
    {
        AddItem Add = new AddItem(this);                //to open form2
        Add.ShowDialog();
    }

}

}

Form2代码=>

namespace MainServerPage

{public partial class AddItem:Form {MainServerPage currentform; //我学习了将表单传递给另一个表单的方法,但它不能正常工作公共AddItem(MainServerPage incomingform)

private void btnUpdate_Click(object sender, EventArgs e)
    {
        ListViewItem item = new ListViewItem(txtCode.Text);
        item.SubItems.Add(txtLocation.Text);
        item.SubItems.Add(cbxStatus.Text);
        item.SubItems.Add(txtWeatherHigh.ToString());
        item.SubItems.Add(txtWeatherLow.ToString());           

        listView2.Items.Add(item);      //send to listView2

        txtCode.Text = "";
        txtLocation.Text = "";
        cbxStatus.Text = "";
        txtWeatherHigh.Text = "";
        txtWeatherLow.Text = "";
        cbxZone.Text = "";                     

    }

    private void btnOk_Click(object sender, EventArgs e)
    {
         currentform.LV = load;    //I got stuck here...do not know what to do
    }
}

}

1 回答

  • 0

    通常,它是列表视图所代表的数据的's not the list view you want to pass, it' . 您可能应该重新考虑您的设计,以便您使用btnUpdate_Click函数构建数据对象,而不是直接构建ListViewItem . 然后,您可以将数据对象传递回第一个表单 .

相关问题