C#. Windows Forms. Investigation of MouseDown, MouseUp events

Investigation of MouseDown, MouseUp events. Programmatic formation of the Label component on the form. Example


Contents


Search other resources:

Task

Develop an application that creates a Label control on a form. The user clicks the left mouse button on the form without releasing it, makes a movement on the form and releases the button. Thus, the user forms a conditional rectangle based on the coordinates of pressing and releasing the mouse button. When the mouse button is released, a control of type Label should be created, which is placed in a rectangular area formed by a click based on the corners of the rectangle.

 

Theoretical information

To solve the problem it is necessary:

  • at the moment of pressing the mouse button on the form, fix the coordinates of the point where the click occurred (x1; y1). For this, the MouseDown event is programmed;
  • at the moment of releasing the mouse button, fix the coordinates of the release point (x2; y2). This is where the MouseUp event is programmed.

For each of the MouseDown and MouseUp events, the system creates an appropriate program code – an event handler. In this handler, an instance of the MouseEventArgs class is available, which implements the following main fields and properties:

  • an enumeration of type MouseButtons. Using this enumeration, you can determine which mouse button is pressed: Left, Right, Middle, etc.;
  • Clicks property – number of mouse button clicks;
  • X, Y – coordinates where the mouse button was pressed;
  • others.

 

Solution

1. Creating a project

Create a project from a Windows Forms template. More details about the features of creating a Windows Forms project are described here.

After creation, the main form of the application will be displayed, which has the name Form1. There are no components on the form.

 

2. Declaration of internal variables of the form class

The main form corresponds to a class named Form1. In this class, you need to declare 4 variables that will correspond to the coordinates of the rectangle created by the user.

 

...

public partial class Form1 : Form
{
  // Rectangle coordinates
  int x1, y1, x2, y2;

  ...

}

...

 

3. Programming the MouseDown event of a form

The MouseDown event of the main form Form1 occurs when user presses the left mouse button. Figure 1 illustrates this event.

C#. Windows Forms. The MouseDown event of the main form Form1

Figure 1. The MouseDown event of the main form Form1

The Form1_MouseDown() event handler remembers the coordinates of the position at the moment when the mouse button was pressed

// Mouse button click event handler
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
  // Save click coordinates - use the MouseEventArgs class
  x1 = e.X;
  y1 = e.Y;
}

 

4. Programming the MouseUp event of the main form

The MouseUp event can be programmed for many controls (component). This also applies to Form1. Figure 2 shows the location of the MouseUp event in the Properties window of the Events tab.

C#. Windows Forms. The MouseUp event of Form1

Figure 2. The MouseUp event of Form1

To solve our problem, we need to create a Label type component in the MouseUp event handler, set its properties and place it on the form. The rectangular area occupied by the component is determined by the coordinates of the corners formed by the MouseDown (x1, y1 coordinate) and MoseUp (x2, y2 coordinate) event handlers.

According to the above, the text of the Form1_MouseUp() handler is as follows

// Mouse button release event handler
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
  // 1. Declare a reference to a control of type Label
  System.Windows.Forms.Label myLabel;

  // 2. Get coordinates (x2, y2)
  x2 = e.X; y2 = e.Y;

  // 3. Normalize coordinates - bring to specific values:
  //   x1 must be less than x2; y1 must be less than y2.
  if (x1 > x2)
  {
    // Swap x1 and x2
    int t = x2; x2 = x1; x1 = t;
  }

  if (y1 > y2)
  {
    // Swap y1 and y2
    int t = y2; y2 = y1; y1 = t;
  }

  // 4. Create a Label control
  // 4.1. Allocate memory for instance
  myLabel = new Label();

  // 4.2. Setting properties responsible for the size and position of the label
  myLabel.AutoSize = false; // size must be fixed
  myLabel.Location = new System.Drawing.Point(x1, y1);
  myLabel.Size = new System.Drawing.Size(Math.Abs(x1 - x2), Math.Abs(y1 - y2));

  // 4.3. Set other properties
  myLabel.Name = "myLabel";
  myLabel.BorderStyle = BorderStyle.FixedSingle;
  myLabel.Text = "This is myLabel";
  myLabel.TabIndex = this.Controls.Count; // tab position

  // 4.4. Add a new component to the form's list,
  //      this.Controls - collection of components placed on the form
  this.Controls.Add(myLabel);
}

Now, after releasing the mouse button (any key), a Label type component will be created.

 


Related topics