Nishant
Nishant

Reputation: 1

How to hide a button on form using button on another form

I'm using a MDIparent form having two panels. panel1 contains form1 and panel2 contains form2. I want to hide buttons on form1 on clicking button on form2. How can i do it ?

Upvotes: 0

Views: 1491

Answers (2)

Anderson Rancan
Anderson Rancan

Reputation: 386

It's a quite difficult to help without an example, but let's try...

As we can see, you're working with two fixed Panels at your MDI Form, right? I will not assume that you're using some kind of pattern, but you should. Please, take a look at:

So, without a pattern, I think the most easy way to achieve that is by DataBindings.

You should first create a class that contains everything you want to control, like the Enabled property from your Buttons. You must use the INotifyPropertyChanged pattern to make the DataBinding, so, your controller may look like this:

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");
        }
    }
}

Your MDI Form should have:

internal partial class MDIParent1 : Form
{
    private IControllerChanged controller;

    public MDIParent1()
    {
        InitializeComponent();

        this.controller = new Controller();
    }
}

The form that contains the button may have:

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

And the Form that will control the Button may have:

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

With this idea, you can control everything on your fixed Forms.

Using this example, I made a very, very simple program, trying to figure out your necessity. Please, take a look at https://github.com/anderson-rancan/stackoverflow_32189531

Upvotes: 0

Craylen
Craylen

Reputation: 333

short description:

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

for a more detailled answer, post a code example :)

Upvotes: 1

Related Questions