您的位置:首页 > 编程语言 > C#

C#成神之路<4> C#第一次程序编写

2016-02-01 19:28 525 查看
这里,我编写了第一个C#程序。作为一个小白,C#大量的类和模板轻松实现C++需要大量代码才能实现的功能令我欣喜若狂。这更坚定我精通C#的决心。下面对我在编写程序时碰到的相关问题进行记录。

本程序从C#的空文件进行编写,便于小白我能够充分体验C#带来的乐趣。

<1>首先创建一个C#空文件。

正面效果:产生较少的后台代码。

负面效果:使用Empty Project模板需要比windows模板程序多做一些工作。

<2>Project->Add reference(添加引用)从中引用:

1、System 

2、System.Drawing

3、System.Windows.Forms

<3>添加条目

project->Add New Item 命令显示Add New Item 对话框。添加Code File。对脚本文件进行C#架构的输入。这个就是对空文件添加脚本的过程。

(关于程序保存的习惯:善用txt记录脚本文件,对效率提升有所帮助。我的C++也是这么做的喔)

<3>设置项目属性

Project-> XXX Properties (XXX属性)

在Source Windows的窗口中的XXX选项卡中,在输出类型中选择Windows Application选项。然后将启动对象设置为包含应用程序的Main()方法的对象。

<4>向程序中添加对象

充分利用编译器右侧的工具箱(TOOL)和属性选项卡。在窗口设计界面进行拖拽和对属性进行更改的操作。

     关于TXT属性:比如在设计button的时候,TXT选项下的按钮显示的名字,可以利用&进行相关快捷键的设计。“&Exit”在程序运行的时候alt+E就可以快速执行这个按钮的功能。

     关于驼峰命名法:对象和变量名中的子名称使用首字母大写形式的惯例。

     双击窗口设计中的组件,可以快速进入编辑相关组件脚本的函数内。

     Environment.NewLine:相当于换行符。

<5>关于按钮中的单击事件代码分析

单击事件中的程序代码称之为RDC(无智能代码)。

为了使得代码更加智能,可以添加代码来验证输入文本框的内容,确认数据所需要的信息。

注:程序中的输出可能会排版错乱,这是标签对象的字体(Font)属性设置为默认TrueType字体的原因。可以更改相应的字体,就可以解决输出排版乱序的问题。

一下附上我的代码:

using System;

using System.Windows.Forms;

public class frmMain : Form

{

    private Button btnExit;

    private Label label1;

    private Label label2;

    private Label label3;

    private Label label4;

    private Label label5;

    private TextBox txtName;

    private TextBox txtAddress;

    private TextBox txtCity;

    private TextBox txtState;

    private TextBox txtZip;

    private TextBox txtResult;

    private Button btnDisplayOutput;

    #region Windows code

    private void InitializeComponent() {

            this.btnDisplayOutput = new System.Windows.Forms.Button();

            this.btnExit = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.label2 = new System.Windows.Forms.Label();

            this.label3 = new System.Windows.Forms.Label();

            this.label4 = new System.Windows.Forms.Label();

            this.label5 = new System.Windows.Forms.Label();

            this.txtName = new System.Windows.Forms.TextBox();

            this.txtAddress = new System.Windows.Forms.TextBox();

            this.txtCity = new System.Windows.Forms.TextBox();

            this.txtState = new System.Windows.Forms.TextBox();

            this.txtZip = new System.Windows.Forms.TextBox();

            this.txtResult = new System.Windows.Forms.TextBox();

            this.SuspendLayout();

            // 

            // btnDisplayOutput

            // 

            this.btnDisplayOutput.Location = new System.Drawing.Point(58, 136);

            this.btnDisplayOutput.Name = "btnDisplayOutput";

            this.btnDisplayOutput.Size = new System.Drawing.Size(75, 23);

            this.btnDisplayOutput.TabIndex = 0;

            this.btnDisplayOutput.Text = "&Display";

            this.btnDisplayOutput.UseVisualStyleBackColor = true;

            this.btnDisplayOutput.Click += new System.EventHandler(this.btnDisplayOutput_Click);

            // 

            // btnExit

            // 

            this.btnExit.Location = new System.Drawing.Point(255, 136);

            this.btnExit.Name = "btnExit";

            this.btnExit.Size = new System.Drawing.Size(75, 23);

            this.btnExit.TabIndex = 1;

            this.btnExit.Text = "E&xit";

            this.btnExit.UseVisualStyleBackColor = true;

            // 

            // label1

            // 

            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.label1.Location = new System.Drawing.Point(22, 23);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(75, 20);

            this.label1.TabIndex = 2;

            this.label1.Text = "Name:";

            this.label1.Click += new System.EventHandler(this.label1_Click);

            // 

            // label2

            // 

            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.label2.Location = new System.Drawing.Point(22, 60);

            this.label2.Name = "label2";

            this.label2.Size = new System.Drawing.Size(75, 20);

            this.label2.TabIndex = 3;

            this.label2.Text = "Address:";

            // 

            // label3

            // 

            this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.label3.Location = new System.Drawing.Point(22, 97);

            this.label3.Name = "label3";

            this.label3.Size = new System.Drawing.Size(75, 20);

            this.label3.TabIndex = 4;

            this.label3.Text = "City:";

            // 

            // label4

            // 

            this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.label4.Location = new System.Drawing.Point(160, 97);

            this.label4.Name = "label4";

            this.label4.Size = new System.Drawing.Size(40, 20);

            this.label4.TabIndex = 5;

            this.label4.Text = "State:";

            // 

            // label5

            // 

            this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.label5.Location = new System.Drawing.Point(255, 97);

            this.label5.Name = "label5";

           
c6fa
this.label5.Size = new System.Drawing.Size(40, 20);

            this.label5.TabIndex = 6;

            this.label5.Text = "Zip:";

            // 

            // txtName

            // 

            this.txtName.Location = new System.Drawing.Point(104, 23);

            this.txtName.Name = "txtName";

            this.txtName.Size = new System.Drawing.Size(247, 21);

            this.txtName.TabIndex = 7;

            this.txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);

