从Form2隐藏在主窗体上的按钮 [英] Hiding Button on Main Form from Form2

查看:61
本文介绍了从Form2隐藏在主窗体上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何在第二个表单中隐藏主表单上的按钮?我尝试编写以下代码,但无法正常工作:

//主要形式

Hi
How can I hide a button on the Main Form from the second form? I tried to write the following code but it is not working:

//Main Form

private void button1_Click(object sender, EventArgs e)
        {
            frmtwo formtwo = new frmtwo();
            formtwo.ShowDialog();
        }



//表格二



//Form Two

private void button1_Click(object sender, EventArgs e)
       {
           frmMain  formMain = new frmMain();
           formMain.button1.Hide();
           this.Close();
       }

推荐答案

不,那行不通-您需要当前主窗体实例,而不是新实例.
您尝试做的是将手机放在汽车的杂物箱中,然后期望能够打开我汽车中的杂物箱并找到您的电话.

可能-如果您在打开主窗体的实例时将其交给第二个实例,但这是一个非常糟糕的主意-它将两个窗体的设计锁定在一起,因此您不能在没有主窗体的情况下使用form2形式,或更改主要形式而不考虑对form2的影响.这违反了OOP的原则,被认为是非常糟糕的做法.

而是在form2中构造一个事件,主窗体在创建该事件时会对其进行订阅.发出事件信号后,主窗体便可以隐藏其自己的按钮,或采取其认为合适的其他任何措施.

这很容易做到.在form2中:
No, that wont work - you need the current instance of the main form, not a new one.
What you have tried to do is put your mobile phone in the glove box of your car, then expect to be able to open the glove box in my car and find your phone.

It is possible - if you hand an instance of the main form to the second when you open it, but it is a very bad idea - it locks the design of the two forms together, so you can''t use form2 without the main form, or change the main form without considering teh effects on form2. This is against the principles of OOP and considered very bad practice.

Instead, construct an event in form2 which the main form subscribes to when it creates it. When the event is signalled, the main form can then hide it''s own button, or take whatever other measures it sees fit.

It''s pretty easy to do. In form2:
// Signal operation disabled
public event EventHandler DisableOperation;

protected virtual void OnDisableOperation(EventArgs e)
   {
   EventHandler eh = DisableOperation;
   if (eh != null)
      {
      eh(this, e);
      }
   }

private void DoSomethingToDisableOperation()
   {
   OnDisableOperation(null);
   }

以您的主要形式:

In your main form:

private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.DisableOperation += new EventHandler(DisableOperation);
    fd.ShowDialog();
    }

//
// Fired when the file is changed at a lower level.
//
private void DisableOperation(object sender, EventArgs e)
    {
    myButton.Enabled = false;
    }


这里的问题是您没有使用主窗体的原始实例,而只是在使用新实例.您必须在第二种形式中引用您的主形式.您可以通过属性或重载的构造函数声明一个成员(类型为main形式)并将其设置为主形式的原始实例,然后使用该成员变量隐藏按钮.
The problem here is that you are not working with the original instance of the main form, you are just working with a new instance. You have to have the reference to your main form in your second form. You declare a member (of typemain form) and set it to the original instance of the main form, either through a property or an overloaded constructor and then hide the button using the member variable.


现在,您将创建mainForm的新实例,并希望获得对现有实例的引用.您有几种选择.仅举几例:
1.您可以在Form2中添加一个属性,以在创建formtwo时指定表单并使用该属性.即:
You now create a new instance of the mainForm and you want to get a reference to the existing instance. You have several options. To name a few:
1. You can add a property to Form2 for specifying the form when creating formtwo and use that. ie:
formtwo.some_form = frmMain;


2.您可以使用Application.OpenForms获得该表格,即(假设主表格位于索引0:


2. You can get the form using Application.OpenForms, ie (assuming the mainform is at index 0:

Application.OpenForms[0]



祝你好运!



Good luck!


这篇关于从Form2隐藏在主窗体上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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