About event handling (C #)

Source: Internet
Author: User
In order to use events in your application, you must provide an event handler (a method of event handling) that responds to the logic of the event execution program, and register the event handler with the event source. This process, called the event line, is described in the following sections, Web forms and Windows Forms descriptions, how to work.
1.Web Forms:
In the example, there is a butto and a textbox, when the button is clicked, the text box changes the background color.
<script language= "C #" runat=server>
private void Button_Clicked (object sender, EventArgs e) {
Box.backcolor = System.Drawing.Color.LightGreen;
}
</script>
<body>
<form method= "POST" action= "events.aspx" runat=server>
Click the button, and notice the color of the text box.<br><br>
<asp:textbox
id = "box" Text = "Hello" BackColor = "Cyan" runat=server/>
<br><br>
<asp:button
id = "button" OnClick = "button_clicked" Text = "click Me"
runat = server/>
</form>
</body>
Save, generate Events.aspx file, through, browser, you can see the effect.
The following is a brief description, in the example, of a substantial step.
1. The event source is an instance of the "System.WebForms.UI.WebControls.Button" server control.
2. button produces a click event.
The delegate for the 3.Click event is EventHandler.
4. The page has an event handler called Button_Clicked.
The 5.Click event is connected with the following syntax "OnClick =" button_clicked "and button_clicked.
For the actual web developer, it is not necessary to care about the details of the delegate, as in the example above, to simply implement the event.
2.Windows Forms
In the example, there is a butto and a textbox, when the button is clicked, the text box changes the background color.
Using System;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Drawing;
public class Myform:form
{
Private TextBox box;
Private button button;

Public MyForm (): Base ()
{
box = new TextBox ();
Box. BackColor = System.Drawing.Color.Cyan;
Box. size = new size (100,100);
Box. Location = new Point (50,50);
Box. Text = "Hello";

button = New button ();
button. Location = new Point (50,100);
button. Text = "click Me";

To connect an event, generate a delegate instance and increase it to the Click event.
button. Click + + new EventHandler (this. button_clicked);
Controls.Add (box);
Controls.Add (button);
}
Event handlers
private void Button_Clicked (object sender, EventArgs e)
{
Box. BackColor = System.Drawing.Color.Green;
}
STAThreadAttribute explains that Windows Forms uses a single-threaded suite model.
[STAThreadAttribute]
public static void Main (string[] args)
{
Application.Run (New MyForm ());
}
}
Save "Events.cs", enter at the command line, the following
Csc/r:system.dll,system.drawing.dll,system.windows.forms.dll Events.cs
Compile the build, Events.exe, execute, and see the effect.
The above example, the simple explanation. NET, the handling of events. An event must have an event source, and event data. In the example, the event data is Evengargs. It is the base class for all event data classes.
For a better understanding of the event. You can go to MSDN to see tutorials about, events, and delegates.

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.