简体   繁体   English

从Form2访问Form1控件

[英]Access Form1 control from Form2

In my application I have two forms (see image below). 在我的应用程序中,我有两种形式(请参见下图)。 Clicking on "Add" button shows the second "Add wireless network" form. 单击“添加”按钮将显示第二个“添加无线网络”表格。

在此处输入图片说明

After filling the form and clicking on "OK", second form adds the new profile and updates the wifi network profiles in the first form. 填写表单并单击“确定”后,第二个表单将添加新的配置文件并在第一个表单中更新wifi网络配置文件。 There is a RefreshProfiles function in the first form and I call it in the second form with this: 在第一种形式中有一个RefreshProfiles函数,在第二种形式中我用此函数称呼它:

((MainForm)this.Owner).RefreshWiFiProfiles();

and the "Add" button's code is this: 而“添加”按钮的代码是这样的:

private void AddButton_Click(object sender, EventArgs e)
{
    NewNetworkForm newNetworkForm = new NewNetworkForm();
    newNetworkForm.Owner = this;
    newNetworkForm.ShowDialog();
}

This setup is working fine, but since the number of lines increased as I added new things, I wanted to divide the code. 此设置工作正常,但是由于随着我添加新内容而增加了行数,因此我想对代码进行划分。 So I created a class to contain some functions. 因此,我创建了一个包含某些功能的类。

namespace WirelessNetworkManager
{
    public class Tools
    {
        public static void RefreshWiFiProfiles(ListView ListViewControl)
        {
            // clear old list
            ListViewControl.Items.Clear();

            // update it
        }
    }
}

I call the method in the first form with this: 我用第一种形式调用该方法:

Tools.RefreshWiFiProfiles(ProfilesListView);

and it's working fine. 而且工作正常。 The problem is, since I need to update the profiles list from the second form too, I need to call this in NewNetworkForm . 问题是,由于我也需要从第二个表单中更新配置文件列表,因此需要在NewNetworkForm调用它。 I can access and pass the ProfilesListView in MainForm because it's in there. 我可以在MainForm访问和传递ProfilesListView ,因为它在那里。 How can I pass a control that is in MainForm to a method in NewNetworkForm and modify it? 如何将MainForm的控件传递给NewNetworkForm的方法并进行修改? Or is there a better approach to do this? 还是有更好的方法来做到这一点?

File structure 档案结构

  • MainForm.cs (WirelessNetworkManager.MainForm) MainForm.cs(WirelessNetworkManager.MainForm)
  • NewNetworkForm.cs (WirelessNetworkManager.NewNetworkForm) NewNetworkForm.cs(WirelessNetworkManager.NewNetworkForm)
  • Tools.cs (WirelessNetworkManager.Tools) Tools.cs(WirelessNetworkManager.Tools)

You can set the Modifiers property of your ProfilesListView in Form2 to Public or Internal ( It's Private by default ) then you can access ProfilesListView of Form2 like this : Form2.ProfilesListView 您可以将Form2中ProfilesListView的Modifiers属性设置为PublicInternal默认情况下为Private ),然后可以像这样访问Form2的ProfilesListView:Form2.ProfilesListView

For example: 例如:

ProfilesList.Refresh(Form2.ProfilesListView);

Your mistake is : You are creating a new Form when you are using RefreshWiFiProfiles() method. 您的错误是:使用RefreshWiFiProfiles()方法时,您正在创建一个新的Form

You should access to existing Form2, for example if Form2 is Owner of Form1, this code works : 您应该访问现有的Form2,例如,如果Form2是Form1的所有者,则此代码有效:

Form ownerForm = (Form)this.Owner;
Tools.RefreshWiFiProfiles(ownerForm.ProfilesListView);

Here is the complete example: 这是完整的示例:

Form1 declaration: Form1声明:

  1. Add a ListView to Form1 , Set it's Modifiers to Public and add some items in it. ListView添加到Form1 ,将其修饰符设置为Public并在其中添加一些项。
  2. Add a Button to show Form2 添加一个按钮以显示Form2
  3. Add a Button to clear Form2 ListView 添加一个按钮以清除Form2 ListView

Form1 Code: Form1代码:

public partial class Form1 : Form
    {
        Form2 form2;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            form2 = new Form2();
            form2.Owner = this;
            form2.Show();
        }

        private void btnClearForm2List_Click(object sender, EventArgs e)
        {
            Tools.RefreshWiFiProfiles(form2.lstViewOfForm2);
        }
    }

Form2 declaration: Form2声明:

  1. Add a ListView to Form2 , Set it's Modifiers to Public and add some items in it. ListView添加到Form2 ,将其修饰符设置为Public并在其中添加一些项。
  2. Add a Button to clear Form1 ListView 添加一个按钮以清除Form1 ListView

Form2 Code: Form2代码:

public partial class Form2 : Form

    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnClearForm1List_Click(object sender, EventArgs e)
        {
            Form1 form1 = (Form1)this.Owner;
            Tools.RefreshWiFiProfiles(form1.lstViewOfForm1);
        }
    }

Declaration of Tools Class: 工具声明类:

public static class Tools
{
    public static void RefreshWiFiProfiles(ListView listView)
    {
        listView.Clear();
    }
}

I tried implementing this. 我尝试实现这一点。 __curious_geek 's answer and it worked fine. __curious_geek答案 ,它工作正常。

MainForm.cs MainForm.cs

namespace WirelessNetworkManager
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            // passing "this" (MainForm) to the second form
            NewNetworkForm newNetworkForm = new NewNetworkForm(this);
            newNetworkForm.ShowDialog();
        }
    }
}

NewNetworkForm.cs NewNetworkForm.cs

namespace WirelessNetworkManager
{
    public partial class NewNetworkForm : Form
    {
        public NewNetworkForm()
        {
            InitializeComponent();
        }

        // a local variable to reference the first form (MainForm)
        private MainForm mainForm = null;

        // a second overloaded constructor accepting a form
        public NewNetworkForm(Form callingForm)
        {
            // mainForm now refers to the first form (MainForm)
            mainForm = callingForm as MainForm;
            InitializeComponent();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            // create wifi profile with user input

            // accessing the ListView using this.mainForm
            Tools.RefreshWiFiProfiles(this.mainForm.ProfilesListView);

            this.Close();
        }
    }
}

Tools.cs Tools.cs

namespace WirelessNetworkManager
{
    public class Tools
    {
        public static void RefreshWiFiProfiles(ListView ListViewControl)
        {
            ListViewControl.Items.Clear();

            // iterate through wifi profiles and populate ListViewControl
        }
    }
}

Also the control element's (ListViewControl) Modifiers should be set to Public or Internal. 此外,控件元素的(ListViewControl)修饰符应设置为Public或Internal。 Otherwise you'll get an error. 否则会出现错误。

'WirelessNetworkManager.MainForm.ProfilesListView' is inaccessible due to its protection level

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM