技术开发 频道

C#深入浅出全接触:实战篇

  修改一下代码

  现在我们要增加一些代码来执行一些有用的操作。第一件事是将右上角的窗口切换到Class View(类视图),然后展开"Hello1" 名称空间,点击 "Class1" 类。在属性窗口中将名称修改为
Hello。现在的代码窗口变为:

  将光标移到Main 方法内的TODO 注释之后,输入“Console”这个命令。请注意发生了什么:你能看到系统自动列出了Console 类的相关方法。选择 WriteLine,然后写入下面这一行:

Console.WriteLine("Hello from VS.NET!");

 

  运行

  现在,从“Build”菜单中选择“Build”项,然后从“Debug”(调试)菜单中选择 "Start Without Debugging"(不调试启动)。最后,控制台应该显示出 "Hello From VS.NET!"的信息。这说明,我们已经大功告成了。

  2.用Visual C# 创建Windows 应用程序

  在 Visual C#创建一个Windows (GUI) 应用程序要以前版本的VC++ 容易得多。下面将介绍用Visual C#工程文件向导创建Windows 应用程序的过程。

  创建应用程序框架

  在 VS .NET IDE 中选择“新建->工程文件->Visual C# 工程文件->Windows 应用程序”:

  然后点击 OK,出现一个表单设计视图(这与VB 或Delphi 相同)。在右侧我们看到了一个解决方案导航器( Solution Explorer)。向导为新表单增加了一个Form1.cs 文件,其中包括了这个表单及其所有子窗口的的代码:

  双击 Form1.cs 就能看到这个代码:

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 ();
[url
=]//@this.TrayHeight[/url] = 0;
[url=]//@this.TrayLargeIcon[/url] = false;
[url=]//@this.TrayAutoArrange[/url] = 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());
}
}
}

  从以上代码中,我们看到:向导增加了一个默认的名称空间以及对WinForms 所要求的不同名称空间的引用;Form1 类是从System.WinForms.Form中派生出来的;InitializeComponent 方法负责初始化(创建)表单及其控件(当在表单中托放下一些控件时,可以看到它的更多细节);Dispose 方法负责清除所有不再使用的资源。

0
相关文章