A more effective C # method to dynamically Register button events _ Practical Tips

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.