简体   繁体   中英

Calling a non static void method from another class in c#

Hey I got Code Written in Class Form1 And Form2. I want to call the method openkindForm() from Form2. I tried every soloution I found. I got this one at the moment which is not working it gives me a System.NullReferenceException. I do not know why it isnt working. I tried it so long but whatever I do it will not workout somehow. I would be thankfull for an answer. Kind regards

First Class

   using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace FBDP00
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
          submenupanel.Visible = false;
    }   

        private void funktionenSM_Click(object sender, EventArgs e)
        {
            switch (submenupanel.Visible)
            {
                case true:
                    submenupanel.Visible = false;
                    break;

                case false:
                    submenupanel.Visible = true;
                    break;

            }   
        }


        public void neuepruefungSm_Click(object sender, EventArgs e)
        {
            submenupanel.Visible = false;
            openkindForm(new Form2());          
        }


        public Form activeForm = null;
        public   void openkindForm(Form childForm)
        {
           if (activeForm != null)
            {
                activeForm.Close();
            }

            activeForm = childForm;
            childForm.TopLevel = false;

            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            backgroundPanel.Controls.Add(childForm);
           childForm.Show();

        }            
    }
}

Class 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static FBDP00.Form1;





namespace FBDP00
{

public partial class Form2 : Form
{
    public Form1 testform;

    public Form2()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void funktionenSM_Click(object sender, EventArgs e)
    {

    }

    public void baukontrolleb_Click(object sender, EventArgs e)
    {

        testform.openkindForm(new Form3());
    }
   }
}

In Form2 you have defined a variable for Form1 (testform), but you don't set this anywhere. So this is why you get a Null reference error when you try to use it, because it is null!

So when you create your Form2 then you need to set this value. In your case, maybe in the constructor like this.

public Form1 testform;

private Form2(Form1 f1)
{
    InitializeComponent();
    testform = f1;
}

Then call it like this:

public void neuepruefungSm_Click(object sender, EventArgs e)
{
    submenupanel.Visible = false;
    openkindForm(new Form2(this));
}

However, looking at your openkindForm method, it seems to me that this really has nothing to do with Form1, and shares no variables, so should not be in this class.

You should either make this static (together with the activeForm variable), or make it a Singleton class instead. But certainly this should be a separate class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM