C # in-depth understanding (4)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.