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 Form1()
{
InitializeComponent();
}
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)
{
mianLineName.Enabled = true;
deviceName.Enabled = true;
mianLineName.Focus();
}
private void day_CheckedChanged(object sender, EventArgs e)
{
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)
{
mianLineName.Enabled = false;
deviceName.Enabled = false;
}
private void throwAndPut_CheckedChanged(object sender, EventArgs e)
{
mianLineName.Enabled = false;
deviceName.Enabled = false;
}
}
}
标签:string,编写,C#,Win,void,System,EventArgs,Text,using 来源: https://www.cnblogs.com/caicai920/p/16283684.html