请使用此示例代码,使用 2 种表格,
Form1 代码
public delegate void ListViewAddDelegate(string text);
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void AddItem(string item)
{
listView1.Items.Add(item);
}
private void button1_Click(object sender, EventArgs e)
{
ListViewAddDelegate Del = new ListViewAddDelegate(AddItem);
Form2 ob = new Form2(Del);
ob.Show();
}
}
}
Form2 的代码
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public ListViewAddDelegate deleg;
public Form2()
{
InitializeComponent();
}
public Form2(ListViewAddDelegate delegObj)
{
this.deleg = delegObj;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Equals(""))
{
deleg(textBox1.Text);
}
else
{
MessageBox.Show("Text can not be emopty");
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}