c# - Calling Form2's function from Form1 with Form1's public variables -


so basically, program logs game sockets, , once log in "control panel" button appears modify stuff in game, sending message in game. "control panel" button brings out of these features through new form. provide code snippet of form1 opening form2.

private void cpanel_btn_click(object sender, eventargs e) {     form2 cpanel = new form2();     cpanel.show(); } 

as can see, brings form2. trying form2 communicate form1. basically, form2 can communicate form1 far running simple voids (functions)-- that's all. here form2's constructor class. gave public name "main", , it's set "public form1 main;" on top of form2's class. form2's full class.

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 can see button's void on bottom of class tries communicate form1 , send data. however, runs function, let's @ form1's "sendgamedata" void.

    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 function, seems run error bottom line of void. form2 can't run sendgamedata because doesn't have "game_socket" public socket. huge issue, because stopping code working. game_socket assigned form1 before big happens, , don't want have reconstruct because that'll overlap sockets , make have reconnect , stuff.

i'm wondering how can make form2 have full access form1, , have of it's public variables have been set.

oh, , error i'm receiving is: object reference not set instance of object.

you creating new instance of form1 in form2, rather passing form2 instance of form1. 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!");         }     } } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -