建网站用什么软件,徐州建站模板公司,造林,诸城网站建设开发一、winform项目与窗体控件
1、部分类的使用
好处#xff1a;让自动生成的代码后置#xff0c;我们编写程序的代码显得更加简洁
特点#xff1a;在最后编译的时候#xff0c;仍然编译成一个窗体类。
窗体和控件的基本使用
3、Event事件#xff08;委托--》事件#…一、winform项目与窗体控件
1、部分类的使用
好处让自动生成的代码后置我们编写程序的代码显得更加简洁
特点在最后编译的时候仍然编译成一个窗体类。
窗体和控件的基本使用
3、Event事件委托--》事件
理解事件
在.Net平台上面给我们所用的这些控件封装了很多的事件。所谓事件就是对用户操作的某一个行为进行封装。比如当用户点击一个按钮的时候单击这个动作已经被封装成了Click事件那么我们只要把这个事件拿出来当用户触发单击这个动作的时候也就是这个事件被调用了我们就可以在这个事件中完成我们需要的任务。
InitializeComponent();//调用Desinger类中的方法用于控件初始化
Sender表示当前控件的对象
二、学会
【1】能够找到我们需要的控件事件。
【2】根据事件生成事件方法并编写业务逻辑。
【3】如果事件不在需要要知道如何把事件关联委托和事件方法的删除如果址删除一个事件方法会报错。
【4】窗体的俩个事件并且学会窗体关闭前的确认逻辑是如何处理的
事件参数 //窗体关闭后发生的private void Form1_FormClosed(object sender, FormClosedEventArgs e){//可以在这里编写要做的其他任务// MessageBox.Show(窗体关闭了,,MessageBoxButtons.OK);}
核心内容窗体常用属性、按钮常用属性、按钮单击事件、生成方法、事件删除方法、窗体常用事件和退出确认的实现。
事件的集中响应
原理就是相同的控件、可以关联同一事件响应方法
好处可以集中处理数据
核心内容按钮的集中添加和Tag数据的封装、窗体Controls集合优化事件关联。
事件通用处理中数据的获取
核心内容在按钮事件中获取数据的方法、对象的封装、泛型集合List运用
三、控件或窗体右键属性 c# ContextMenuStrip控件简单用法-CSDN博客
C# Winform MessageBox的用法 各种类型弹出框-CSDN博客
winform窗体关闭事件的实例-CSDN博客
事件的集中响应
复制Ctrl拖动控件时它的事件也会复制
原理相同的控件可以关联同一个事件响应方法
好处我们可以集中处理数据 容器控件Panel等...
在容器中放控件必须加到对应的容器的集合Controls里面
代码实例
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 client_sideUI
{public partial class Form1 : Form{private Liststring Clacklist new Liststring();//数据绑定控件private BindingSource bindingSource null;/// summary/// 构造方法初始化所有的控件/// /summarypublic Form1(){//this.button1.Text 111;//在初始化的方法前面不要写任何代码InitializeComponent();//调用Desinger类中的方法用于控件初始化//将控件的Click事件和事件方法关联this.button1.Click new System.EventHandler(this.buttonNoe_Click);//我们想完成控件或其他初始化内容在构造方法中写SetupDataBinding();}// 设置数据绑定private void SetupDataBinding(){bindingSource new BindingSource();// 使用匿名类型转换字符串列表var bindingList Clacklist.Select(s new { 课程名称 s }).ToList();bindingSource.DataSource bindingList;dataGridView1.DataSource bindingSource;}// 刷新DataGridView显示private void RefreshGridView(){SetupDataBinding();//刷新数据bindingSource.ResetBindings(false);//重新加载数据dataGridView1.Refresh();}//事件方法private void button1_Click(object sender, EventArgs e){}//事件方法private void buttonNoe_Click(object sender, EventArgs e){//sender表示当前控件的对象//Button btn sender as Button;Button btn (Button)sender;MessageBox.Show(btnclick);//选中单选框this.checkBox1.Checked true;//我们也可以动态的取消事件的关联//this.button1.Click - new System.EventHandler(this.buttonNoe_Click);}//窗体所有控件和初始化完毕后要执行的事件我们通常不用private void Form1_Load(object sender, EventArgs e){//不建议在这里写初始化内容}//窗体关闭之前发生的private void Form1_FormClosing(object sender, FormClosingEventArgs e){if ((MessageBox.Show(确定关闭吗, 提示, MessageBoxButtons.YesNo) DialogResult.Yes)){e.Cancel false;}else{//不关闭窗体e.Cancel true;}}//窗体关闭后发生的private void Form1_FormClosed(object sender, FormClosedEventArgs e){// MessageBox.Show(窗体关闭了,,MessageBoxButtons.OK);}//private void button1_MouseEnter(object sender, EventArgs e)//{// Button btn sender as Button;// //tag属性可以随便写值// btn.Tag btn1;// MessageBox.Show(btn.Tag.ToString());//}//关闭窗体private void button2_Click(object sender, EventArgs e){this.Close();}private void label1_Click(object sender, EventArgs e){Label label (Label)sender;label.Text 1;}private void button3_6_Click(object sender, EventArgs e){Button button (Button)sender;Clacklist.Add(button.Tag.ToString());//bindingSource.DataSource Clacklist;MessageBox.Show(${Clacklist[0].ToString()});RefreshGridView();}private void button7_Click(object sender, EventArgs e){Clacklist.Clear();RefreshGridView();}}
}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 client_sideUI
{public partial class Exercise : Form{//实体类class Coure{public int Coureid { get; set; }public string CoureName { get; set; }//public String CoureHour { get; set; }}private ListCoure ts new ListCoure();public Exercise(){InitializeComponent();int i 0;foreach (Control control in this.groupBox1.Controls){//if(control is Button)//通过控件类型过滤我们不需要的控件//{// Button button control as Button;// if (button.Text ! 保存)// {// button.Tag button.Text ${i};// button.Click new System.EventHandler(this.btn_Click);// }//}string originalText control.Text;string textWithoutLastChar originalText.Remove(originalText.Length - 1);if (control is Button control.Text.ToString() ! 保存){// control.Tag control.Text ${i};control.Click new System.EventHandler(this.btn_Click);string newText textWithoutLastChar ${i};control.Text newText;}i;}}//事件集中处理方法private void btn_Click(object sender, EventArgs e){Button btn sender as Button;//MessageBox.Show(btn.Tag.ToString());//btn.BackColor Color.AliceBlue;//将当前课程信息封装到课程对象并将课程对象封装到列表中this.ts.Add(new Coure{CoureName btn.Text,Coureid Convert.ToInt32(btn.Text.ToString().Substring(btn.Text.Length - 1))});//改变当前按钮的背景色btn.BackColor Color.Green;}//保存课程private void button13_Click(object sender, EventArgs e){foreach(var item in ts){//MessageBox.Show(${item.CoureName} ${item.Coureid});Console.WriteLine(${item.CoureName} ----${item.Coureid});}}}}