将文本从列表视图复制到另一个winform中
本文关键字:另一个 winform 复制 视图 文本 列表 | 更新日期: 2023-09-27 18:09:56
我真的很纠结,所以我希望有人能帮忙。我有两个winform,一个有列表视图,另一个有文本框。我想检查在列表视图中检查了哪个项目,并将此文本复制到第二个表单中。我已经尝试过此代码,但它不起作用,非常感谢任何帮助!
// 1st form
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
// 2nd form
private void Form4_Load(object sender, EventArgs e)
{
Form1 main = new Form1();
textBox1.Text = main.GetItemValue();
}
加载Form4
后,您将在其中创建一个新的Form1
。您需要参考原始Form1
。这可以通过多种方式实现,最简单的方法可能是将引用传递到Form4
构造函数中。
// Form 1
// This button creates a new "Form4" and shows it
private void button5_Click(object sender, EventArgs e)
{
Form4 editItem = new Form4(this);
editItem.Show();
}
public string GetItemValue()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
return listView1.Items[i].Text;
}
}
return "Error";
}
--
// Form 2 (Form4)
// Private member variable / reference to a Form1
private Form1 _form;
// Form4 Constructor: Assign the passed-in "Form1" to the member "Form1"
public Form4(Form1 form)
{
this._form = form;
}
// Take the member "Form1," get the item value, and write it in the text box
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = this._form.GetItemValue();
}
您只需要几个更改。无需添加您自己存储所有者表单的方式,因为此功能已经存在。
private void button5_Click(object sender, EventArgs e) // Brings up the second form
{
Form4 editItem = new Form4();
editItem.Show(this); //passes a reference to this form to be stored in owner
}
然后在另一个表格上引用它。
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = ((Form1)owner).GetItemValue();
}
尝试这种方式
FORM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
//Fields
public List<string> itemTexts;
public Form1()
{
InitializeComponent();
//Generate some items
for (int i = 0; i < 10; i++)
{
ListViewItem item = new ListViewItem();
item.Text = "item number #" + i;
listView1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Checked)
{
itemTexts.Add(item.Text);
}
}
Form2 TextBoxForm = new Form2(itemTexts);
TextBoxForm.Show();
}
}
}
FORM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
//Fields
List<string> itemTexts;
public Form2(List<string> itemTexts)
{
InitializeComponent();
this.itemTexts = itemTexts;
foreach (string text in itemTexts)
{
textBox1.Text += text + Environment.NewLine;
}
}
}
}