            // 

            // txtAddress

            // 

            this.txtAddress.Location = new System.Drawing.Point(104, 58);

            this.txtAddress.Name = "txtAddress";

            this.txtAddress.Size = new System.Drawing.Size(247, 21);

            this.txtAddress.TabIndex = 8;

            // 

            // txtCity

            // 

            this.txtCity.Location = new System.Drawing.Point(104, 97);

            this.txtCity.Name = "txtCity";

            this.txtCity.Size = new System.Drawing.Size(50, 21);

            this.txtCity.TabIndex = 9;

            // 

            // txtState

            // 

            this.txtState.Location = new System.Drawing.Point(206, 97);

            this.txtState.Name = "txtState";

            this.txtState.Size = new System.Drawing.Size(43, 21);

            this.txtState.TabIndex = 10;

            this.txtState.TextChanged += new System.EventHandler(this.txtState_TextChanged);

            // 

            // txtZip

            // 

            this.txtZip.Location = new System.Drawing.Point(301, 96);

            this.txtZip.Name = "txtZip";

            this.txtZip.Size = new System.Drawing.Size(50, 21);

            this.txtZip.TabIndex = 11;

            // 

            // txtResult

            // 

            this.txtResult.Location = new System.Drawing.Point(22, 165);

            this.txtResult.Multiline = true;

            this.txtResult.Name = "txtResult";

            this.txtResult.ReadOnly = true;

            this.txtResult.Size = new System.Drawing.Size(329, 84);

            this.txtResult.TabIndex = 12;

            this.txtResult.TextChanged += new System.EventHandler(this.txtResult_TextChanged);

            // 

            // frmMain

            // 

            this.ClientSize = new System.Drawing.Size(369, 261);

            this.Controls.Add(this.txtResult);

            this.Controls.Add(this.txtZip);

            this.Controls.Add(this.txtState);

            this.Controls.Add(this.txtCity);

            this.Controls.Add(this.txtAddress);

            this.Controls.Add(this.txtName);

            this.Controls.Add(this.label5);

            this.Controls.Add(this.label4);

            this.Controls.Add(this.label3);

            this.Controls.Add(this.label2);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.btnExit);

            this.Controls.Add(this.btnDisplayOutput);

            this.Name = "frmMain";

            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

            this.Text = "Mailing Label Program";

            this.ResumeLayout(false);

            this.PerformLayout();

    }

    #endregion

    //#region是C# 预处理器指令。 

    //#region 使您可以在使用 Visual Studio

    //代码编辑器的大纲显示功能时指定可展开或折叠的代码块。

    public frmMain() {

        InitializeComponent();

    }

    public static void Main() {

        frmMain main = new frmMain();

        Application.Run(main);

    }

    private void label1_Click(object sender, EventArgs e)

    {

    }

    private void txtState_TextChanged(object sender, EventArgs e)

    {

    }

    private void btnDisplayOutput_Click(object sender, EventArgs e)

    {

        string buffer;

        buffer = "Mailing Label: " + Environment.NewLine +Environment.NewLine;

        buffer = buffer + "Name: " + txtName.Text + Environment.NewLine;

        buffer = buffer + "Address: " + txtAddress.Text + Environment.NewLine;

        buffer = buffer+"City: "+txtAddress.Text+"State:"+txtState.Text+"Zip:"+txtZip.Text;

        txtResult.Text = buffer;

    }

    private void txtResult_TextChanged(object sender, EventArgs e)

    {

    }

    private void txtName_TextChanged(object sender, EventArgs e)

    {

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息