C#. Windows Forms. Component Button. Interaction between Label and Button components. The MouseMove event

Component Button. Interaction between Label and Button components. The MouseMove event

Before studying this topic, it is recommended that you familiarize yourself with the previous topic:


Contents


Search other resources:

1. General information about the Button component

The Button component is a button on the form (Figure 1).

C#. Windows Forms. Component Button. Placement in Toolbox

Figure 1. Component Button. Placement in Toolbox

The Button component is a class that inherits from the ButtonCase abstract class and implements the IButtonControl interface. In turn, the ButtonCase class is inherited from the Control class. In many .NET Framework classes, there are 3 types of buttons that inherit from ButtonCase: Button, CheckBox, RadioButton.

The Button class (component) has a set of properties available both programmatically and in design mode (Figure 2).

C#. Windows Forms. Component Button. Properties of Button component

Figure 2. Properties of Button component

Following are the main properties of the Button component.

 

2. Properties of Button component

The basic properties of the Button control are given below. Here are the basic properties that are different from the properties of the Label component. You can read more about the Label component here.

Property Description
Name This is the name of an instance of an object of type Button.
AutoSize If AutoSize=True, then the control changes its size automatically depending on the content (the text typed in the Text property).
AutoSizeMode Specifies the mode in which the Button control resizes itself. This property is only effective if AutoSize=True.
DialogResult The result of returning from a modal form that was called by the ShowDialog() method. If the project has created a new form of type Form2 containing an OK button, then calling the form and checking the return result from the form can be something like this:

// Declare an instance of type form
Form2 f2 = new Form2();

// If the return result from the form is OK, then perform some actions
if (f2.ShowDialog() == DialogResult.OK)
{
  // actions to be performed if the OK button is pressed
  // ...
}
FlatAppearance For buttons that have FlatStyle=FlatStyle.Flat, specifies the appearance of the border and the colors used for validation and mouse state.
FlatStyle Specifies the appearance of the control when the user moves the mouse over the control and clicks on it.
Padding Specifies padding from the edges of a control that defines the area of that control
Size Control size in pixels
TabIndex Tab position. This position is relevant when TabStop=True
TabStop If TabStop=True, then the control can receive input focus using the Tab key.
Text Text associated with the control
TextAlign Alignment of text displayed in a control
TextImageRelation Specifies the position of the image in relation to the button text. This property is meaningful if at least one image is loaded for the button control

 

3. Events of the Button control

The following table displays the main actions of a Button control.

Event Description
Click Click on the button
MouseClick Button click with more detailed information using MouseEventArgs class
Enter Occurs when a button receives input focus
Leave Occurs when a control (button) loses activity (focus) on a form
KeyDown Appears when a key is pressed (including the Alt and Ctrl keys). Allows you to get the code of the pressed key, described in the Keys enumeration
KeyPress Occurs when the control has focus and a key is pressed and released. Allows you to get the character code of the char type.
KeyUp Occurs when a key is released. Also, as for the KeyDown event, allows you to get the code of the pressed key, described in the Keys enumeration
PreviewKeyDown Occurs before the KeyDown event when a key is pressed and the input focus is over the button
MouseDown Occurs when the mouse pointer is over a component and the mouse button is pressed
MouseEnter Occurs when the mouse enters the visible part of the control
MouseHover Occurs when the mouse remains inside the control for a while
MouseLeave Occurs when the mouse leaves the visible part of the control
MouseMove Occurs when the mouse cursor moves over the area of a button
MouseUp Occurs when the mouse pointer is over a component and the mouse button is released

 

4. An example of using the Label and Button components. Programming the MouseMove event
4.1. Task

Develop a project containing 2 buttons (Button) and 2 labels (Label). Investigate the operation of the component by completing the following steps.

4.2. Instructions
4.2.1. Project creation

Create a project using Microsoft Visual Studio. An example of creating a project of the “C# – Windows Forms” type can be found here.

Name the project “Demo Program”. To do this, you can use the Text property of the main form Form1.

 

4.2.2. Placement of Label, Button controls. Customizing controls

Place two controls of the Label type and two controls of the Button type on the form. As a result, the system will create four instances named label1, label2, button1, button2 (Figure 3).

C#. Windows Forms. Class Form. Initial state of the form and controls on the form

Figure 3. Initial state of the form and controls on the form

Set the following properties of form controls:

  • label1.Text = “Form Size = “ (the Text property of the label1 control);
  • label2.Text = “Catch me!”;
  • button1.Text = “Form Size”;
  • button2.Text = “Hide/Show”.

After adjusting the size of some controls and settings, the form will look like shown in Figure 4.

C#. Windows Forms. Lesson. Class Button. Form view after customizing controls

Figure 4. Form view after customizing controls

 

4.2.3. Programming the Click event of the button1 component. Get the size of the form

At this stage, you need to program the Click event handler of the button1 component. More details about the features of event programming in Microsoft Visual Studio can be found here.

The text of the button1_Click() event handler is as follows:

private void button1_Click(object sender, EventArgs e)
{
  // The click event handler for the button1 button.
  // Display the dimensions of the form, use the Width, Height properties.
  label1.Text = String.Format("Form Size: W = {0}, H = {1}",
    this.Width, this.Height);
}

As you can see from the above code, by pressing the button1 button in the label1 component, information about the current size of the form is displayed. If, while running the program, you try to resize the form and press button1, the new sizes will be displayed again in label1.

 

4.2.4. Programming the Click event of the button2 component. Show or hide form components

When button2 (“Show/Hide”) is clicked, label1, label2, button2 components must be hidden or displayed. According to this, in the Click event handler of the button2 component, you must enter the following text

private void button2_Click(object sender, EventArgs e)
{
  // Show/Hide - property Visible
  button1.Visible = !button1.Visible;
  label1.Visible = !label1.Visible;
  label2.Visible = !label2.Visible;

  if (button1.Visible)
    button2.Text = "Hide";
  else
    button2.Text = "Show";
}

 

4.2.5. Programming the MouseMove event handler for the label2 component

The MouseMove event of any control occurs when the mouse moves over the rectangular area of that control. The Label type component can also program this event.

In the label2_MouseMove() event handler, the following code is entered

private void label2_MouseMove(object sender, MouseEventArgs e)
{
  // The MouseMove event for the label2 component means that the label needs to be moved

  // 1. Attempt to place area label2
  label2.Left = label2.Left + 10;
  label2.Top = label2.Top - 10;

  // 2. Checking if the label is out of shape at the X coordinate
  if ((label2.Left < -20) || (label2.Left > this.Width - 20))
  {
    // If the label has gone beyond the form along the x coordinate,
    // then change the horizontal position of label2
    label2.Left = 10;
  }

  // 3. Checking if the label is out of the form at the Y coordinate
  if ((label2.Top < -10) || (label2.Top > this.Height - 10))
  {
    // If the label is outside the form in Y coordinate,
    // then change the vertical position of label2
    label2.Top = 80;
  }
}

After starting the program for execution, it will not work to move the mouse inside the rectangular area occupied by label2.

 


Related topics