Home Course Index << Prev. Next >> PDF Version of this Page

Course API Comparison
Chapter C2: A Windows Forms Project


Copyright © by V. Miszalok, last update: 2011-03-28


For an introduction into the Windows Forms API see: Wikipedia.

Preliminaries

Guidance for Visual C# 2010 Express Edition:

1) Main Menu after start of VS 2010: Tools → Options → check lower left checkbox: Show all Settings → Projects and Solutions → Visual Studio projects location: → C:\temp

2) Main Menu after start of VS 2010: File → New Project... → Visual Studio installed templates: Windows Forms Application
Name: compareWinForm
→ OK
Form1.cs[Design] appears.

3) Two superfluous files must be deleted: Form1.Designer.cs and Program.cs.
You reach these files via the Solution Explorer - compareWinForm-window: Click the plus-sign in front of branch compareWinForm and the plus-sign in front of branch Form1.cs.
Right-click the branch Program.cs. A context menu opens. Click Delete. A message box appears: 'Program.cs' will be deleted permanently. Quit with OK.
Right-click the branch Form1.Designer.cs and delete this file too.

4) Right-click the gray window Form1. A small context menu opens. Click View Code.
You see now the preprogrammed code of Form1.cs. Erase this code completely.

 

Form1.cs

Write the following lines into the empty window of Form1.cs:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{ [STAThread] static void Main() { Application.Run( new Form1() ); }
  Button button1  = new Button();
  Button button2  = new Button();
  Label  label    = new Label();
  Pen    blackPen = new Pen( Color.Black, 2 );
  public Form1()
  { BackColor  = Color.White;
    Text       = "Windows Forms Program in C#";
    Controls.Add( button1 );
    Controls.Add( button2 );
    Controls.Add( label );
    button1.Text = " Talk! "; button1.BackColor = Color.Gray;
    button2.Text = " Clear "; button2.BackColor = Color.Gray;
    button1.Click += new EventHandler( button1Click );
    button2.Click += new EventHandler( button2Click );
    label.BackColor = Color.LightGray;
    label.TextAlign = ContentAlignment.MiddleCenter;
    ResizeRedraw = true;
    Width = 600; Height = 80;
  }
  protected override void OnPaint( PaintEventArgs e )
  { Rectangle cr   = ClientRectangle;
    Font      font = new Font( "Arial", Math.Max(1f, cr.Width/40f) );
    button1.Font = button2.Font = label.Font = font;
    Graphics g = CreateGraphics();
    SizeF buttonTextSize = g.MeasureString( button2.Text, font );
    button1.Left   = 4;
    button1.Width  = Convert.ToInt32( buttonTextSize.Width * 1.2f );
    button1.Height = Convert.ToInt32( buttonTextSize.Height +  4f );
    button1.Top    = Convert.ToInt32( (cr.Height-button1.Height)/2f );
    button2.Left   = cr.Width - button1.Width - 4;
    button2.Width  = button1.Width;
    button2.Height = button1.Height;
    button2.Top    = button1.Top;
    label.Left     = button1.Left + button1.Width + 2;
    label.Width    = cr.Width - button1.Width - button2.Width - 12;
    label.Height   = button1.Height;
    label.Top      = button1.Top;
    Rectangle border = new Rectangle( 1, button1.Top-3, cr.Width-2, button1.Height+6 );
    g.Clear( BackColor );
    g.DrawRectangle( blackPen, border );
  }
  protected void button1Click( object sender, EventArgs e )
  { label.Text = "Windows Forms Program in C#. Resize!";
  }
  protected void button2Click( object sender, EventArgs e )
  { label.Text = "";
  }
}

Click Debug → Start Without Debugging Ctrl F5.
Click the Talk!-button and resize the window.
Another sample for a resizable WindowsForms application can be found in chapter 5 of Course 2D Computer Graphics with C#.NET: C5:. Controls, GUI Programming.

top of page: