【问题标题】:How to hide a button on form using button on another form如何使用另一个表单上的按钮隐藏表单上的按钮
【发布时间】:2015-08-24 18:46:38
【问题描述】:

我正在使用具有两个面板的 MDIparent 表单。 panel1 包含 form1,而 panel2 包含 form2。我想在单击 form2 上的按钮时隐藏 form1 上的按钮。我该怎么做?

【问题讨论】:

    标签: c# winforms button


    【解决方案1】:

    简短说明:

            => your A - form needs a click event "button_clicked"
            => MDI Parent recognize the event ( use a delegate / Listener )
            => MDI Parent has a list of his childforms
            => your B - form needs a public function "hide_button" who hides your control
            => check if your B - form is open
            => loop to the desired childform and call the function 
    

    如需更详细的答案,请发布代码示例 :)

    【讨论】:

      【解决方案2】:

      没有示例很难提供帮助,但让我们试试......

      正如我们所见,您在 MDI 表单中使用两个固定面板,对吗? 我不会假设您正在使用某种模式,但您应该这样做。请看:

      因此,如果没有模式,我认为最简单的方法是通过 DataBindings。

      您应该首先创建一个包含您想要控制的所有内容的类,例如 Button 中的 Enabled 属性。您必须使用 INotifyPropertyChanged 模式来进行 DataBinding,因此,您的控制器可能如下所示:

      internal class Controller : INotifyPropertyChanged
      {
          private bool button1Enabled;
      
          public bool Button1Enabled
          {
              get { return this.button1Enabled; }
              set
              {
                  if (this.button1Enabled == value) return;
                  this.button1Enabled = value;
                  this.NotifyChange("Button1Enabled");
              }
          }
      }
      

      您的 MDI 表单应具有:

      internal partial class MDIParent1 : Form
      {
          private IControllerChanged controller;
      
          public MDIParent1()
          {
              InitializeComponent();
      
              this.controller = new Controller();
          }
      }
      

      包含按钮的表单可能有:

      private void Form1_Load(object sender, EventArgs e)
      {
          this.button1st.DataBindings.Add("Enabled", this.controller, "Button1Enabled", false, DataSourceUpdateMode.OnPropertyChanged);
      }
      

      控制按钮的表单可能有:

      private void button3rd_Click(object sender, EventArgs e)
      {
          this.controller.Button1Enabled = !this.controller.Button1Enabled;
      }
      

      有了这个想法,您可以控制固定表单上的所有内容。

      使用这个例子,我做了一个非常非常简单的程序,试图找出你的必要性。请看https://github.com/anderson-rancan/stackoverflow_32189531

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-07
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 2014-04-21
        • 2011-12-10
        • 1970-01-01
        • 2014-08-11
        相关资源
        最近更新 更多