How to access main form functions from child forms

  • Thread starter Thread starter eric_berlin
  • Start date Start date
E

eric_berlin

I imagine that this question has been posted before, but I couldn't
find it. I have an app with several custom dialogs (windows forms) and
I need to access methods that are on the main form. The only way I
have found this to be possible is to make the main form functions
static, but that is too restrictive. What is a good clean way to do
this?
 
You can make them public ... then pass the instance of the main for to the
dialog ..

as an example ...

public class MainForm {
public void SetLabel(string _Text) {
this.lblFoo.Text = _Text;
}
}

public class Dialog {
private MainForm m_Parent;

private void SomeMethod() {
m_Parent.SetLabel("Foo");
}

public Dialog(MainForm _Parent) {
m_Parent = _Parent;
}
}

Another common methodology used to to have the dialog have public events
which the parent form then handles.

Cheers,

Greg Young
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top