C#成神之路<9> C#引用数据类型扩展

1、字符串和转义序列
C#在字符串中使用特殊的转义字符表示将他后面的字符区别对待。特殊的转义字符:\

逐字的字符串字符:@告诉VS完全按照出现在双引号标记内的内容来构建字符串。

2、DateTime 引用对象
(1)VS追踪时间的方式
Windows操作系统将维护系统时钟作为他职责的一部分VS用系统时钟来跟踪滴答(Tick)的数量。Tick是100ns为度量的时间单位。1ns是1s的十亿分之一。光在1ns的时间里前进18英寸。
DateTime的对象保存从公元0001年1月1日午夜开始的滴答数量。

(2)DateTime和ToString的转换
有些数据类型允许向ToString方法传递一个参数,因此可以按照特定的方式格式化数据。
实例:
“F”:完整日期和时间,包括s。
“f”:完整日期和时间不包括s。
“D”:完整日期。
“d”:短日期。
“G”:一般日期,包括s。
“g”:一般日期,不包括s。
“T”:完整时间,包括s。
“t”:完整时间,不包括s。

3、示例程序

(1)对C#程序中的一些共同主题的讨论

I.#region和#endregion是C#的程序指令,用于隐藏代码块。其中包含的代码段大多都是vs自动生成的代码段。

II.切断对象链:
this.label1 = new System.Windows.Forms.Label();
如上面的代码,new:创建某种类型的新对象。
.点运算符:进行对象的深层次访问。

III.程序开始执行的方式

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

所有C#程序都要从一个Main函数开始执行。
表示创建frmMain类的一个新对象。并且运行。

IV.类构造函数
类构造函数:与类同名的方法。
目的:在使用类的对象之前执行需要完成的所有人物。
属性:类构造函数总是与他们出现在其中的类同名。
如果希望实例化该类的对象,构造函数总是会在他们的名称前面使用public
构造函数永远不会同他的调用者返回值。
还有函数签名和重载的概念,同C++一样。

