简体   繁体   English

从 c# 中的另一个 class 调用非 static void 方法

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

Hey I got Code Written in Class Form1 And Form2.嘿,我在 Class Form1 和 Form2 中编写了代码。 I want to call the method openkindForm() from Form2.我想从 Form2 中调用 openkindForm() 方法。 I tried every soloution I found.我尝试了我找到的每一个解决方案。 I got this one at the moment which is not working it gives me a System.NullReferenceException.我现在得到了这个,它不工作它给了我一个 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第一个 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 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.在 Form2 中,您为 Form1 (testform) 定义了一个变量,但您没有在任何地方设置它。 So this is why you get a Null reference error when you try to use it, because it is null!所以这就是为什么你在尝试使用它时得到一个 Null 引用错误,因为它是空的!

So when you create your Form2 then you need to set this value.因此,当您创建 Form2 时,您需要设置此值。 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.但是,看你的openkindForm方法,在我看来这确实与Form1无关,并且不共享任何变量,所以不应该在这个class中。

You should either make this static (together with the activeForm variable), or make it a Singleton class instead.您应该将此 static(与 activeForm 变量一起),或改为 Singleton class。 But certainly this should be a separate class.但当然这应该是一个单独的 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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