Yesterday I asked a question how it is possible to access my mainform from another form to give it the focus.
Giving focus back to main form
The solution was to add a reference to the mainform when the new form is called.
This works great for .focus()
by doing:
Form mainform_instance;
public NewForm(Form mainform)
{
this.mainform_instance = mainform;
InitializeComponent();
}
However now I need to access a 'custom function' on the main form from my new form.
But I cannot access it since it is declared as Form mainform_instance
and the Form
type doesn't have my custom function. (at least that's what I think what goes wrong.)
So I thought I try:
MainForm mainform_instance;
and
Namespace.MainForm mainform_instance;
But both don't work.
How can I access my function (foo()
) on the mainform from the new form?
EDIT
As requested by Adam Maras in comment:
namespace Namespace
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
EDIT2
MainForm code which calls the NewForm:
newForm = new Namespace.NewForm(this);
newForm.Show();
NewForm construct:
namespace Namespace
{
public partial class NewForm : Form
{
// here I tried to do MainForm mainform_instance as well as in the construct param
Form mainform_instance;
public NewForm(Form mainform)
{
this.mainform_instance = mainform;
InitializeComponent();
}