zoukankan      html  css  js  c++  java
  • Visual Studio 2010: C# Hello World Tutorial

    Visual Studio 2010: C# Hello World Tutorial

     
    This tutorial will teach you how to make the most basic C# program, Hello World, using Visual Studio 2010. 

    Loading Visual Studio 2010
    I'll assume that you've already installed Visual Studio 2010. Load it up. If this is the first time you have ran VS2010 you'll need to select a default language. Pick Visual C# Development Settings if you are a C# programmer. 

    Name:  Visual_studio_2010_language_settings.JPG
Views: 6527
Size:  100.8 KB

    You should see the introduction Visual Studio 2010 page:

    Name:  Windows_Visual_Studio_2010_Start_Page.JPG
Views: 6652
Size:  133.8 KB


    Your First VS2010 C# Program
    Select File/New/Project and the New Project window will appear. Select Visual C#. If you don't see this option you may have to click the plus (+) beside Other Languages. This is because you have selected a default language other than C#. Highlight Windows Forms Application

    Change the name to HelloWorld which will also change the solution name at the same time. Your window should look like mine:

    Name:  visual_studio_2010_project_type.JPG
Views: 6614
Size:  132.7 KB

    Click the Ok Button to continue and create your new project.


    Designing the Application
    You should now see your new project. In the center, you should see Form1.cs [Design]. Click directly on this. A dotted line will appear around it and the Properties Window will fill with values. 

    By default, you will not see the properties window. Click View / Properties Window in the File Menu. This will load the Properties window in the bottom right of your screen. 

    Nexy, you should name your form appropriately. Find (Name) in the properties window (you may need to scroll) which should have a value of Form1. Change the value to frmMain. Preceding the names of your objects with what they are (frm = form) will create legible, clean code that is easy to read when you edit your code 1 month later. In this tutorial all objects will have a preceding name which specifies what they are.

    The 2nd thing you should change is the text of your form which currently states Form1. The text label is simply the "title" of your form. Scroll down (or up) in the properties view until you find Text with a value of Form1. Change the value to "Hello World". 

    Name:  Visual_Studio_2010_design_hello_world.JPG
Views: 6579
Size:  133.1 KB


    Creating the Hello World Program
    We are going to be adding a button to the form which will contain our code. The button is an object and stored in the Toolbox tab. If you selected C# as your default language, it is on the left hand side of your screen.

    Name:  visual_studio_2010_toolbox.JPG
Views: 6347
Size:  17.3 KB

    Mouse over the toolbox to unhide the objects. Default is to auto-hide the toolbox. I always click the Auto Hide button (looks like a thumbtack) to disable auto-hide. Your option. 

    Click on Button. Place your mouse cursor over the center of your form (frmMain), left-click and hold. Move your cursor to the right and bottom to create a square. Release the mouse button. You should now see a button in the center of your screen with the Text/Title of Button1.

    Name:  visual_studio_2010_button.JPG
Views: 6336
Size:  26.1 KB


    Button Properties and Code
    Click on the button (button1) to see the properties of the object. Find Name with the value of button1 and change the value to btnHelloWorld. Scroll down and find Text with the value of button1. Change the value to Click Me.

    You have now created a button and properly named/labeled it. The next step is adding the code that makes it work. Double click the button (btnHelloWorld) in the center of your form. This actually creates the button click event function and brings up the code window. 

    Name:  visual_studio_2010_button_code.JPG
Views: 6344
Size:  39.1 KB

    Your cursor is directly below the opening curly bracket { and directly above the closing bracket }. The first thing you should do when a new function is created is comment the code. The Visual Studio 2008 IDE has built in functions that assist you in this task. Press the up arrow three times. Press Enter once. Now press the forward slash (/) three times. As you left of the forward slash the third time XML comments are created for entering function data. Your cursor will be between the summary tags. Enter here what the function below will do. Our function will create a message box with the text "Hello World". Hence, enter:

    Code:
    ///<summary>
    /// Function to create Message Box with 
    /// text "Hello World".
    /// </summary>
    Notice as you press enter three new forward slashes (/) are entered automatically and your cursor is indented to the correct place. This is one of the great features of the VS IDE, automatic indention. You may not appreciate it much now but if you ever do work in Notepad or a similar non programming text editor you will learn how much it helps.

    Move your cursor down below the function btnHellowWorld_Click:
    Code:
    private void btnHelloWorld_Click(object sender, EventArgs e)
            {
    Your cursor should be just below the opening bracket { and just above the closing bracket }. Type MessageBox.Show("Hello World");. Notice the autofill drop down that appears. Another feature of the Visual Studio suite that you will find very useful in time. As you type functions are highlighted in this autofill, pressing enter before you finish typing will automatically fill the text in for you. 

    Name:  visual_studio_2010_hello_world.JPG
Views: 6510
Size:  69.3 KB


    The MessageBox Function
    Displays a modal dialog box that contains a brief application-specific message, such as status or error information. The message box returns an integer value that indicates which button the user clicked.

    MessageBox Function (Windows)


    Executing your First Program
    There are two ways to run the code:

    1) Goto Debug/Start Debugging
    2) Press F5

    Choose either of the options and your code will be compiled and executed.

    Name:  hello_world_click_me.JPG
Views: 6334
Size:  17.1 KB

    Click the button "Click Me". You should see:

    Name:  hello_world_executed.JPG
Views: 6288
Size:  12.1 KB


    You've Made your First Program!
    Congratulations, you've created your first Visual Studio 2010 C# Program. You've learned some of the very basics here in this tutorial and I highly recommend getting a Visual Studio 2010 C# Book to continue learning. If you have any questions or comments please post them here.
     Attached Files



    Read more: http://www.webmastertalkforums.com/programming-tutorials/14877-visual-studio-2010-c-hello-world-tutorial.html#ixzz1dairCZrm

  • 相关阅读:
    Django实现组合搜索的方法示例
    MySQL一些常用命令
    Linux IPMI 安装配置实用[转载]
    注意力训练的十个方法(转来的,原著者,不要打我)
    0001:Web与Web框架
    第二天Python
    第一天Python
    那些被疯狂追求的女孩,后来怎么样了?
    Linux服务器---流量监控MRTG
    Linux基础命令---文本编辑ex
  • 原文地址:https://www.cnblogs.com/youxin/p/2247584.html
Copyright © 2011-2022 走看看