2
Answers

Access controls on a form from another form

Resmy Ravi

Resmy Ravi

13y
1.4k
1

Hi...

I am developing a woindows application.In the MDI form i have used two panels panel1 and panel 2.when the mdi form loades first panel1
gets displayed.when i display another form and i need to make visible the panel2 when a button in form1 is clicked.how can i possible this?plz help me...
Answers (2)
0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 13y
Directly accessing the controls of another form is not object-oriented.

MDI is designed for windows of multiple documents, not multiple windows. I don't know the details of your requiremetns, but probably a better design would be to create class that is like a document and then the forms and the rest of the application can access the objects.
0
Zoran Horvat

Zoran Horvat

NA 5.7k 516.2k 13y
You can freely access contents of another form, but off course you should not manipulate contained controls directly. Rather, create a public method on the form, e.g. like this:

public void SetPanelVisible(bool visible)
{
        panel2.Visible = visible;
}

Now on the main form you must keep reference to the child form. Use that reference in the button Click event handler:
private void button_Click(object sender, EventArgs e)
{
        form2.SetPanelVisible(true);
}

Zoran