To get to the point, let's talk about how to dynamically register button events.
First, we need to set the variable to get the value of clicking on a number button to calculate the final result in the "=" button click event. Here's a piece of code I wrote at the beginning:
Copy Code code as follows:
Public double? value1 = null;//Get the numeric value before the operator
Public double? value2 = null;//Get the numeric value before the operator
Public type Caltype=type. none;//Get operator
private void Btnvalue1_click (object sender, EventArgs e)
{
if (value1 = null)
{
value1 = Convert.todouble (btnvalue1. Text);
}
Else
{
value2 = Convert.todouble (btnvalue1. Text);
}
This.txtvalue.Text = btnvalue1. Text;
The Click event for the "1" button
A total of 10 such numeric key button click events
Do not think this is difficult to write, so there is a button registration event. Here's a piece of code that follows me:
Copy Code code as follows:
Public double? value1 = null;//Get the numeric value before the operator
Public double? value2 = null;//Get the numeric value after the operator
Public type Caltype=type. none;//Get operator
private void Form1_Load (object sender, EventArgs e)
{
Btnvalue0. Click + + new EventHandler (Btnvalue_click);
Btnvalue1. Click + + new EventHandler (Btnvalue_click);
Btnvalue2. Click + + new EventHandler (Btnvalue_click);
Btnvalue3. Click + + new EventHandler (Btnvalue_click);
Btnvalue4. Click + + new EventHandler (Btnvalue_click);
Btnvalue5. Click + + new EventHandler (Btnvalue_click);
Btnvalue6. Click + + new EventHandler (Btnvalue_click);
Btnvalue7. Click + + new EventHandler (Btnvalue_click);
Btnvalue8. Click + + new EventHandler (Btnvalue_click);
Btnvalue9. Click + + new EventHandler (Btnvalue_click);
by EventHandler the Click event of the button control
Bind to Btnvalue_click to dynamically register button events
}
private void Btnvalue_click (object sender, EventArgs e)
{
Button btn = (button) sender;
Instantiate the button to get the value of the current click button
if (value1 = null)
{
value1 = Convert.todouble (btn. Text);
}
Else
{
value2 = Convert.todouble (btn. Text);
}
This.txtvalue.Text = btn. Text;
}
Is it easier to feel than before ... (To source words please contact me qq:342468914)