Library sample chapters
Introduction to Windows Forms
The Hello World Application
Listing 3.1.1 is a simple Windows Forms application that displays the text "Hello Windows Forms!" in a label on the main form.
Listing 3.1.1 HWF.cs: The Hello Windows Forms Application
// HWF.cs
namespace HelloWindowsFormsNamespace {
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
public class HelloWindowsForms : System.WinForms.Form
{
//Label 1 displays the text message "Hello Windows Forms!"
private System.WinForms.Label label1;
//The constructor is where all initialization happens.
//Forms created with the designer have an InitializeComponent() method.
public HelloWindowsForms()
{
this.label1 = new System.WinForms.Label();
label1.Location = new System.Drawing.Point(8, 8);
label1.Text = "Hello Windows Forms!";
label1.Size = new System.Drawing.Size(408, 48);
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24f);
label1.TabIndex = 0;
label1.TextAlign = System.WinForms.HorizontalAlignment.Center;
this.Text = "Hello World";
this.MaximizeBox = false;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BorderStyle = System.WinForms.FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.ClientSize = new System.Drawing.Size(426, 55);
this.Controls.Add(label1);
}
// This main function instantiates a new form and runs it.
public static void Main(string[] args)
{
Application.Run(new HelloWindowsForms());
}
}
} // end of namespace
You can see that the class HelloWindowsForms contains a simple label component, label1. It has only two methods: a static Main method that creates and runs an instance of the HelloWindowsForms class and a constructor in which all of the component initialization occurs.
This shows the minimum functionality and minimum complication of the Windows Forms system. Programs designed and maintained by VS.NET or WinDes.exe have some added functions and data members that detract from the simplicity of this example, but are necessary for the RAD environments to keep track of the form design.
To build the executable, we have prepared a small batch file that creates a Windows executable and references all the correct library DLLs. Using this batch file will allow you to stay away from the Visual Studio build environment for a while—because it hides too much of the important information—and concentrate on the actual functionality. Listing 3.1.2 shows build.bat.
Listing 3.1.2 build.bat
csc /t:winexe /r:Microsoft.win32.Interop.dll, system.dll,
system.configuration.dll, system.data.dll,
system.diagnostics.dll, system.drawing.dll,
system.winforms.dll /out:%1.exe %1.cs
%2 %3 %4 %5 %6 %7 %8 %9
build.bat is available with the software that accompanies this book, but if you want to type it in yourself, make sure that it's all on the same line and there are no spaces between the libraries named in the /r: reference directive. To use build.bat, simply type build followed by the name of the .cs file to compile. For example
build HWF
will compile the HWF.cs file and create HWF.exe.
Running the hwf.exe program will produce a form shown in Figure 3.1.1.
Figure 3.1.1
The Hello Windows Forms Application.
Related articles
Related discussion
-
looking for help on asp
by cladironbeard (2 replies)
-
Socket Programming in C# - Part 1
by graumanoz (23 replies)
-
LINQ in Action
by naser1 (0 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
-
simple vb to c#, help please
by lksath (1 replies)
Events coming up
-
Aug
28
St. Louis Day of .NET
St. Charles, United States
Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend
Actually all you have to do in place an ampersand before the letter you want underlined.
So if you want the "r" in "Print" underlined, enter in the code:
"P&rint"
That's it.
if you want to view the _ all the time for alt shortcuts you have to go to:
display properties->appearance->effects
and uncheck the "Hide underlined charactors for keyboard navigation until I press the ALT key" checkbox.
these instructions are for XP SP2 Pro build 2600
The "" on the short-cut character is visible only when "ALT" is pressed. Is it possible to display the "" programatically???
This thread is for discussions of Introduction to Windows Forms.