Trying to build a simple app in winforms but I'm noticing a gap in my understanding of events inside the winforms model. I know I'm fairly close to the solution but there are a few things that I dont know how to do the "winforms way"
Here is the UI layout of what I am trying to accomplish. Form1 is generated via the VS2019 template, but both LeftSide and RightSide are two separate classes that I have built.
*****Form1************************
| |
| **LeftSide*** **RightSide** |
| | | | | |
| | _TextBox | | _TextBox | |
| | _Button | | _Button | |
| | | | | |
| | | | | |
| ************* ************* |
| |
**********************************
Both LeftSide and RightSide:
- return a Panel with a textbox and button
- have 'child' controls that are not exposed (ie, the class itself is responsible for the UI layout and the data validation)
- require communication to each other and Form1
- are not inherited from anything; they are used in Form1 via a namespace
- are instantiated inside Form1
Here is (an incomplete) Form1, idk what to add here
namespace myapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Here is both LeftSide and RightSide classes (note that they are the same, except substitute the word "left" with "right")
namespace myapp.LeftSide
{
class LeftSide
{
private Panel LeftSide_Panel = new Panel();
private Textbox LeftSide_TextBox = new Textbox();
private Button LeftSide_Button = new Button();
private string LeftSide_Data; //store TextBox.text here
public ReturnPanel()
{
//return a Panel that Form1 can use
LeftSide_Panel.Controls.Add(LeftSide_Button);
LeftSide_Panel.Controls.Add(LeftSide_TextBox);
return this.LeftSide_Panel;
}
}
}
I understand that I can make a custom event handler for LeftSide_Button by creating a function inside LeftSide like such:
private void LeftSide_Button_Click(object sender, EventArgs e)
{
//read the textbox, assume it is valid and store the string in our LeftSide_Data
this.LeftSide_Data = this.LeftSide_TextBox.Text;
}
The above works if I wanted to keep the data local to the class, but I'm at a loss of how to expose LeftSide_Button_Click to, primarily, Form1 and sometimes to RightSide.
My issues are:
- if LeftSide owns the data that is inside its textbox (and stored in LeftSide_Data) and LeftSide_Button_Click is my custom validation event (validation is not shown above in order to simplify) how does Form1 become not only notified that the button has been clicked but is also sent the string that is inside LeftSide_Data?
- how do I expose the LeftSide event to anyone who wants to subscribe? Is it with the 'delegate' keyword or the 'event' keyword? or something else? there are too many event based keywords and thus the main source of confusion.
- should
private void LeftSide_Button_Click(object sender, EventArgs e)be changed from returning void toprivate Eventhandler LeftSide_Button_Click(object sender, EventArgs e)and if so, what am I returning... what is an EventHandler "made of"?
EDIT: I found the following diagram, and even though it does not pass data along with the event, is implementing this a step in the right direction?
