开发者

Trying to use a reference to my mainform

开发者 https://www.devze.com 2023-03-31 08:42 出处:网络
Yesterday I asked a question how it is possible to access my mainform from another form to give it the focus.

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


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!


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();


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消