修改一下代码
现在我们要增加一些代码来执行一些有用的操作。第一件事是将右上角的窗口切换到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 description for Form1. /// public class Form1 : System.WinForms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// public override void Dispose() { base.Dispose(); components.Dispose(); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// 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) { } /// /// The main entry point for the application. /// public static void Main(string[] args) { Application.Run(new Form1()); } } } 从以上代码中,我们看到:向导增加了一个默认的名称空间以及对WinForms 所要求的不同名称空间的引用;Form1 类是从System.WinForms.Form中派生出来的;InitializeComponent方法负责初始化(创建)表单及其控件(当在表单中托放下一些控件时,可以看到它的更多细节);Dispose方法负责清除所有不再使用的资源 。 添加控件 要向一个表单中添加控件或者子窗口,需要打开 工具箱ToolBox。这个工具箱的概念来自VB。点击菜单“视图->工具箱”,激活工具箱功能: ToolBox(工具箱)窗口的样子如下图所示。现在就可以添加控件了,添加方法与Visual Studio的以前版本一样,拖放或者双击控件都可以。 首先在表单上托放下一个按钮和一个编辑框,然后让我们看看系统向初始组件(InitializeComponent)中增加了什么东西。 接着在属性窗口中设置控件的属性,这与VB中的操作方式一样。在控件上点击右键,并点中“属性”菜单条就可以调出属性窗口。 现在看看InitializeComponent方法,就会发现这些代码已经增加到其中了。接着手工修改一下这些代码: 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.button1_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); 添加事件处理器 最后,要为按钮增加一个事件处理器,实现浏览文件的目的。在按钮上双击,打开Button1_Click事件处理器。同理,使用同样的方法可以为任何控件编写事件处理器。 protected void button1_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 ; } } 到此就完成了所有步骤,剩下的就是运行这个程序。它实现了浏览一个文件,然后将选择的文件名装进文本框的功能。请下载相关代码:winFormApp.zip 。 3、创建C# 类库 (Dll) 以前在VC++中创建一个dll文件不能说简单,但在Visual C# 中,这将同样是轻而易举的事情。下面的介绍分成两部分:1、创建 DLL 2、在客户端测试 dll。 (1)创建DLL 首先创建一个空的类库工程。在VS.NET集成环境(IDE)中选择“文件->新建->工程文件->Visual C# 工程->类库”,点击Browse(浏览)按钮选择工程文件名和相应的目录,再点击 OK: 接着看看工程和它的相关文件。Solution Explorer(解决方案探测器)向工程中增加两个C# 类,第一个是 AssemblyInfo.cs ,第二个是Class1.cs。我们并讨论AssemblyInfo,重点介绍 Class1.cs。 双击Class1.cs,就能看到一个名称空间mcMath。我们将在客户机引用这个名称空间以使用这个类库: namespace mcMath { using System; /// /// Summary description for Class1. /// public class Class1 { public Class1() { // // TODO: Add Constructor Logic here // } } } 现在就可以Build(构造)这个工程了。Build(构造)完毕后,就会在工程文件的bin/debug 目录中生成mcMath.dll文件。 增加一个方法 从View (视图)菜单中打开ClassView(类视图),开始只显示Class1,没有方法和属性。现在来增加一个方法和一个属性。 用鼠标右键单击“Class1”,选择“Add(增加)-> Add Method(增加方法)”,这时将弹出C# 方法生成向导: 在这个窗口中增加方法名、存取类型、返回类型、参数以及注释信息。使用Add(增加)和Remove(取消)按钮可分别从参数列表中增加和取消参数。这里增加了一个方法long Add( long val1, long val2 ),它负责将两个数字相加并返回和。 增加一个属性 同理可以 C# 属性生成向导,向类中增加一个属性: 增加了一个方法和一个属性后, Class1变成下图所示的样子: 仔细观察这个 Class1,你会发现C#的向导程序向类中增加了如下两个函数: public long Add (long val1, long val2) { return 0; } public bool Extra { get { return true; } set { } } 向类中增加代码 这里把Class1修改成为 mcMathComp ,因为 Class1是个容易造成混淆的名字,当想将这个类用在一个客户应用程序中时会造成问题。下面的代码对上面的做了些调整: namespace mcMath { using System; public class mcMathComp { private bool bTest = false; public mcMathComp() { } public long Add (long val1, long val2) { return val1 + val2; } public bool Extra { get { return bTest; } set { bTest = Extra ; } } } } 构造 dll 选择Build菜单创建dll文件,如果一切OK,就会在工程文件的 bin\debug目录生成dll文件。 (2)在客户端测试 dll