• C# Winform编程(1)基础篇


    Visual Studio 2022开发环境新建WinForm应用项目

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    WinForm代码结构

    在这里插入图片描述

    • Program.cs
      程序入口
      Application.Run(new Form1()); // 设置哪个窗体文件首先被执行

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      
      namespace WindowsFormsApp1
      {
          internal static class Program
          {
              /// 
              /// 应用程序的主入口点。
              /// 
              [STAThread]
              static void Main()
              {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.Run(new Form1());
              }
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    • Form1.cs 窗体文件,可以添加多个窗体文件。
      在窗体设计界面可以拖动控件到窗体文件,编辑窗体文件。

    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;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent(); // Form1.Designer.cs里面定义初始化组件
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • Form1.Designer.cs
    namespace WindowsFormsApp1
    {
        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.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(79, 101);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(992, 526);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.SaveFileDialog saveFileDialog1;
            private System.Windows.Forms.Button button1;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    请添加图片描述

    新键窗体文件

    右键项目->添加->窗体(Windows窗体)
    在这里插入图片描述

    从Form1启动Form2

    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;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
             private void button1_Click(object sender, EventArgs e)
    		 {
    		     Form2 form2 = new Form2();
    		     form2.Show(); // 窗口方式显示,允许在后台运行,Form1和Form2的鼠标焦点可随意切换
    		     
    		 }
    		
    		 private void button2_Click(object sender, EventArgs e)
    		 {
    		     Form2 form2 = new Form2();
    		     form2.ShowDialog(); // 对话框方式显示,不允许在后台运行,必须先关闭Form2焦点才能切换到Form1
    		 }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    请添加图片描述

    修改控件属性

    在这里插入图片描述

    退出程序和关闭窗口

    • 退出程序
      Application.Exit();
    • 关闭窗口
      form.Close();
  • 相关阅读:
    Zookeeper系列——2Zookeeper应用及常用命令
    基于Springboot的旅游网管理系统设计与实现(有报告)。Javaee项目,springboot项目。
    微软行星云计算——Chloris Global Biomass 2003 - 2019年全球生物质数据集
    JavaWeb[总结]
    【Paraview教程】第一章安装与基础介绍
    【SQLite】一、SQLite简介——MySQL的简洁版
    code 网址
    vue3 底部导航栏不在登录页面显示
    SpringCloud之RocketMQ3
    C语言-学生管理系统(结构体+数组实现)
  • 原文地址:https://blog.csdn.net/u013420428/article/details/133361675