如何将listview行数据传递到另一个现有形式 [英] how to pass listview row data into another existing form
问题描述
我想拥有2个表单,其中第一个表单具有一个按钮,该按钮将在对话框表单中加载form2. form2将显示一个显示学生数据的列表视图.现在我需要提取所选行的第一个索引.一旦我双击该行,form2将关闭并将数据传递到form1的文本框中.
i want to have 2 forms in which the first form has a button that will load up form2 in a dialog form. form2 will show a listview displaying the data of a student. now i need to extract the 1st index of the selected row. once i double click on the row, form2 would close and pass the data into a textbox in form1.
我使用下面的代码关闭了我的form1并在form2中创建了一个新的form1实例.
i have used the code below which closes my form1 and creates a new instance of form1 in form2.
来自form2:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1 wa= new Form1();
wa.loadid(cl);
wa.Show();
this.Close();
}
来自form1:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Close();
}
public void loadid(String ms)
{
String newstring = ms;
studentid.Text = newstring;
}
推荐答案
我建议使用对话框,它非常简单:
I suggest using a Dialog, it makes it very easy:
这是Form1.实例化并作为对话框打开f2,然后等待其结果.确定
This is Form1. You instantiate and open f2 as Dialog and then wait for its result.OK
private void Button1_Click(System.Object sender, System.EventArgs e)
{
var f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK) {
studentId.Text = f2.SelectedStudentId;
} else {
studentId.Text = "Select a Student!!!!";
}
}
在Form2中,您已经在其中创建了列表视图和一个公开显示以下内容的公共属性:
This in Form2, where you have created your listview and a public property to expose:
public string SelectedStudentId { get; set; }
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
SelectedStudentId = cl;
DialogResult = DialogResult.OK; //will close this dialog (form2)
}
这篇关于如何将listview行数据传递到另一个现有形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!