2. Use Visual C # To create a Windows Application
Creating a Windows (GUI) Application in Visual C # is much easier than creating a previous version of VC ++. The following describes how to use the Visual C # project file Wizard to create a Windows application.
Create an application framework
In VS. net ide, select "new-> project file-> Visual C # project file-> Windows application ":
Click OK to display a form design view (which is the same as VB or Delphi ). On the right side, we see a Solution Explorer ). The wizard adds a Form1.cs file to the new form, including the code for this form and all its subwindows:
Double-click Form1.cs to view the Code:
Namespace mcWinFormsApp
{
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. WinForms;
Using System. Data;
/// <Summary>
/// Summary description for Form1.
/// </Summary>
Public class Form1: System. WinForms. Form
{
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. ComponentModel. Container components;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Public override void Dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
This. components = new System. ComponentModel. Container ();
// @ This. TrayHeight = 0;
// @ This. TrayLargeIcon = false;
// @ This. TrayAutoArrange = true;
This. Text = "Form1 ";
This. AutoScaleBaseSize = new System. Drawing. Size (5, 13 );
This. Click + = new System. EventHandler (this. Form1_Click );
}
Protected void Form1_Click (object sender, System. EventArgs e)
{
}
/// <Summary>
/// The main entry point for the application.
/// </Summary>
Public static void Main (string [] args)
{
Application. Run (new Form1 ());
}
}
}
From the code above, we can see that the wizard adds a default namespace and references to different namespaces required by WinForms. The Form1 class is from System. winForms. the InitializeComponent method is used to initialize (create) the Form and its controls (more details can be seen when some controls are put down in the Form ); the Dispose method is used to clear all resources that are no longer in use.
Add controls
To add a control or subwindow to a form, you must open the ToolBox. The concept of this toolbox comes from VB. Click "View-> toolbox" in the menu to activate the Toolbox function:
Shows the ToolBox window. Now you can add the control. The method is the same as that of the previous version of Visual Studio. You can drag and drop the control or double-click the control.
First, place a button and an edit box on the form, and then let's see what the system adds to the initial component (InitializeComponent.
Then, set the properties of the control in the Properties window, which is the same as that in VB. Right-click the control and click "properties" to bring up the Property Window.
Now let's take a look at the InitializeComponent method and we will find that the code has been added to it. Then manually modify the code:
This. components = new System. ComponentModel. Container ();
This. button1 = new System. WinForms. Button ();
This. textBox1 = new System. WinForms. TextBox ();
// @ This. TrayHeight = 0;
// @ This. TrayLargeIcon = false;
// @ This. TrayAutoArrange = true;
Button1.Location = new System. Drawing. Point (16, 24 );
Button1.Size = new System. Drawing. Size (88, 32 );
Button1.TabIndex = 0;
Button1.Text = "Browse ";
Button1.Click + = new System. EventHandler (this. button#click );
TextBox1.Location = new System. Drawing. Point (128, 32 );
TextBox1.Text = "textBox1 ";
TextBox1.TabIndex = 1;
TextBox1.Size = new System. Drawing. Size (144, 20 );
This. Text = "Form1 ";
This. AutoScaleBaseSize = new System. Drawing. Size (5, 13 );
This. Click + = new System. EventHandler (this. Form1_Click );
This. Controls. Add (this. textBox1 );
This. Controls. Add (this. button1 );
Add event Processor
Finally, add an event processor for the button to browse the file. Double-click the button to open the button#click event processor. Similarly, you can use the same method to write event processors for any controls.
Protected void button#click (object sender, System. EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog ();
Fdlg. Title = "C # Corner Open File Dialog ";
Fdlg. InitialDirectory = @ "c :";
Fdlg. Filter = "All files (*. *) | *. * | All files (*. *) | *.*";
Fdlg. FilterIndex = 2;
Fdlg. RestoreDirectory = true;
If (fdlg. ShowDialog () = DialogResult. OK)
{
TextBox1.Text = fdlg. FileName;
}
}
At this point, all the steps are completed, and the rest is to run the program. It allows you to browse a file and then load the selected file name into the text box. Download the relevant code:WinFormApp.zip.