-
- 2.1. Partial Classes
- 2.2. Properties
-
- 4.1. Mouse Events
- 4.2. Keyboard Events
Assignment
- Create a new project with the name “MouseEvents”
- Change the name of the default form to “MainForm”
It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
Assignment
- Open the “*.Designer.cs” file associated to the form
Frequently used properties:
- Design: Name, etc.
- Appearence: Text, ForeColor, BackColor, Font, etc.
Assignment
Sample Code available – Check the “SimpleWebBrowser” Sample
-
Create a setup file for the application, that can be redistributed to other users
You need to right click on the project in the “Solution Explorer” window and choose the publish option
Event | Event argument | Description |
---|---|---|
Click | EventArgs | Raised when the control is clicked. |
DoubleClick | EventArgs | Raised when the control is double-clicked. |
MouseEnter | EventArgs | Raised when the mouse cursor enters the control. |
MouseHover | EventArgs | Raised when the mouse cursor hovers over the control. |
MouseLeave | EventArgs | Raised when the mouse cursor leaves the control. |
MouseDown | MouseEventArgs | Raised when the mouse cursor is over the control and a mouse button is pressed. |
MouseMove | MouseEventArgs | Raised when the mouse cursor is moved over the control. |
MouseWheel | MouseEventArgs | Raised when the control has focus and the mouse wheel is rotated. |
MouseUp | MouseEventArgs | Raised when the mouse cursor is over the control and a mouse button is released. |
Click events are logically higher-level events of a control. They are often raised by other actions, such as pressing the ENTER key when the control has focus.
Depressing a mouse button when the cursor is over a control typically raises the following series of events from the control:
- MouseDown event
- Click event
- MouseClick event
- MouseUp event
Assignment
Sample code available – Check the “MouseEvents” Sample
-
Add the following code
public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void btnReset_Click(object sender, EventArgs e) { lbl.Text = ""; txt.Text = ""; } //similar: lbl_MouseHover, lbl_MouseLeave, lbl_Click, lbl_DoubleClick private void lbl_MouseEnter(object sender, EventArgs e) { lbl.Text = "MouseEnter"; TextBoxDraw("###Label MouseEnter"); } //similar: lbl_MouseMove, lbl_MouseUp, lbl_MouseWheel private void lbl_MouseDown(object sender, MouseEventArgs e) { lbl.Text = "MouseDown"; var str = "###Label MouseDown"; str += Environment.NewLine + "Button: " + e.Button; str += Environment.NewLine + "Clicks: " + e.Clicks; str += Environment.NewLine + "Delta: " + e.Delta; str += Environment.NewLine + "X: " + e.X; str += Environment.NewLine + "Y: " + e.Y; TextBoxDraw(str); } //similar: OnMouseHover, OnMouseLeave protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); TextBoxDraw("###Form MouseEnter"); } private void TextBoxDraw(string str) { txt.AppendText(Environment.NewLine + str); } }
Event | Event argument | Description |
---|---|---|
KeyDown | KeyEventArgs | Raised when a key is pressed. The KeyDown event occurs prior to the KeyPress event. |
KeyPress | KeyPressEventArgs | Raised when a character generating key is pressed. The KeyPress event occurs after the KeyDown event and before the KeyUp event. |
KeyUp | KeyEventArgs | Raised when a key is released. |
Assignment
We want to create a numeric only TextBox that can be used to build a simple calculator application, such as the one below.
Sample code available – Check the “KeyEvents” Sample
-
Create a new project with the name “KeyEventsNumericTextBox”
-
Add a TextBox and name it “tbNumericTextBox”
-
Add the following event handler at the end of this document
-
Change the previous event handler in order to allow the use of the “BackSpace” key
-
Change the previous event handler in order to allow the digit separator for the current culture
private void tbNumericTextBox_KeyPress(object sender, KeyPressEventArgs e) { OnKeyPress(e); if (!char.IsDigit(e.KeyChar)) { // Consume this invalid key e.Handled = true; } }