1.更改光标顺序
视图-->Tab键顺序
启动的时候,光标就会在用户名的文本框中,并且在按tab键的时候,光标就会按照我们定的顺序显示。即用户名文本框--tab-->密码文本框--tab-->登录--tab-->重置
2.新建类
右键-->添加-->类
写入代码,封装字段生成属性,右键-->重构-->封装字段-->确认-->确认
3.控制有些窗口只能打开一个
代码示例:
private void 员工列表ToolStripMenuItem_Click(object sender, EventArgs e) { //先判断当前窗口中有没有员工列表这个窗体 //如果有则将其放到所有窗口的最前面,如果没有就打开窗体 foreach(Form item in this.MdiChildren) { if(item.GetType().Name == "StaffForm") { item.BringToFront(); return; } } //如果没有该窗体的情况: StaffForm staffForm = new StaffForm(); staffForm.MdiParent = this; staffForm.Show(); }
4.关闭窗口的时候提示信息
在对应的窗口中找到这个然后双击
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dialogResult = MessageBox.Show("确定退出系统吗? ", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if(dialogResult == DialogResult.Yes) { Application.ExitThread(); } else { //取消事件 e.Cancel = true; } } private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e) { //退出系统:this.Close()会继承上面的窗体关闭操作 //提示框与MainForm_FormClosing里面的提示框内容相同 this.Close(); } private void toolStripButton3_Click(object sender, EventArgs e) { //退出系统 this.Close(); }
5.关闭所有子窗体
private void toolStripButton4_Click(object sender, EventArgs e) { //关闭所有子窗体 DialogResult dialogResult = MessageBox.Show("确定要关闭所有子窗体吗? ","系统提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if(dialogResult == DialogResult.Yes) { //激活状态的子窗体 Form activeChild = this.ActiveMdiChild; while(activeChild != null) { //关闭已打开的子窗体 activeChild.Close(); activeChild = this.ActiveMdiChild; } } }
6.定时器
添加事件
private void timer1_Tick(object sender, EventArgs e) { DateTime dateTime = DateTime.Now; toolStripStatusLabel3.Text = dateTime.ToString("yyyy年MM月dd日 HH:mm:ss"); }
7.窗体加载更新当前登录用户
private void MainForm_Load(object sender, EventArgs e) { toolStripStatusLabel2.Text = User.Uname; }
8.员工列表显示
private void StaffForm_Load(object sender, EventArgs e) { MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select sId as '编号',sCard as '工号',sName as '姓名',\r\nsSex as '性别',sAge as '年龄',sAddress as '地址',\r\ndeptName as '部门',sWages as '工资'\r\nfrom staffinfo s INNER JOIN deptinfo d on s.deptId = d.deptId;"); MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); msda.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; mySqlConnection.Close(); }
9.下拉框显示
private void StaffForm_Load(object sender, EventArgs e) { //初始化 setDataGridView(); setComboBox(); } /// <summary> /// 初始化ComboBox /// </summary> private void setComboBox() { MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select deptId,deptName from deptinfo"); MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); msda.Fill(ds); //显示值 comboBox1.DisplayMember = "deptName"; //隐藏值 comboBox1.ValueMember = "deptId"; comboBox1.DataSource = ds.Tables[0]; mySqlConnection.Close(); }
效果:
在下拉框默认显示请选择,需要在查询数据之后添加多一条数据
private void StaffForm_Load(object sender, EventArgs e) { //初始化 setDataGridView(); setComboBox(); } /// <summary> /// 初始化ComboBox /// </summary> private void setComboBox() { MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select deptId,deptName from deptinfo"); MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); //msda.Fill(ds); msda.Fill(ds, "depts"); //添加请选择 DataRow dr = ds.Tables["depts"].NewRow(); dr[0] = "0"; dr[1] = "请选择"; ds.Tables["depts"].Rows.InsertAt(dr, 0); //显示值 comboBox1.DisplayMember = "deptName"; //隐藏值 comboBox1.ValueMember = "deptId"; comboBox1.DataSource = ds.Tables["depts"]; mySqlConnection.Close(); }
效果:
10.员工列表添加多条件查询
private void StaffForm_Load(object sender, EventArgs e) { //初始化DataDridView列表数据 setDataGridView(); //初始化ComboBox下拉框 setComboBox(); // } /// <summary> /// 初始化DataGridView数据列表 /// </summary> private void setDataGridView() { MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); //string sql = string.Format("select sId as '编号',sCard as '工号',sName as '姓名',\r\nsSex as '性别',sAge as '年龄',sAddress as '地址',\r\ndeptName as '部门',sWages as '工资'\r\nfrom staffinfo s INNER JOIN deptinfo d on s.deptId = d.deptId;"); //条件查询 string sql = string.Format("select sId as '编号',sCard as '工号',sName as '姓名',\r\nsSex as '性别',sAge as '年龄',sAddress as '地址',\r\ndeptName as '部门',sWages as '工资'\r\nfrom staffinfo s INNER JOIN deptinfo d on s.deptId = d.deptId where 1=1"); if (textBox1.Text.Trim().Length !=0) { //注意最前面要写空格 sql += string.Format(" and sCard like '%{0}%'", textBox1.Text.Trim()); } if(textBox2.Text.Trim().Length !=0) { sql += string.Format(" and sName like '%{0}'", textBox2.Text.Trim()); } if(comboBox1.SelectedIndex > 0) { sql += string.Format(" and deptName like '%{0}'", comboBox1.Text.Trim()); } MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); msda.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; mySqlConnection.Close(); } /// <summary> /// 初始化ComboBox下拉框 /// </summary> private void setComboBox() { MySqlConnection mySqlConnection = new MySqlConnection("server=127.0.0.1;port=3306;user=root;password=123456;database=course"); mySqlConnection.Open(); string sql = string.Format("select deptId,deptName from deptinfo"); MySqlDataAdapter msda = new MySqlDataAdapter(sql, mySqlConnection); DataSet ds = new DataSet(); //msda.Fill(ds); msda.Fill(ds, "depts"); //添加请选择 DataRow dr = ds.Tables["depts"].NewRow(); dr[0] = "0"; dr[1] = "请选择"; ds.Tables["depts"].Rows.InsertAt(dr, 0); //显示值 comboBox1.DisplayMember = "deptName"; //隐藏值 comboBox1.ValueMember = "deptId"; comboBox1.DataSource = ds.Tables["depts"]; mySqlConnection.Close(); } private void button1_Click(object sender, EventArgs e) { setDataGridView(); }
标签:状态栏,C#,void,private,tab,new,mySqlConnection,ds,string From: https://www.cnblogs.com/hmy22466/p/17822393.html