问题描述
让我们考虑一下我有两种形式:Form1
和Form2
.在Form1
上,我有一个列表视图DataVisualizationList
.如果我要使用Form1
中的列表视图的选定项目,则foreach (ListViewItem dr in DataVisualizationList.SelectedItems)
可以完美地工作.如何从Form2
做同样的事情?
Let's consider I have two forms: Form1
and Form2
.On Form1
I have a listview DataVisualizationList
.If I want to work with selected items of the listview from Form1
, foreach (ListViewItem dr in DataVisualizationList.SelectedItems)
works perfectly.How to do the same from Form2
?
推荐答案
您要么必须引用Form2
中的Form1
,并且可以公开访问DataVisualizationList
,要么必须引用在Form2
中.
You would have to either have a reference to Form1
in Form2
and have the DataVisualizationList
be publicly accessable, or have a reference to DataVisualizationList
in Form2
.
您可以通过成员引用来做到这一点.
You could to this with member references.
您必须在Form2中设置Form1的引用.
You would have to set the reference of Form1 in Form2.
在Form1中这样的事情
Something like this inside Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.Show();
}
然后在Form2中
public partial class Form2 : Form
{
public Form1 f1;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (f1 != null)
{
foreach (ListViewItem dr in f1.DataVisualizationList.SelectedItems)
{
}
}
}
}
这篇关于从其他表单访问列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!