C#-Win From开发-事件代码编写
C#-事件代码编写
控件事件生成
- 事件控件生成,在双击事件的时候,VS会自动生成初始化代码。并生成以你设定控件Name属性的名称,自动生成一个方法。
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 AlarmExportToExcel
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
控件内容获取
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 AlarmExportToExcel
{
public partial class Form1 : Form
{
//控件控制
public void show_Box(bool status) {
label5.Visible = status;
label4.Visible = status;
mianLineName.Visible = status;
deviceName.Visible = status;
}
public Form1()
{
InitializeComponent();
//默认隐藏控件
show_Box(false);
}
private void select_Click(object sender, EventArgs e)
{
//开始时间
string startime = startDateTimePicker.Text.ToString().Trim();
//结束时间
string endtime = endDateTimePicker.Text.ToString().Trim();
//获取ID
string lineName = mianLineName.Text.ToString().Trim();
//获取名称
string deviceNme = deviceName.Text.ToString().Trim();
//获取查询的数据名称
string dbNamePara = dbName.Text.ToString().Trim();
//DataSouce dataSouce = new DataSouce();
if (dbName.Text.ToString().Trim().Equals("")) {
MessageBox.Show("请选择数据库!", "错误");
return;
}
if (week.Checked || day.Checked || allAlarm.Checked || throwAndPut.Checked)
{
//如果选择周,参数校验
if (week.Checked) {
if (string.IsNullOrEmpty(lineName) || string.IsNullOrEmpty(deviceNme)) {
MessageBox.Show("名称或者ID不能为空!", "错误");
return;
}
}
DataTable result = DataSouce.executeQuery(dbNamePara, startime, endtime, week, day,allAlarm,throwAndPut,lineName, deviceNme);
//dataGridView1.Dock = System.Windows.Forms.DockStyle.Top;
if (result==null)
{
MessageBox.Show("查询数据异常,请稍后查询!", "错误");
}
//dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.DataSource = result;
}
else {
MessageBox.Show("请选择查询类型!","错误");
}
}
private void connectDB_Click(object sender, EventArgs e)
{
string DBName = dbName.Text.ToString().Trim();
try
{
DataSouce conn = new DataSouce();
result.Text = conn.testConnecting(DBName);
}
catch (Exception)
{
result.Text = "连接失败";
throw;
}
}
private void week_CheckedChanged(object sender, EventArgs e)
{
//显示控件
show_Box(true);
//允许编辑
mianLineName.Enabled = true;
deviceName.Enabled = true;
mianLineName.Focus();
}
private void day_CheckedChanged(object sender, EventArgs e)
{
//关闭显示
show_Box(false);
mianLineName.Enabled = false;
deviceName.Enabled = false;
}
private void toExcel_Click(object sender, EventArgs e)
{
DataSouce souce = new DataSouce();
souce.DataToExcel(dataGridView1);
}
private void allAlarm_CheckedChanged(object sender, EventArgs e)
{
//关闭显示
show_Box(false);
mianLineName.Enabled = false;
deviceName.Enabled = false;
}
private void throwAndPut_CheckedChanged(object sender, EventArgs e)
{
//关闭显示
show_Box(false);
mianLineName.Enabled = false;
deviceName.Enabled = false;
}
}
}
开开心心,上班!
快快乐乐,游玩!
及时行乐!