如何使一种形式的按钮影响另一种形式的列表视图? [英] How to make buttons from one form affect a listview in another one?
问题描述
我想要一种形式的按钮,然后在单击它们时,将一项以另一种形式添加到列表中.
I want to have buttons in one form, then as I click on them, an item is added to a list in another form.
这是我当前的代码,请记住,在此代码中,按钮的格式与列表视图的形式相同,我想将其中任一按钮移到新的位置:
This is my current code, take in mind that in this code the buttons are in the same form as the listview, and I want to move either to a new one:
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Add("Panama");
}
private void button3_Click(object sender, EventArgs e)
{
listView1.Items.Add("Brazil");
}
private void button4_Click(object sender, EventArgs e)
{
listView1.Items.RemoveAt(0);
}
单击按钮后,如何以另一种形式的列表添加项目?
How do I do so when the button is clicked, the list in another form gets the item added?
推荐答案
您需要引用调用操作的表单中的其他表单. 一种方法是将ListView公开为Form本身的属性,并通过Form对其进行引用
You need to have a reference to the other forms from the form that invokes the action. One way is to expose the ListView as a property on the Form itself and the just reference it via the Form
从Form1
private void button4_Click(object sender, EventArgs e)
{
Form2Instance.MyListView.Items.RemoveAt(0);
}
在Form2中,添加以下包装以显示原始listView
In Form2 add the following wrapper to expose the original listView
public ListView MyListView{get{return this.ListView1;}}
这篇关于如何使一种形式的按钮影响另一种形式的列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!