首次提交
This commit is contained in:
184
SCADA/Pages/CrudHelper.cs
Normal file
184
SCADA/Pages/CrudHelper.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace MesWork.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// CRUD页面通用辅助方法(搜索框交互、弹窗控件创建、Excel导出)
|
||||
/// </summary>
|
||||
public static class CrudHelper
|
||||
{
|
||||
// ====================================================================
|
||||
// 搜索框占位提示交互
|
||||
// ====================================================================
|
||||
|
||||
/// <summary>
|
||||
/// 搜索框获得焦点时清除占位提示
|
||||
/// </summary>
|
||||
public static void SearchBox_GotFocus(TextBox txt, string hint)
|
||||
{
|
||||
if (txt.Text == hint) { txt.Text = ""; txt.ForeColor = Color.FromArgb(31, 41, 55); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 搜索框失去焦点时恢复占位提示
|
||||
/// </summary>
|
||||
public static void SearchBox_LostFocus(TextBox txt, string hint)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(txt.Text)) { txt.Text = hint; txt.ForeColor = Color.FromArgb(156, 163, 175); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 搜索框回车触发搜索
|
||||
/// </summary>
|
||||
public static bool SearchBox_KeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取搜索框的实际搜索关键词(排除占位提示)
|
||||
/// </summary>
|
||||
public static string GetSearchKeyword(TextBox txt, string hint)
|
||||
{
|
||||
return txt.Text == hint ? "" : txt.Text.Trim();
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// 编辑弹窗辅助
|
||||
// ====================================================================
|
||||
|
||||
/// <summary>
|
||||
/// 创建标准编辑弹窗框架(统一样式 + 确定/取消按钮)
|
||||
/// </summary>
|
||||
public static Form CreateEditDialog(string title, int width, int height)
|
||||
{
|
||||
var dlg = new Form
|
||||
{
|
||||
Text = title,
|
||||
Size = new Size(width, height),
|
||||
StartPosition = FormStartPosition.CenterParent,
|
||||
FormBorderStyle = FormBorderStyle.FixedDialog,
|
||||
MaximizeBox = false,
|
||||
MinimizeBox = false,
|
||||
BackColor = Color.White,
|
||||
Font = new Font("微软雅黑", 11F)
|
||||
};
|
||||
return dlg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向弹窗添加确定/取消按钮
|
||||
/// </summary>
|
||||
public static (Button btnOk, Button btnCancel) AddDialogButtons(Form dlg, ref int y)
|
||||
{
|
||||
y += 16;
|
||||
var btnOk = new Button
|
||||
{
|
||||
Text = "确 定", BackColor = Color.FromArgb(74, 144, 217), ForeColor = Color.White,
|
||||
FlatStyle = FlatStyle.Flat, Size = new Size(120, 36), Location = new Point(80, y),
|
||||
DialogResult = DialogResult.OK
|
||||
};
|
||||
btnOk.FlatAppearance.BorderSize = 0;
|
||||
|
||||
var btnCancel = new Button
|
||||
{
|
||||
Text = "取 消", BackColor = Color.FromArgb(229, 231, 235), ForeColor = Color.FromArgb(55, 65, 81),
|
||||
FlatStyle = FlatStyle.Flat, Size = new Size(120, 36), Location = new Point(220, y),
|
||||
DialogResult = DialogResult.Cancel
|
||||
};
|
||||
btnCancel.FlatAppearance.BorderSize = 0;
|
||||
|
||||
dlg.Controls.Add(btnOk);
|
||||
dlg.Controls.Add(btnCancel);
|
||||
dlg.AcceptButton = btnOk;
|
||||
dlg.CancelButton = btnCancel;
|
||||
return (btnOk, btnCancel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向弹窗添加文本输入字段
|
||||
/// </summary>
|
||||
public static TextBox AddTextField(Form dlg, string label, ref int y, string value, int labelX = 30, int inputX = 130, int inputWidth = 240, bool isPassword = false)
|
||||
{
|
||||
dlg.Controls.Add(new Label { Text = label, Location = new Point(labelX, y + 4), AutoSize = true });
|
||||
var txt = new TextBox { Text = value ?? "", Location = new Point(inputX, y), Size = new Size(inputWidth, 28), BorderStyle = BorderStyle.FixedSingle };
|
||||
if (isPassword) txt.PasswordChar = '*';
|
||||
dlg.Controls.Add(txt);
|
||||
y += 42;
|
||||
return txt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向弹窗添加下拉选择字段
|
||||
/// </summary>
|
||||
public static ComboBox AddComboField(Form dlg, string label, ref int y, string[] items, string selected, int labelX = 30, int inputX = 130, int inputWidth = 240)
|
||||
{
|
||||
dlg.Controls.Add(new Label { Text = label, Location = new Point(labelX, y + 4), AutoSize = true });
|
||||
var cmb = new ComboBox { Location = new Point(inputX, y), Size = new Size(inputWidth, 28), DropDownStyle = ComboBoxStyle.DropDownList };
|
||||
cmb.Items.AddRange(items);
|
||||
cmb.Text = selected ?? items[0];
|
||||
dlg.Controls.Add(cmb);
|
||||
y += 42;
|
||||
return cmb;
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// 导出Excel通用方法
|
||||
// ====================================================================
|
||||
|
||||
/// <summary>
|
||||
/// 通用的Excel导出流程(弹出保存对话框→查全量数据→NPOI导出→打开文件位置)
|
||||
/// </summary>
|
||||
/// <param name="sheetName">Excel工作表名称(如"工件管理")</param>
|
||||
/// <param name="defaultFileName">默认文件名(如"工件管理",会自动加时间后缀)</param>
|
||||
/// <param name="queryAllData">查询全量数据的委托,返回DataTable</param>
|
||||
/// <param name="hasData">当前是否有数据可导出</param>
|
||||
public static void ExportToExcel(string sheetName, string defaultFileName, Func<DataTable> queryAllData, bool hasData)
|
||||
{
|
||||
if (!hasData)
|
||||
{
|
||||
MessageBox.Show("没有数据可导出,请先查询", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
return;
|
||||
}
|
||||
using (var sfd = new SaveFileDialog())
|
||||
{
|
||||
sfd.Filter = "Excel 文件|*.xlsx";
|
||||
sfd.FileName = $"{defaultFileName}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
|
||||
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
||||
if (sfd.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
DataTable dt = queryAllData();
|
||||
if (dt != null && dt.Rows.Count > 0)
|
||||
{
|
||||
bool exported = NPOITest.ExeclHelper.DataTableToExcel(dt, sheetName, sfd.FileName);
|
||||
if (exported)
|
||||
{
|
||||
MessageBox.Show($"导出成功!共 {dt.Rows.Count} 条记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{sfd.FileName}\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("导出失败,请检查文件是否被占用", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("查询全量数据失败,请重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user