C#. Windows Forms. Practice. An example of creating a pentagonal form




An example of creating a pentagonal form. Windows Forms application

According to this example, you can learn how to develop a Windows Forms application, the main form of which has a different appearance.


Contents


Search other websites:

Task

Develop a Windows Forms application in which the main form is pentagonal. Place a button to close the form on the form. Recommended coordinates in pixels (x; y) of the pentagon of the shape: (0; 150), (50; 300), (420; 300), (650; 150), (400; 0).

 

Instruction

1. Create a new Windows Forms project

The process of creating a new form is described in more detail here. Once created, the default name of the form is Form1.

 

2. Customizing Form1

In the Properties window, you need to configure the following properties of Form1:

  • property FormBorderStyle = None;
  • property BackColor = Blue (Figure 1). To find the color Blue, go to the Web tab.

C#. Windows Forms application. Setting the BackColor property

Figure 1. Setting the BackColor property

 

3. Place and configure the Close button on the form

Use the Toolbox to place a button on the form. As a result, the system will create a button component named button1. For button1, the following properties must be set:

  • Text = “Close”;
  • Location = (150, 200).

 

4. Programming the event handler for clicking on the button button1

To program an event, you need to perform the following actions (Figure 2):

  • select the button1 (Close) button;
  • in the Properties window goto Events tab;
  • In the list of event handlers, double-click in the Click event area. This will open the code for this event handler. Another way to call the Click event handler code is to double-click on the button that is placed on the form.

C#. Windows Forms. The Click event of the button1 component in the Properties window

Figure 2. The Click event of the button1 component in the Properties window

Add the following code to the event handler text

this.Close();

The above line uses this to access an instance of form Form1. The Close() method of the form closes the form, so that the program exits.



 

5. Programming the event handler Form_Load()

In accordance with the condition of the task, it is necessary that after starting the form is displayed in the form of a pentagon. To do this, you need to program the Load event handler of form Form1. The Load event is raised at the beginning of program execution when the form appears on the screen (loading).

To call the Load event handler, perform the following actions (Figure 3):

  • use the mouse to activate Form1;
  • in the Properties window, in the Events tab of the events, double-click the mouse in the Load event field. As a result, the Form1_Load() event handler will be displayed.

C#. Windows Forms App. Event Load of Form

Figure 3. Event Load of Form1 form

In the Load() event handler, enter the following code

 

// Event handler Form1_Load()
private void Form1_Load(object sender, EventArgs e)
{
  // 1. Declare an instance of type GraphicsPath - a class
  //   of the .NET Framework library that describes a sequence
  //   of connected lines and curves
  System.Drawing.Drawing2D.GraphicsPath gp = new
    System.Drawing.Drawing2D.GraphicsPath();

  // 2. Create an array of points corresponding
  //   to the coordinates of the pentagon forming the form.
  // 2.1. Declare an instance of type "array of points Point[]".
  //      Point is a class that describes a point on the screen.
  Point[] mp = new Point[5];

  // 2.2. Allocate memory for each point and fill it values
  mp[0] = new Point(0, 150);
  mp[1] = new Point(50, 300);
  mp[2] = new Point(420, 300);
  mp[3] = new Point(650, 150);
  mp[4] = new Point(400, 0);

  // 3. Add array of point Point[] to the instance gp
  gp.AddPolygon(mp);

  // 4. Create a Region based on a sequence of points gp
  Region rg = new Region(gp);

  // 5. Set this.Region form region to a new value rg
  this.Region = rg;
}

 

6. Run the program. Results

The result of the program is shown in Figure 4.

C#. Windows Forms application. Pentagonal form

Figure 4. Pentagonal form

 


Related topics