C#. Windows Forms. The Label component. Programmatically create a Label control. MessageBox class

The Label component. Programmatic creation of the Label component. The MessageBox class. The DialogResult enum


Search other resources:

1. Component Label. Properties of Label component

A component of type Label represents static text on the monitor. Figure 1 shows the placement of the Label component on the Toolbox in Microsoft Visual Studio 2022.

C#. Windows Forms. Component Label. Placement of the Label component on the form

Figure 1. Component Label. Placement of the Label component on the form

The main properties of the Label component are listed below.

Properties Description
Name The name of an instance (object) of the Label type
Anchor Specifies the edges of the container to which a particular control is bound. When a control is bound to an edge, the distance between the nearest edge of the control and the specified edge will remain constant.
AutoEllipsis If AutoEllipsis = True, then the automatic processing of text that goes beyond the width of the label is enabled (Figure 2). This property is only effective if the AutoSize property is set to False.

C#. Windows Forms. Component Label. Effect of the AutoEllipsis property on the appearance of the text

Figure 2. Effect of the AutoEllipsis property on the appearance of the text “Hello world!”: a) AutoEllipsis = True; b) AutoEllipsis = False

AutoSize If AutoSize=True, the width of the Label type component automatically changes depending on the font size (Figure 3). If AutoSize=False, you can set fixed sizes of the borders of the Label element, in which the text will fit.

C#. Windows Forms. Effect of the AutoSize property on the text appearance

Figure 3. Effect of the AutoSize property on the “Hello, world!” text appearance: a) AutoSize = True;     b) AutoSize = False

BackColor The background color of the area in which the Label control is displayed.
BorderStyle View of the border of the rectangular area of the Label control
ContextMenuStrip A property that contains a reference to the context menu that is invoked when the right mouse button is pressed in the area of the Label control. To use the context menu, this menu must be pre-configured. The ContextMenuStrip control is used to form the context menu.
Cursor Specifies the appearance of the mouse cursor when the mouse pointer is in the window area of the Label control.
Dock Specifies which control borders are associated with the container.
Enabled Determines if the control is disabled. If Enabled = True, then the control is not disabled, for example, it can respond to mouse clicks.
Font Text font type (size, text options)
ForeColor The foreground color of the component is the color of the displayed text.
GenerateMember If GenerateMember=True, then a variable is generated for this component. The variable name is set to the value specified in the Name field. This option is required when a Label control is used in code for dynamically rendered information. If the Label control is only used to display immutable text, it’s a good idea to set GenerateMember=False to save memory. To view the effect of this property, navigate to the Form1.Designer.cs part of the form class, which displays all of the form’s controls.
Image A reference to an image that can be set for this control. The image is displayed over the text.
Location The (X, Y) coordinate of the top-left corner of the control.
Locked If Locked=True, it is not possible to change the position and size of the control on the form (the position is locked) at design time.
MaximumSize Sets the maximum possible size (Width, Height) of the control. If MaximumSize=(0, 0), then the control can take on an arbitrary size.
MinimumSize Sets the minimum possible size (Width, Height) of the control. If MinimumSize=(0, 0), then the control can take on an arbitrary size.
Modifiers Specifies an access modifier for a variable (instance) of type Label, provided that GenerateMember=True.
Size Element size in pixels.
TabIndex Ordinal number in changing tab stops using the TAB key.
Text The text associated with the control.
TextAlign Specifies the position of text within the area that is intended to display the Label control. This option makes sense if AutoSize=false.
UseWaitCursor If UseWaitCursor=True then property Cursor=WaitCursor
Visible If Visible=False, then the control is invisible on the form


2. Events of the Label component

The events that the Label component responds to are described in the following table:

Property Description
Click Clicking with the mouse on the Label control
DoubleClick Double click on the Label control
MouseClick Click with the mouse on the Label control. Unlike the Click event, it allows you to get more complete information about the click: which mouse button is pressed, the coordinate (x, y) of the click, and so on. All information is obtained from the MouseEventArgs class.
MouseDoubleClick Double click on the element. Compared to DoubleClick, it adds an instance of the MouseEventArgs class containing all the necessary information about the click made (coordinate (x, y), which key was pressed, etc.).
Paint Used to draw graphics on the area of the control.


3. Programmatic creation of the Label control. Example

You need to place a button on the form, for example button1, and in the Click handler of this button, type the following text:


private void button1_Click(object sender, EventArgs e)
  // Programmatically creating a Label control
  // 1. Declare an instance of Label type
  Label myLabel = new Label();

  // 2. Set parent class,
  //   in our case, the parent class is Form.
  //   The Parent property is of type Control
  myLabel.Parent = this.Parent;

  // 3. Set the text of the label
  myLabel.Text = "Hello!";

  // 4. Set the placement on the form at point (10, 80)
  myLabel.Location = new Point(10, 80);

  // 5. Property AutoSize
  myLabel.AutoSize = true;

  // 6. Set the instance name
  myLabel.Name = "myLabel";

  // 7. Add to the Controls property of the Form class.
  //    The Form class inherits this property from the Control class.
  //    The type of property Controls - ControlCollection,
  //    that is, the ControlCollection class is defined in the Control class.
  //    The Controls property is a set of child controls.


After starting the program and clicking on the button, the Label label will appear.


4. Class MessageBox. The purpose. Example

The static MessageBox class represents a dialog box in which information can be displayed (Figure 4). The main class method that displays a dialog box is the Show() method, which has many overloaded implementations.

C#. Windows Forms. The window that is opened as a result of calling the Show() method of the MessageBox class

Figure 4. The window that is opened as a result of calling the Show() method of the MessageBox class (one of the cases)

Using the class is effective when you need to display quick messages on the fly with a basic set of standard buttons and their fast processing. Thus, there is no need to create unnecessary small dialogue forms that complicate the perception of the structure of the entire project.

To demonstrate the use of the MessageBox class, you need to place a button on the form (for example, button2) and program a click event handler on this button.


private void button2_Click(object sender, EventArgs e)
  // MessageBox class - represents a dialog box
  // that displays known response confirmation messages, such as Ok, No, etc.
  // 1. Calling a simple window with text
  MessageBox.Show("This is a message");

  // 2. Call another window with title
  MessageBox.Show("This is a message", "This is a caption");

  // 3. Call window with buttons Yes, No, Cancel
  // 3.1. Create buttons Yes, No, Cancel
  MessageBoxButtons mbb;
  mbb = MessageBoxButtons.YesNoCancel;

  // 3.2. Display a window and check the user's response
  DialogResult dr;
  dr = MessageBox.Show("This is YesNoCancel message.", "Title", mbb);

  if (dr == DialogResult.Yes)
    MessageBox.Show("Your choice: Yes");
  if (dr == DialogResult.No)
    MessageBox.Show("Your choice: No");
    MessageBox.Show("Your choice: Cancel");

  // 3.3. Display a window with the title and check the answer is another option
  mbb = MessageBoxButtons.OKCancel;
  dr = MessageBox.Show("Please, select OK or Cancel", "OK-Cancel window", mbb);

  if (dr == DialogResult.OK)
    MessageBox.Show("You select OK");
    MessageBox.Show("You select Cancel");


As you can see from the example above, the methods of the MessageBox class are combined with the use of the DialogResult enum. This enumeration declares constants corresponding to the codes of the basic standard buttons used in the Windows interface. For example, the buttons Ok, Cancel, Yes and others are described.


Related topics