从Form2访问Form1控件

时间:2015-07-05 07:06:14

标签: c# forms arguments controls

在我的应用程序中,我有两种形式(见下图)。点击"添加"按钮显示第二个"添加无线网络"形式。

enter image description here

填写表单并单击"确定"后,第二个表单添加新配置文件并以第一种形式更新wifi网络配置文件。第一种形式有RefreshProfiles函数,我用第二种形式称它:

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

和"添加"按钮的代码是:

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

这个设置工作正常,但由于我添加新内容时线条数量增加,我想分割代码。所以我创建了一个包含一些函数的类。

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

            // update it
        }
    }
}

我用第一种方式调用方法:

Tools.RefreshWiFiProfiles(ProfilesListView);

它工作正常。问题是,由于我还需要从第二个表单更新配置文件列表,我需要在NewNetworkForm中调用它。我可以访问并传递ProfilesListView MainForm,因为它在那里。如何将MainForm中的控件传递给NewNetworkForm中的方法并对其进行修改?或者有更好的方法来做到这一点吗?

文件结构

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

2 个答案:

答案 0 :(得分:1)

您可以将Form2中的ProfilesListView的修饰符属性设置为公开内部它的私有属性默认)然后您可以像下面这样访问Form2的ProfilesListView:Form2.ProfilesListView

例如:

ProfilesList.Refresh(Form2.ProfilesListView);

您的错误是:当您使用RefreshWiFiProfiles()方法时,您正在创建新表单

您应该访问现有的Form2,例如,如果Form2是Form1的所有者,则此代码有效:

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

以下是完整的示例:

Form1声明:

  1. ListView 添加到 Form1 ,将其修改器设置为公开并添加一些项目在它。
  2. 添加按钮以显示Form2
  3. 添加按钮以清除Form2 ListView
  4. 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声明:

    1. ListView 添加到 Form2 ,将其修改器设置为公开并添加一些项目在它。
    2. 添加按钮以清除Form1 ListView
    3. 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);
              }
          }
      

      工具类声明:

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

答案 1 :(得分:0)

我尝试实施this. __curious_geekanswer,但效果很好。

<强> 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

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

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

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

此外,控制元素(ListViewControl)修饰符应设置为Public或Internal。否则你会收到错误。

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