从其他表单访问列表视图项 [英] Accessing listview items from another form

查看:41
本文介绍了从其他表单访问列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一下我有两种形式:Form1Form2. 在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)
            {

            }
        }
    }
}

这篇关于从其他表单访问列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