首页 > 编程语言 >C# WindowForm界面初探,窗体访问,绑定数据源,重载构造函数

C# WindowForm界面初探,窗体访问,绑定数据源,重载构造函数

时间:2024-08-15 22:06:45浏览次数:9  
标签:控件 Form C# 数据源 System 窗体 using 构造函数

今日份主要内容

C# WindowForm界面初探

  1. Winform项目模板,目录解析
  2. 窗体对象
  3. 控件对象
  4. 界面设计基础

1.控件?

控件的本质是,控件是构建用户界面(User Interface)的基础,通过控件组合设计出符合需求的界面效果。

相当于html的标签。

基本要求:

  • 界面效果,布局
  • 交互(事件,委托)

2.学习控件的技巧?

公共的属性:修改界面效果

事件:修改行为

Winform控件官方文档:按功能列出控件 - Windows Forms .NET Framework | Microsoft Learn

Winform官方简单的案例:使用 C# 创建 Windows 窗体应用 - Visual Studio (Windows) | Microsoft Learn

第一个控件,窗体Form:提供一个界面容器,Form继承于基类System.Windows.Forms.Form

public partial class Form1 : Form{

}

fn+f7打开窗体的业务逻辑代码进行查看或编写,fn+f4打开控件的属性进行简单的调整。可以双击控件,就能查看代码。

反编译查看Form,using Drawing; //系统设计好的,也可以自定义绘制GDI+。

三个重要的位置:FormXXX.cs,设计器(搭建界面),控件的属性窗口。

  • 想操作一个控件,前提是先选中一个控件。
  • 两个窗体之间传递数据:通过构造函数。
  • 窗体事件:FormLoad,FormClosed,Click
  • 数据绑定:把数据源绑定到一个控件 binding 上,数据源和控件之间建立联系。
  • alt+enter +enter引入命名空间

代码演示

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1.窗体
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // 窗体启用样式
            Application.EnableVisualStyles(); 
            Application.SetCompatibleTextRenderingDefault(false);

        // 应用程序启动一个主窗体(第一个窗体)
        Application.Run(new Form1());
    	}
	}
}

Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1.窗体
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Console.WriteLine("hello world1111");
            Debug.WriteLine("hello world22222");
        }

        private void btnStudentList_Click(object sender, EventArgs e)
        {
            // 编写一段代码,打开新窗体
            // 1.实例化一个窗体对象, this当前窗体的实例,通过另外一个窗体的构造函数,把当前窗体的实例传递过去。
            StudentForm studentForm = new StudentForm(this,"学生列表");
            studentForm.Show();  // 打开一个窗体
            //this.Close();// 关闭当前窗体
            this.Hide(); // 隐藏窗体
            //studentForm.ShowDialog();  // 以对话框的形式打开窗体。
        }
    }
}

StudentForm.cs


using _1.窗体.Data;
using _1.窗体.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1.窗体
{
    public partial class StudentForm : Form
    {
        private Form mainForm = null;

        // 把一个无参的构造函数,改成有参构造函数
        // 参数form1类型是窗体Form,用来存储传递过来的this。
        public StudentForm(Form form1, string text)
        {
            InitializeComponent();

            mainForm = form1;
            this.Text = text;

            Debug.WriteLine("构造函数执行");
        }

        private void StudentForm_Load(object sender, EventArgs e)
        {
            Debug.WriteLine("窗体的Load事件构造函数执行");

            // 倒推
            BindDataGridView();
        }

        // 2. 把数据源students和datagridview控件绑定一起。
        private void BindDataGridView()
        {
            // 先把上次绑定的数据源清除,
            dataGridView1.DataSource = null;
            // 再重新绑定,这样可以解决列表中数据已变化,但网格没有刷新的问题。
            dataGridView1.DataSource = StudentData.Students;//StudentData作为数据源,是一个类,Students是这个类里面的列表。而列表里数据的类型则是由另一个Student类定义的,Student是一个实体类。实体类:实体本质对现实的描述,映射ORM(Object Relation Mapping) POCO,这里不做展示。
        }

        // FormClosed
        private void StudentForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainForm.Show();//这个需要自行绑定事件,才会有效果。
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddStudentForm form = new AddStudentForm();
            // 窗体对话框会返回一个结果,是个枚举值,常用的结果:OK,Cancel
            // AddStudentForm窗体返回的结果。
            DialogResult dialogResult = form.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                // 重新绑定数据源
                BindDataGridView();
            }
        }
    }
}