using System;
using System.Windows.Forms;
public class frmMain : Form
{
    private Label label1;
    private Label label2;
    private Label label3;
    private Label label4;
    private Label label5;
    private Label label6;
    private Label label7;
    private TextBox txtLongDate;
    private TextBox txtShortDate;
    private TextBox txtGeneralDateAndTime;
    private TextBox txtLongTime;
    private TextBox txtShortTime;
    private TextBox txtDaysToNewYears;
    private Button btnRefresh;
    private TextBox txtComleteDateAndTime;
    private Button btnClose;
    #region Windows code
    private void InitializeComponent()
    {
            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.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.txtLongDate = new System.Windows.Forms.TextBox();
            this.txtShortDate = new System.Windows.Forms.TextBox();
            this.txtGeneralDateAndTime = new System.Windows.Forms.TextBox();
            this.txtLongTime = new System.Windows.Forms.TextBox();
            this.txtShortTime = new System.Windows.Forms.TextBox();
            this.txtDaysToNewYears = new System.Windows.Forms.TextBox();
            this.btnRefresh = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.txtComleteDateAndTime = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label1.Location = new System.Drawing.Point(36, 48);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(178, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Full Date and Time";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label2
            // 
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label2.Location = new System.Drawing.Point(36, 93);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(178, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "Long Date";
            this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label3
            // 
            this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label3.Location = new System.Drawing.Point(36, 138);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(178, 23);
            this.label3.TabIndex = 2;
            this.label3.Text = "Short Date";
            this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label4
            // 
            this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label4.Location = new System.Drawing.Point(36, 182);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(178, 23);
            this.label4.TabIndex = 3;
            this.label4.Text = "General Date and Time";
            this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label5
            // 
            this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label5.Location = new System.Drawing.Point(36, 222);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(178, 23);
            this.label5.TabIndex = 4;
            this.label5.Text = "Long Time";
            this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label6
            // 
            this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label6.Location = new System.Drawing.Point(36, 265);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(178, 23);
            this.label6.TabIndex = 5;
            this.label6.Text = "Short Time";
            this.label6.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label7
            // 
            this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label7.Location = new System.Drawing.Point(36, 307);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(178, 23);
            this.label7.TabIndex = 6;
            this.label7.Text = "Days until New Years Eve";
            this.label7.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtLongDate
            // 
            this.txtLongDate.Location = new System.Drawing.Point(229, 91);
            this.txtLongDate.Multiline = true;
            this.txtLongDate.Name = "txtLongDate";
            this.txtLongDate.ReadOnly = true;
            this.txtLongDate.Size = new System.Drawing.Size(178, 21);
            this.txtLongDate.TabIndex = 7;
            // 
            // txtShortDate
            // 
            this.txtShortDate.Location = new System.Drawing.Point(229, 139);
            this.txtShortDate.Multiline = true;
            this.txtShortDate.Name = "txtShortDate";
            this.txtShortDate.ReadOnly = true;
            this.txtShortDate.Size = new System.Drawing.Size(178, 21);
            this.txtShortDate.TabIndex = 8;
            // 
            // txtGeneralDateAndTime
            // 
            this.txtGeneralDateAndTime.Location = new System.Drawing.Point(229, 182);
            this.txtGeneralDateAndTime.Multiline = true;
            this.txtGeneralDateAndTime.Name = "txtGeneralDateAndTime";
            this.txtGeneralDateAndTime.ReadOnly = true;
            this.txtGeneralDateAndTime.Size = new System.Drawing.Size(178, 21);
            this.txtGeneralDateAndTime.TabIndex = 9;
            // 
            // txtLongTime
            // 
            this.txtLongTime.Location = new System.Drawing.Point(229, 222);
            this.txtLongTime.Multiline = true;
            this.txtLongTime.Name = "txtLongTime";
            this.txtLongTime.ReadOnly = true;
            this.txtLongTime.Size = new System.Drawing.Size(178, 21);
            this.txtLongTime.TabIndex = 10;
            // 
            // txtShortTime
            // 
            this.txtShortTime.Location = new System.Drawing.Point(229, 266);
            this.txtShortTime.Multiline = true;
            this.txtShortTime.Name = "txtShortTime";
            this.txtShortTime.ReadOnly = true;
            this.txtShortTime.Size = new System.Drawing.Size(178, 21);
            this.txtShortTime.TabIndex = 11;
            // 
            // txtDaysToNewYears
            // 
            this.txtDaysToNewYears.Location = new System.Drawing.Point(229, 307);
            this.txtDaysToNewYears.Multiline = true;
            this.txtDaysToNewYears.Name = "txtDaysToNewYears";
            this.txtDaysToNewYears.ReadOnly = true;
            this.txtDaysToNewYears.Size = new System.Drawing.Size(178, 21);
            this.txtDaysToNewYears.TabIndex = 12;
            // 
            // btnRefresh
            // 
            this.btnRefresh.Location = new System.Drawing.Point(36, 363);
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(105, 23);
            this.btnRefresh.TabIndex = 13;
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.UseVisualStyleBackColor = true;
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // btnClose
            // 
            this.btnClose.Location = new System.Drawing.Point(298, 363);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(109, 23);
            this.btnClose.TabIndex = 14;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.button1_Click);
            // 
            // txtComleteDateAndTime
            // 
            this.txtComleteDateAndTime.Location = new System.Drawing.Point(229, 49);
            this.txtComleteDateAndTime.Multiline = true;
            this.txtComleteDateAndTime.Name = "txtComleteDateAndTime";
            this.txtComleteDateAndTime.ReadOnly = true;
            this.txtComleteDateAndTime.Size = new System.Drawing.Size(178, 21);
            this.txtComleteDateAndTime.TabIndex = 15;
            // 
            // frmMain
            // 
            this.ClientSize = new System.Drawing.Size(431, 436);
            this.Controls.Add(this.txtComleteDateAndTime);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnRefresh);
            this.Controls.Add(this.txtDaysToNewYears);
            this.Controls.Add(this.txtShortTime);
            this.Controls.Add(this.txtLongTime);
            this.Controls.Add(this.txtGeneralDateAndTime);
            this.Controls.Add(this.txtShortDate);
            this.Controls.Add(this.txtLongDate);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            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.Name = "frmMain";
            this.ResumeLayout(false);
            this.PerformLayout();

    }
    #endregion
    //#region是C# 预处理器指令。 
    //#region 使您可以在使用 Visual Studio
    //代码编辑器的大纲显示功能时指定可展开或折叠的代码块。
    public frmMain()
    {
        InitializeComponent();
        UpdateTimeInfo();
    }

    public static void Main()
    {
        frmMain main = new frmMain();
        Application.Run(main);
    }

