在C#编程环境中:winform窗体应用程序是一种客户端程序,可以用来获取和显示数据。
首先新建一个窗体应用程序项目,在建完一个项目后可以看到有两个.cs类库文件:Form1.cs
和Program.cs
,打开form1可以发现有Designer.cs
和Form1
文件,这里面前者存放的是放在窗体中控件的属性代码,而后面存放的是一些事件程序。当我们在窗体中添加一些控件的时候,designer中就会自动生成属性代码,而当我们想要设置该控件所要实现的事件时就可以Form1中编写。
下面是designer中所自动生成的自己在窗体中添加的控件属性代码,这些属性可以在下面的程序中修改也可以右击属性,直接在属性窗口中修改,并且在属性窗口中修改之后,这里的代码也会跟着自动产生;
namespace SHOWPIC
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.btnsave = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.txtxm = new System.Windows.Forms.TextBox();
this.txtxx = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.btnload = new System.Windows.Forms.Button();
this.btnreset = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnsave
//
this.btnsave.Location = new System.Drawing.Point(470, 52);
this.btnsave.Name = "btnsave";
this.btnsave.Size = new System.Drawing.Size(353, 46);
this.btnsave.TabIndex = 1;
this.btnsave.Text = "保存";
this.btnsave.UseVisualStyleBackColor = true;
this.btnsave.Click += new System.EventHandler(this.btnsave_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(34, 115);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(821, 436);
this.textBox1.TabIndex = 2;
//
// txtxm
//
this.txtxm.Location = new System.Drawing.Point(233, 203);
this.txtxm.Name = "txtxm";
this.txtxm.Size = new System.Drawing.Size(346, 25);
this.txtxm.TabIndex = 5;
//
// txtxx
//
this.txtxx.Location = new System.Drawing.Point(233, 313);
this.txtxx.Name = "txtxx";
this.txtxx.Size = new System.Drawing.Size(337, 25);
this.txtxx.TabIndex = 6;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(127, 206);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(52, 15);
this.label1.TabIndex = 7;
this.label1.Text = "姓名:";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(127, 323);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(52, 15);
this.label2.TabIndex = 8;
this.label2.Text = "学校:";
//
// btnload
//
this.btnload.Location = new System.Drawing.Point(233, 461);
this.btnload.Name = "btnload";
this.btnload.Size = new System.Drawing.Size(75, 23);
this.btnload.TabIndex = 9;
this.btnload.Text = "登陆";
this.btnload.UseVisualStyleBackColor = true;
this.btnload.Click += new System.EventHandler(this.btnload_Click);
//
// btnreset
//
this.btnreset.Location = new System.Drawing.Point(504, 461);
this.btnreset.Name = "btnreset";
this.btnreset.Size = new System.Drawing.Size(75, 23);
this.btnreset.TabIndex = 10;
this.btnreset.Text = "重置";
this.btnreset.UseVisualStyleBackColor = true;
this.btnreset.Click += new System.EventHandler(this.btnreset_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(913, 603);
this.Controls.Add(this.btnreset);
this.Controls.Add(this.btnload);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtxx);
this.Controls.Add(this.txtxm);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnsave);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnsave;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox txtxm;
private System.Windows.Forms.TextBox txtxx;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnload;
private System.Windows.Forms.Button btnreset;
}
}
下面是在form1下编写的事件程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SHOWPIC
{
public partial class Form1 : Form
{
//这里是构造函数,当该类在运行是后自动运行;
public Form1()
{
InitializeComponent();
}
//当窗体在使用时进行的事件
private void Form1_Load(object sender, EventArgs e)
{
btnsave.Visible = false;
textBox1.Visible = false;
}
//这里是登陆界面的按钮所实现的事件;
private void btnload_Click(object sender, EventArgs e)
{
string xm = txtxm.Text;
string xx = txtxx.Text;
if (xm == "猴哥" && xx == "方寸山")
{
label1.Visible = false;
label2.Visible = false;
btnload.Visible = false;
btnreset.Visible = false;
txtxx.Visible = false;
txtxm.Visible = false;
btnsave.Visible = true;
textBox1.Visible = true;
}
else
{
MessageBox.Show("输入错误");
txtxm.Clear();
txtxx.Clear();
txtxm.Focus();
}
}
//这里是登陆界面所实现的重置功能;
private void btnreset_Click(object sender, EventArgs e)
{
txtxm.Clear();
txtxx.Clear();
}
//这里是保存按钮所实现的功能;
private void btnsave_Click(object sender, EventArgs e)
{
FileStream fswrite = new FileStream(@"C:\Users\WJ\Desktop\new.txt", FileMode.Create,FileAccess.Write);
byte[] array = System.Text.Encoding.Default.GetBytes(textBox1.Text);
fswrite.Write(array, 0, array.Length);
MessageBox.Show("写入成功");
}
}
}
上面就是一个窗体文件的基本的属性设置以及事件的程序。这里我们观看整个项目工程,看到有两个.cs类库文件:Form1.cs
和Program.cs
,这里Program.cs
,就是我们的主程序入口,打开的代码初始的程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SHOWPIC
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}