1

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();
        }
Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • You want to use the sub-class type if you are trying to reach members you authored. – IAbstract Aug 29 '11 at 20:39
  • @Adam Maras: `The type or namespace name 'mainForm' could not be found (are you missing a using directive or an assembly reference?)` – PeeHaa Aug 29 '11 at 20:44
  • I notice that your error lists `mainForm` as the type, whereas your code examples show `MainForm`. Make sure your capitalization is correct everywhere. – Adam Maras Aug 29 '11 at 21:03
  • @Adam Maras: I'm sorry, that's just a typo. The auto-complete function also doesn't show my mainform as a type. And yes it is declared public. – PeeHaa Aug 29 '11 at 21:06
  • Can you post the top of your MainForm.cs file? Basically, the namespace declaration and the top of the class declaration. – Adam Maras Aug 29 '11 at 21:08
  • @Adam Maras: Done! Thanks for taking the time to help me. – PeeHaa Aug 29 '11 at 21:16
  • How odd... everything appears to be in order. Can you show me the same for your `NewForm`, as well as where and how you're invoking the constructor for it from `MainForm`? – Adam Maras Aug 29 '11 at 21:19
  • Okay, something wonky is going on with your project. Based on everything you've posted, your original attempt should have worked flawlessly. Can you ZIP your solution and upload it somewhere so I can better inspect it? – Adam Maras Aug 29 '11 at 21:28
  • @Adam Maras: Sure. I'm zipping it now. And will upload it once it finished. I'll let you know. – PeeHaa Aug 29 '11 at 21:32

4 Answers4

1

If you know exactly what kind of form type to use, simply change the code to reference that kind of form.

MainForm mainform_instance;

public NewForm(MainForm mainform)
{
    this.mainform_instance = mainform;
    InitializeComponent();
}

If you can have multiple types of forms then you can try to cast it to MainForm prior to using it, and if successful - use it.

MainForm mainForm = mainform_instance as MainForm;
if (mainForm != null) mainForm.foo();
havardhu
  • 3,576
  • 2
  • 30
  • 42
1

In the NewForm objects Load event, this.Owner would return your main form object if you invoked NewForm with a ShowDialog(this) call.

// in your MainForm
NewForm nwForm = new NewForm();
nwForm.ShowDialog(this);

// inside your NewForm object, after loading
(this.Owner as MainForm).Foo();

I realize I misdirected you in your previous post by asking you to use this.Parent(); I should have remembered it is this.Owner(). Apologies!

Arun
  • 2,493
  • 15
  • 12
  • It's ok, however this isn't going to work. How would the NewForm know the Owner has a `Foo()` method? The thing is I get an error if I try to setup my MainForm in my NewForm when I try to declare: `MainForm MyMainForm = new MainForm();` – PeeHaa Aug 29 '11 at 20:52
  • Got it... You are right in that this.Owner wouldnt know of the "Foo()" method - I got to cast it. I have edited the answer. I am assuming Foo() is public. – Arun Aug 29 '11 at 20:58
  • On the second part (you getting error in Mainform), I am not clear. How are you invoking the second form from the Main form? – Arun Aug 29 '11 at 20:59
  • `newForm = new Namespace.NewForm(this); newForm.Show();` – PeeHaa Aug 29 '11 at 21:19
  • First, I would avoid passing MainForm in NewForm's constructor. Try modifying the NewForms constructor to accept no arguments. Second, I will invoke Show() (or ShowDialog(), if I need it to be Modal) WITH the parameter "this". That is, newForm.Show(this); – Arun Aug 29 '11 at 21:26
0

Make the custom function public and pass the reference of main form to the new form. Then using the main form's reference you will be able to call any public method of main form from new form

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

Is your function declared private?

I never recommend a main form reference to any other form ...there is always a better way. That said, if you have a method that needs to be executed from the form, you must pass a reference to that main form method.

public NewForm(Form mainform, Action mainFormMethod)
{
    this.mainform_instance = mainform;
    InitializeComponent();

    mainFormMethod.Invoke(); //  or just mainFormMethod();
}

See MSDN: Action Delegate

There is also the Action<T> ...be sure and check those out. If your method requires a return, you will need to check out Func<TResult>

For the records, I would recommend re-thinking sending the reference to the main form.

IAbstract
  • 19,551
  • 15
  • 98
  • 146