小tip

关于C#里的sln文件和csporj文件的区别:初学C#,总结一下.sln和.csproj的区别 - ProZkb - 博客园 (cnblogs.com)

标签:控件,Form,C#,数据源,System,窗体,using,构造函数
From: https://www.cnblogs.com/dbsdb/p/18361873

相关文章

  • Android Qcom USB Driver学习(二)
    BCv1.2充电规范BatteryChargingSpecificationUSBport如何识别不同的Charger类型USBCharger类型USB_SDP_CHARGERPOWER_SUPPLY_TYPE_USB(StandardDownstreamPort)USBChargerUSB_CDP_CHARGERPOWER_SUPPLY_TYPE_USB_CDP(ChargingDownstreamPort)USBChar......
  • Spring Boot集成Spring Cloud Stream实现消息驱动微服务
    SpringBoot集成SpringCloudStream实现消息驱动微服务大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在构建微服务架构时,消息驱动的微服务是一种常见的设计模式。SpringCloudStream提供了一种简单而强大的模型来发送和接收消息,从而实现解耦和异......
  • JavaScript 中,`'10' < '1'` 的结果为 `false`,原因
    在JavaScript中,'10'<'1'的结果为false,这是因为JavaScript在进行比较操作时,会将字符串按照字符编码进行比较,而不是将它们转换为数字。字符编码比较:字符串'10'的第一个字符是'1',而字符串'1'的第一个字符也是'1'。由于两个字符串的第一个字符相同,所以JavaScript......
  • Python - Protocols
    IntroducedinPython3.8viathetypingmodule,Protocolsofferamoreflexibleapproachthan ABCs,knownasstructuralducktyping,whereanobjectisconsideredvalidifithascertainattributes ormethods,regardlessofitsactualinheritance.Unlike......
  • Vue 项目中,设置的 `color` 样式为 Hex 代码,但最终显示为 RGB 代码 情况原因
    在Vue项目中,设置的color样式为Hex代码,但最终显示为RGB代码,这通常是由于以下几种情况导致:1.CSS预处理器(Sass,Less)的影响:当你使用Sass或Less等CSS预处理器时,它们会将Hex颜色代码转换为RGB颜色代码,以便更好地进行颜色计算和操作。如果你在style属性......
  • Spring Boot集成Spring Cloud Bus进行消息总线通信
    SpringBoot集成SpringCloudBus进行消息总线通信大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在微服务架构中,服务之间的通信是一个常见需求。SpringCloudBus提供了一种基于消息总线的通信机制,可以用于服务间的配置更新、事件发布和订阅等场景......
  • mysql数据库中decimal数据类型比较大小
    在MySQL中,DECIMAL数据类型用于存储精确的数值,它非常适合用于需要高精度计算的场景,如金融应用。当我们需要在MySQL数据库中比较DECIMAL类型数据的大小时,可以使用标准的比较运算符,如>,<,>=,<=,=和<>(或!=)。以下是一个详细的示例,说明如何在MySQL中使用DECIMAL数据类型并比较......
  • Python - Foundational Design Principles
    EncapsulateWhatVariesOneofthemostcommonchallengesinsoftwaredevelopmentisdealingwithchange.Requirements evolve,technologiesadvance,anduserneedsalsochange.Therefore,itiscrucialtowritecodethat canadaptwithoutcausingaripple......
  • PCB入门笔记—绘制一个只有两个排针的PCB全流程记录—立创EDA专业版
    PCB绘制入门......
  • C语言-使用数组法,指针法实现将一个5X5的矩阵中最大的元素放在中心,四个角分别放四个最
    1.题目要求:将一个5X5的矩阵中最大的元素放在中心·,四个角分别放四个最小的元素(顺序为从左到右,从上到下,从小到大存放),写一函数实现之。2.数组法实现#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>//一、数组法实现intmain(){ intarr[5][5]={ {1,2,3,4,5},......