    private void UpdateTimeInfo()
    {
        int days;
        DateTime myTime = new DateTime();
        myTime=DateTime.Now;
        DateTime newYears = new DateTime(myTime.Year,12,31);
        txtComleteDateAndTime.Text = myTime.ToString("f");
        txtLongDate.Text = myTime.ToString("D");
        txtShortDate.Text = myTime.ToString("d");
        txtGeneralDateAndTime.Text= myTime.ToString("g");
        txtLongTime.Text = myTime.ToString("T");
        txtShortTime.Text = myTime.ToString("t");

        days = newYears.DayOfYear - myTime.DayOfYear;
        txtDaysToNewYears.Text = days.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       Close(); 
    }

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        UpdateTimeInfo();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace DormMIS { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Login : System.Windows.Forms.Form { private System.Windows.Forms.Button btClose;//取消按钮 private System.Windows.Forms.Button btAdd; private System.Windows.Forms.TextBox password; private System.Windows.Forms.TextBox name; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private OleDbConnection oleConnection1 = null; public Login() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); this.oleConnection1=new OleDbConnection(DormMIS.database.dbConnection.connection); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Login)); this.btClose = new System.Windows.Forms.Button(); this.btAdd = new System.Windows.Forms.Button(); this.password = new System.Windows.Forms.TextBox(); this.name = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btClose // this.btClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btClose.ForeColor = System.Drawing.Color.Black; this.btClose.Location = new System.Drawing.Point(168, 156); this.btClose.Name = "btClose"; this.btClose.Size = new System.Drawing.Size(75, 23); this.btClose.TabIndex = 20; this.btClose.Text = "取消"; this.btClose.Click += new System.EventHandler(this.btClose_Click); // // btAdd // this.btAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btAdd.ForeColor = System.Drawing.Color.Black; this.btAdd.Location = new System.Drawing.Point(48, 156); this.btAdd.Name = "btAdd"; this.btAdd.Size = new System.Drawing.Size(75, 23); this.btAdd.TabIndex = 19; this.btAdd.Text = "确定"; this.btAdd.Click += new System.EventHandler(this.btAdd_Click); // // password // this.password.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.password.Location = new System.Drawing.Point(128, 116); this.password.Name = "password"; this.password.PasswordChar = '*'; this.password.Size = new System.Drawing.Size(100, 21); this.password.TabIndex = 18; this.password.Text = "admin"; // // name // this.name.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.name.Location = new System.Drawing.Point(128, 76); this.name.Name = "name"; this.name.Size = new System.Drawing.Size(100, 21); this.name.TabIndex = 17; this.name.Text = "admin"; this.name.TextChanged += new System.EventHandler(this.name_TextChanged); // // label3 // this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label3.ForeColor = System.Drawing.Color.Black; this.label3.Location = new System.Drawing.Point(64, 116); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(56, 23); this.label3.TabIndex = 16; this.label3.Text = "密 码"; // // label2 // this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label2.ForeColor = System.Drawing.Color.Black; this.label2.Location = new System.Drawing.Point(64, 76); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 23); this.label2.TabIndex = 15; this.label2.Text = "用户名"; // // label1 // this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.ForeColor = System.Drawing.Color.Black; this.label1.Location = new System.Drawing.Point(40, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(228, 28); this.label1.TabIndex = 14; this.label1.Text = "宿舍管理信息系统"; // // Login // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.Ivory; this.ClientSize = new System.Drawing.Size(304, 210); this.Controls.Add(this.btClose); this.Controls.Add(this.btAdd); this.Controls.Add(this.password); this.Controls.Add(this.name); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Login"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "登录"; this.Load += new System.EventHandler(this.Login_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Login()); } private void btAdd_Click(object sender, System.EventArgs e) { if(name.Text.Trim()==""||password.Text.Trim()=="") MessageBox.Show("请输入用户名和密码","提示"); else { oleConnection1.Open(); OleDbCommand cmd=new OleDbCommand("",oleConnection1); string sql="select * from userinfo where UName='"+name.Text.Trim()+"' and PWD='"+password.Text.Trim()+"'"; cmd.CommandText=sql; if (null!=cmd.ExecuteScalar()) { //隐藏登录窗口 this.Visible=false; //创建并打开主界面 Main main=new Main(); main.Tag=this.FindForm(); OleDbDataReader dr; cmd.CommandText=sql; dr=cmd.ExecuteReader(); dr.Read(); main.statusBarPanel2.Text=name.Text.Trim(); main.ShowDialog(); } else MessageBox.Show("用户名或密码错误","警告"); } oleConnection1.Close(); } private void btClose_Click(object sender, System.EventArgs e) { this.Close(); } private void name_TextChanged(object sender, EventArgs e) { } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值