WinForm之通过事件传值来完成添加修改窗体赋值
来源:互联网 发布:淘宝卖家如何添加客服 编辑:程序博客网 时间:2024/06/07 02:55
废话不多说,直接上实例,对学生表进行事件传值来完成对添加修改窗体的赋值。
(1)创建Student类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _05事件传值{ public class Student { //TSId, TSName, TSGender, TSAddress, TSAge, private int _tSId; public int TSId { get { return _tSId; } set { _tSId = value; } } private string _tSName; public string TSName { get { return _tSName; } set { _tSName = value; } } private char _tSGender; public char TSGender { get { return _tSGender; } set { _tSGender = value; } } private string _tSAddress; public string TSAddress { get { return _tSAddress; } set { _tSAddress = value; } } private int _tSAge; public int TSAge { get { return _tSAge; } set { _tSAge = value; } } }}
(2)SQLHelper类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;using System.Configuration;namespace _05事件传值{ public class SqlHelper { private static readonly string str = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString; //连接字符串 public static int ExecuteNonQuery(string sql, params SqlParameter[] ps) { using (SqlConnection con=new SqlConnection(str)) { using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); cmd.Parameters.AddRange(ps); return cmd.ExecuteNonQuery(); } } } public static object ExecuteScalar(string sql, params SqlParameter[] ps) { using (SqlConnection con = new SqlConnection(str)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); cmd.Parameters.AddRange(ps); return cmd.ExecuteScalar(); } } } public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps) { SqlConnection con = new SqlConnection(str); using (SqlCommand cmd=new SqlCommand(sql,con)) { cmd.Parameters.AddRange(ps); try { con.Open(); return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } catch (Exception ex) { con.Close(); con.Dispose(); throw ex; } } } }}(3)新建两个窗体Form1和StudentAddAndUpdate
(4)创建个事件参数类MyEventArgs继承至EventArgs,用于事件传值
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _05事件传值{ public class MyEventArgs:EventArgs { /// <summary> /// 标识 /// </summary> public int Temp { get; set; } /// <summary> /// 传对象 /// </summary> public object Obj { get; set; } }}(5)Form1窗体代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace _05事件传值{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LoadStudent(); } private void LoadStudent() { List<Student> list = new List<Student>(); string sql = "select tsid,tsname,tsgender,tsaddress,tsage from student"; using (SqlDataReader reader=SqlHelper.ExecuteReader(sql)) { if (reader.HasRows) { while (reader.Read()) { Student stu = new Student(); stu.TSAddress = reader["TSAddress"].ToString(); stu.TSAge = Convert.ToInt32(reader["TSAge"]); stu.TSId = Convert.ToInt32(reader["TSId"]); stu.TSName = reader["TSName"].ToString(); stu.TSGender = Convert.ToBoolean(reader["TSGender"])?'男':'女'; list.Add(stu); } } } dgvStudent.AutoGenerateColumns = false; dgvStudent.DataSource = list; dgvStudent.SelectedRows[0].Selected = false; } //新增 private void btnAdd_Click(object sender, EventArgs e) { ShowStudentAddAndUpdate(1); } //修改 private void btnUpdate_Click(object sender, EventArgs e) { if (dgvStudent.SelectedRows.Count > 0) { Student stu = new Student(); stu.TSAddress = dgvStudent.SelectedRows[0].Cells[4].Value.ToString();//地址 stu.TSName = dgvStudent.SelectedRows[0].Cells[1].Value.ToString();//姓名 stu.TSAge = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[3].Value);//年龄 stu.TSGender = Convert.ToChar(dgvStudent.SelectedRows[0].Cells[2].Value);//性别 stu.TSId = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[0].Value); mea.Obj = stu;//对象存起来了--传值 ShowStudentAddAndUpdate(2);//修改 } else { MessageBox.Show("如果要修改请选中修改的行"); } } //1---新增,2=====修改 public event EventHandler evt; MyEventArgs mea = new MyEventArgs(); public void ShowStudentAddAndUpdate(int p) { StudentAddAndUpdate sau = new StudentAddAndUpdate(); //把方法作为参数进行传递 //注册事件 this.evt+=new EventHandler(sau.SetText);//注册事件 //传值 mea.Temp = p;//标识存起来 if (this.evt!=null) { this.evt(this, mea); //刷新 sau.FormClosed += new FormClosedEventHandler(sau_FormClosed); sau.ShowDialog(); } } void sau_FormClosed(object sender, FormClosedEventArgs e) { LoadStudent(); } }}(6)StudentAddAndUpdate窗体代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _05事件传值{ public partial class StudentAddAndUpdate : Form { public StudentAddAndUpdate() { InitializeComponent(); } private int TP { get; set; } public void SetText(object sender, EventArgs e) { MyEventArgs mea = e as MyEventArgs; this.TP = mea.Temp;//标识存起来 //清空所有文本框的值 foreach (Control item in this.Controls) { if (item is TextBox) { TextBox tb = item as TextBox; tb.Text = ""; } } if (this.TP==1)//新增 { } else if (this.TP==2)//修改 { Student stu = mea.Obj as Student; txtAddress.Text = stu.TSAddress;//地址 txtAge.Text = stu.TSAge.ToString();//年龄 txtName.Text = stu.TSName; //性别 rdoMan.Checked= stu.TSGender == '男' ? true : false; rdoWoman.Checked = stu.TSGender == '女' ? true : false; //id labId.Text = stu.TSId.ToString(); } } // private void btnOk_Click(object sender, EventArgs e) { //是新增还是修改 if (this.TP==1)//新增 { //写sql语句 调用sqlhelper } else if (this.TP==2)//修改 { } } }}
0 0
- WinForm之通过事件传值来完成添加修改窗体赋值
- winform中通过事件实现窗体传值思路【待修改】
- WinForm载入窗体完成后自动执行事件
- winform窗体添加背景音乐
- Winform窗体传值的几种方式(属性赋值,委托,事件)
- Winform wpf在一个窗体的事件里,修改另一个窗体控件的值
- C#之WinForm基础 添加多个窗体
- winform使用委托和事件完成两个窗体之间的通信实例
- winform窗体失去焦点事件
- c# winform窗体关闭事件
- [zz]winform 窗体关闭事件
- winform窗体添加动态效果
- WPF 中添加winform 窗体
- winform子窗体加载父窗体事件
- WinForm在窗体里面添加窗体
- 【WinForm窗体控件开发】之三续 窗体控件设计时的事件属性
- 在C# WinForm中添加窗体大小变化和退出前确认功能,就要自行添加Resize、FormClosing事件
- WinForm设计之About窗体
- VMware虚拟机的安装
- iOS程序崩溃日志
- MyEclipse安装JS代码提示(Spket插件)
- 【代码笔记】iOS-实现网络图片的异步加载和缓存
- IOS Sqlite使用FMDB
- WinForm之通过事件传值来完成添加修改窗体赋值
- runtime 详解
- swift-UITableView的根本使用
- 每日一得--go语言学习笔记
- python开发常用的python模块及安装方法
- 编程语言是女人
- Android Studi 运行报错 Duplicate files copied in APK META-INF/LICENSE.txt 解决方案
- OpenCV 级联检测器Cascadedetect人脸检测源码理解与分析
- Swift/Objc的Runtime(运行时)机制