简体   繁体   English

使用Form1的公共变量从Form1调用Form2的函数

[英]Calling Form2's function from Form1 with Form1's public variables

So basically, my program logs you into a game with sockets, and once you log in the "Control Panel" button appears to modify some stuff in the game, like sending a message in game. 因此,基本上,我的程序将您登录到带有套接字的游戏,并且一旦您登录“控制面板”按钮,便会出现修改游戏中某些内容的信息,例如在游戏中发送消息。 The "Control Panel" button brings out all of these features through a new form. “控制面板”按钮通过新表格显示所有这些功能。 I will now provide you with a code snippet of Form1 opening Form2. 现在,我将为您提供打开Form2的Form1的代码段。

private void cpanel_btn_Click(object sender, EventArgs e)
{
    Form2 cPanel = new Form2();
    cPanel.Show();
}

As you can see, it brings up Form2. 如您所见,它启动了Form2。 I am trying to get Form2 to communicate with Form1. 我正在尝试让Form2与Form1通信。 Basically, Form2 can communicate with Form1 as far as running simple voids (functions)-- but that's about all. 基本上,Form2可以与Form1通信,只要运行简单的void(函数)就可以了-但这仅此而已。 Here is Form2's constructor class. 这是Form2的构造函数类。 I gave it the public name "Main", and It's set with a "public Form1 main;" 我给它起了公共名称“ Main”,并为其设置了“ public Form1 main;”。 on the top of Form2's class. 在Form2类的顶部。 This is Form2's full class. 这是Form2的完整课程。

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form1 Main;

        public Form2()
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            InitializeComponent();
            this.Main = new Form1();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Main.sendGameData("Hello world!");
        }
    }
}

As you can see the button's void on the bottom of this class tries to communicate with Form1 and send some data. 如您所见,此类底部的按钮空白会尝试与Form1通信并发送一些数据。 However, it runs the function, but let's look at Form1's "sendGameData" void. 但是,它运行函数,但是让我们看一下Form1的“ sendGameData”无效。

    public void sendGameData(string data)
    {
        data += "\x00";
        byte[] game_data = Encoding.UTF8.GetBytes(data);
        if (this.log_packets_opt) this.log("[SENT]: " + data);
        this.game_socket.Send(game_data);
    }

It runs the function, but it seems to run into an error with the bottom line of that void. 它运行该函数,但似乎与该空隙的底线发生了错误。 Form2 can't run sendGameData because it doesn't have the "game_socket" public socket. Form2无法运行sendGameData,因为它没有“ game_socket”公共套接字。 This is a huge issue, because it is stopping my code from working. 这是一个很大的问题,因为它使我的代码无法正常工作。 The game_socket is assigned into Form1 before anything big happens, and I don't want to have to reconstruct it because that'll overlap sockets and make you have to reconnect and stuff. 在发生任何大的事情之前,将game_socket分配到Form1中,并且我不想重构它,因为那会与套接字重叠,从而使您不得不重新连接。

I'm wondering how I can make Form2 have full access to Form1, and have all of It's public variables that have already been set. 我想知道如何使Form2拥有对Form1的完全访问权限,并具有已经设置的所有公共变量。

Oh, and the error I'm receiving is: Object reference not set to an instance of an object. 哦,我收到的错误是: 对象引用未设置为对象的实例。

You are creating a new instance of Form1 in Form2, rather than passing Form2 an instance of Form1. 您正在Form2中创建Form1的新实例,而不是将Form1的实例传递给Form2。 Do this instead: 改为这样做:

private void cpanel_btn_Click(object sender, EventArgs e)
{
    Form2 cPanel = new Form2(this);
    cPanel.Show();
}

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form1 Main;

        public Form2(Form1 main)
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            InitializeComponent();
            this.Main = main;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Main.sendGameData("Hello world!");
        }
    }
}

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

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