A processing program for custom events

Source: Internet
Author: User

 

For example, this example is written in accordance with "C # practical program design tutorial" (Tang Yao,

Set the Tag attribute of each button to 0, 1, 2, 3, 4,-1.

The Code is as follows.

Create two classes. One is the event class and the other is the Break class.

The Code is as follows:

// Break. cs

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace WinFormEvent
{

Class Break
{
Public delegate void BreakHandler (object sender, BreakArgs e );
Public event BreakHandler changeBreak;

Public void onChangeBreak (object sender, BreakArgs e)
{
If (changeBreak! = Null)
{
ChangeBreak (sender, e );
}
}

}
}

// BreakArgs. cs

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace WinFormEvent
{
Class BreakArgs: EventArgs
{
Private int I;

Public int Level
{
Get
{
Return I;
}
Set
{
I = value;
}
}

}
}

// Form1.cs

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Namespace WinFormEvent
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
Button1. Click + = new System. EventHandler (this. btnStart_Click );
Button2.Click + = new System. EventHandler (this. btnStart_Click );
Button3.Click + = new System. EventHandler (this. btnStart_Click );
Button4.Click + = new System. EventHandler (this. btnStart_Click );
BtnBack. Click + = new System. EventHandler (this. btnStart_Click );
}

Private void btnStart_Click (object sender, EventArgs e)
{
Break B = new Break ();
B. changeBreak + = new WinFormEvent. Break. BreakHandler (B _changeBreak );
BreakArgs ee = new BreakArgs ();
Switch (Convert. ToInt32 (Button) sender). Tag ))
{
Case 0:
Ee. Level = 0;
Break;
Case 1:
Ee. Level = 1;
Break;
Case 2:
Ee. Level = 2;
Break;
Case 3:
Ee. Level = 3;
Break;
Case 4:
Ee. Level = 4;
Break;
Case-1:
Ee. Level =-1;
Break;
}
B. onChangeBreak (sender, ee );
}

Void B _changeBreak (object sender, BreakArgs e)
{
Switch (e. Level)
{
Case 0:
Label1.Text = "start ";
Break;
Case 1:
Label1.Text = "Level 1 ";
Break;
Case 2:
Label1.Text = "Level 2 ";
Break;
Case 3:
Label1.Text = "Level 3 ";
Break;
Case 4:
Label1.Text = "file 4 ";
Break;
Case-1:
Label1.Text = "Inverted ";
Break;
}
// Throw new Exception ("The method or operation is not implemented .");
}

}
}

// Form1.Designer. cs

Namespace WinFormEvent
{
Partial class Form1
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. IContainer components = null;

/// <Summary>
/// Clear all resources in use.
/// </Summary>
/// <Param name = "disposing"> If the managed resource should be released, the value is true; otherwise, the value is false. </Param>
Protected override void Dispose (bool disposing)
{
If (disposing & (components! = Null ))
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer

/// <Summary>
/// The designer supports the required methods-do not
/// Use the code editor to modify the content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. label1 = new System. Windows. Forms. Label ();
This. groupBox1 = new System. Windows. Forms. GroupBox ();
This. btnStart = new System. Windows. Forms. Button ();
This. button2 = new System. Windows. Forms. Button ();
This. button3 = new System. Windows. Forms. Button ();
This. button4 = new System. Windows. Forms. Button ();
This. button1 = new System. Windows. Forms. Button ();
This. btnBack = new System. Windows. Forms. Button ();
This. groupBox1.SuspendLayout ();
This. SuspendLayout ();
//
// Label1
//
This. label1.AutoSize = true;
This. label1.Location = new System. Drawing. Point (45, 28 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (41, 12 );
This. label1.TabIndex = 0;
This. label1.Text = "label1 ";
//
// GroupBox1
//
This. groupBox1.Controls. Add (this. btnBack );
This. groupBox1.Controls. Add (this. button1 );
This. groupBox1.Controls. Add (this. button4 );
This. groupBox1.Controls. Add (this. button3 );
This. groupBox1.Controls. Add (this. button2 );
This. groupBox1.Controls. Add (this. btnStart );
This. groupBox1.Location = new System. Drawing. Point (31, 86 );
This. groupBox1.Name = "groupBox1 ";
This. groupBox1.Size = new System. Drawing. Size (589, 94 );
This. groupBox1.TabIndex = 1;
This. groupBox1.TabStop = false;
This. groupBox1.Text = "downloading ";
//
// BtnStart
//
This. btnStart. Location = new System. Drawing. Point (16, 39 );
This. btnStart. Name = "btnStart ";
This. btnStart. Size = new System. Drawing. Size (75, 23 );
This. btnStart. TabIndex = 0;
This. btnStart. Tag = "0 ";
This. btnStart. Text = "start ";
This. btnStart. UseVisualStyleBackColor = true;
This. btnStart. Click + = new System. EventHandler (this. btnStart_Click );
//
// Button2
//
This. button2.Location = new System. Drawing. Point (203, 38 );
This. button2.Name = "button2 ";
This. button2.Size = new System. Drawing. Size (75, 23 );
This. button2.TabIndex = 1;
This. button2.Tag = "2 ";
This. button2.Text = "2 ";
This. button2.UseVisualStyleBackColor = true;
//
// Button3
//
This. button3.Location = new System. Drawing. Point (294, 38 );
This. button3.Name = "button3 ";
This. button3.Size = new System. Drawing. Size (75, 23 );
This. button3.TabIndex = 2;
This. button3.Tag = "3 ";
This. button3.Text = "3 ";
This. button3.UseVisualStyleBackColor = true;
//
// Button4
//
This. button4.Location = new System. Drawing. Point (386, 38 );
This. button4.Name = "button4 ";
This. button4.Size = new System. Drawing. Size (75, 23 );
This. button4.TabIndex = 3;
This. button4.Tag = "4 ";
This. button4.Text = "4 ";
This. button4.UseVisualStyleBackColor = true;
//
// Button1
//
This. button1.Location = new System. Drawing. Point (111, 38 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (75, 23 );
This. button1.TabIndex = 4;
This. button1.Tag = "1 ";
This. button1.Text = "1 ";
This. button1.UseVisualStyleBackColor = true;
//
// BtnBack
//
This. btnBack. Location = new System. Drawing. Point (481, 38 );
This. btnBack. Name = "btnBack ";
This. btnBack. Size = new System. Drawing. Size (75, 23 );
This. btnBack. TabIndex = 5;
This. btnBack. Tag = "-1 ";
This. btnBack. Text = "reverse ";
This. btnBack. UseVisualStyleBackColor = true;
//
// Form1
//
This. AutoScaleDimensions = new System. Drawing. SizeF (6F, 12F );
This. AutoScaleMode = System. Windows. Forms. AutoScaleMode. Font;
This. ClientSize = new System. Drawing. Size (651,195 );
This. Controls. Add (this. groupBox1 );
This. Controls. Add (this. label1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. groupBox1.ResumeLayout (false );
This. ResumeLayout (false );
This. initialize mlayout ();

}

# Endregion

Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. GroupBox groupBox1;
Private System. Windows. Forms. Button btnBack;
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. Button button4;
Private System. Windows. Forms. Button button3;
Private System. Windows. Forms. Button button2;
Private System. Windows. Forms. Button btnStart;
}
}

The implementation result is as follows:

When you press any button, an event is generated, notifying the Label to display the corresponding information.

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.