简体   繁体   中英

Passing an object to Windows Forms EventHandler

I have searched for hours, but I can't seem to find an answer for this. I feel as if there is a simple answer and I am just banging my head against the wall trying to find it.

private void Form1_Load(object sender, EventArgs e)
{
     ......bunch of code here...

     GroupBoxController GBControl = new GroupBoxController(groupbox1);

}

private void nextbutton_Click(object sender, EventArgs e)
{
     .....I want to be able to access GBControl and modify in here ..........
}

Thank you for any answers that might solve this.

private GroupBoxController GBControl;

private void Form1_Load(object sender, EventArgs e)
{
     ......bunch of code here...

     GBControl = new GroupBoxController(groupbox1);

}

private void nextbutton_Click(object sender, EventArgs e)
{
     GBControl.Whatever();
}

Try the above.

The general idea is that GBControl is now in scope for all members of the class. Before you had it as a local variable in the Form_Load event handler which makes it inaccessible outside of the function.

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