winform窗体应用程序

小编 2026-06-05 阅读:539 评论:0
在C#编程环境中:winform窗体应用程序是一种客户端程序,可以用来获取和显示数据。 首先新建一个窗体应用程序项目,在建完一个项目后可以看到有两个.cs类库文件:Form1.cs 和Prog...

在C#编程环境中:winform窗体应用程序是一种客户端程序,可以用来获取和显示数据。
首先新建一个窗体应用程序项目,在建完一个项目后可以看到有两个.cs类库文件:Form1.csProgram.cs,打开form1可以发现有Designer.csForm1文件,这里面前者存放的是放在窗体中控件的属性代码,而后面存放的是一些事件程序。当我们在窗体中添加一些控件的时候,designer中就会自动生成属性代码,而当我们想要设置该控件所要实现的事件时就可以Form1中编写。
下面是designer中所自动生成的自己在窗体中添加的控件属性代码,这些属性可以在下面的程序中修改也可以右击属性,直接在属性窗口中修改,并且在属性窗口中修改之后,这里的代码也会跟着自动产生;

namespace SHOWPIC
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name=\"disposing\">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        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.csProgram.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
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
          
        }
    }
}

这就是一个基本的窗体项目的组成。
\"在这里插入图片描述\"

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表