VC # Getting Started: The first Windows program

Source: Internet
Author: User

Preface

C # is a modern programming language, which is included in Visual Studio. Net 7.0. It simplifies the complexity of C ++ and maintains flexibility. It is easy for people who have learned C ++. Visual Studio. net 7.0 2003 was released at the time of writing this article. This article provides some simple development attempts for this version.

  1. Create a project

In the File menu of the integrated development environment, select New-> Project to open the New Project dialog box:


Select Visual C # Projects as the project type, select Windows Application in the template, enter the project Name HelloWindows in Name, and enter the saved path in Location.


After confirmation, the system automatically generates a program framework for you. Now let's get familiar with this development environment.


This is our development environment!

Zone A is our Toolbox, including support for databases, components, form controls, etc. We can select and add it to form. If you cannot find Toolbox, select Toolbox in the menu View to open it.

Zone B is our design workspace (including the design of the interface and code). The figure shows the main window of the application we just created: Form1.

Zone C is equivalent to our workspace in VC6: Solution Explorer can be regarded as the previous FileView. ResourceView and ClassView must be familiar to everyone. Do you see Form1.cs in Solution Explorer? This is the C # file corresponding to Form1. Double-click it to design the Form1 interface. Right-click Form1.cs and select View Code from the pop-up menu to View the corresponding Code.

Zone D is the attribute window: similar to the attribute window in the resource editor in VC6, it is more powerful, you can directly modify the attributes of components such as buttons and list boxes on the screen, such as text and background color.

The next task is to create a dialog box and add a menu to open this dialog box.



   2. Add a new Form: Dialog Box

Select Project> Add Windows Form. In the displayed dialog box, select Local Project Items as the category, select Windows Form as the template, and enter the file Name AboutDlg. cs in Name to confirm. The new Form appears in the workspace. In the Properties dialog box of Area D, Modify Text to "about" and select BackColor to light blue. You can also change other attributes.

Next, we open Toolbox, select the Label in Win Forms, and draw it in the form. In the Properties dialog box, Modify Text to "VC knowledge base. Welcome! Http://www.vckbase.com ", we add a Button in Win Forms and change Text to" OK ".

Now, we have designed the dialog box interface, but how can we close the dialog box when the user presses the OK button?
Double-click "OK". The system will automatically add the processing code for the button. In this case, we add the Close () function to Close the code, as shown below:

Protected void button#click (object sender, System. EventArgs e)
{
Close (); // This is an added sentence.
}

The dialog box has been completed. Next we need to add a menu for the main window. When you select the menu, we will pop up the "about dialog box". Let's continue...

   3. Add a menu for the Main Window

In SolutionView, double-click Form1.cs to open Form1, select MainMenu in Toolbox> Win Forms, and draw it in Form1. In the word "TypeHere", we can enter a menu bar ,:


Double-click "About". The system will add the processing code for this menu bar. You can add the code to open the "about dialog box:

Protected void button#click (object sender, System. EventArgs e)
{
AboutDlg dlg = new AboutDlg (); // assign an AboutDlg object
Dlg. ShowDialog (); // display the dialog box
}

Well, our program has been written. Let's press F5 to see the effect!

   Postscript

The C # function is far more than that. The purpose of this article is to allow VC6 users to get started with the development of WINDOWS applications in C # as soon as possible.

Related Article

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.