当前位置: 首页 > news >正文

C#根据数据量自动排版标签的样例

在这里插入图片描述
这是一个C#根据数据量自动排版标签的样例

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication.Profinet;namespace WindowsFormsApp1
{public partial class Form1 : Form{private CancellationTokenSource _cancellationTokenSource;private Dictionary<string, string> fieldMappings;public Form1(){InitializeComponent();InitializeFieldMappings();}private void Form1_Load(object sender, EventArgs e){}private void InitializeFieldMappings(){fieldMappings = new Dictionary<string, string>{{ "pltid", "平台ID" },{ "basketcode", "篮子编码" },{ "code", "编码" },{ "productCode", "产品编码" },{ "productName", "产品名称" },{ "customerCode", "客户编码" },{ "customerName", "客户名称" },{ "workordernumber", "工单编号" },{ "way", "方式" },{ "matnr", "物料编码" },{ "specs", "规格" },{ "dimension", "尺寸" },{ "equipmentnum", "设备编号" },{ "djBusCode", "业务代码" },{ "degEmCode", "异常代码" },{ "fqty", "数量" },{ "dinsert", "插入时间" },{ "lkstatus", "锁定状态" },{ "viewstatus", "查看状态" },{ "mst", "主状态" },{ "change_time", "变更时间" },{ "lgid", "lg的编号" },{ "lgmsg", "lg的信息" },{ "lgtime", "lg的时间" }};}private async void button1_Click(object sender, EventArgs e){_cancellationTokenSource = new CancellationTokenSource();string connectionString = "Server=ip;Database=root;User Id=1;Password=1;";string query = "SELECT * FROM [1].[1] where viewstatus=1";bool isConnected = await Task.Run(() => TestDatabaseConnection(connectionString));if (isConnected){textBox2.BackColor = Color.Green;label1.Text = "已连接";await Task.Run(() => MonitorDatabase(connectionString, query, _cancellationTokenSource.Token));}else{textBox2.BackColor = Color.Red;label1.Text = "连接失败";}}private bool TestDatabaseConnection(string connectionString){try{using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();return true;}}catch{return false;}}private void MonitorDatabase(string connectionString, string query, CancellationToken token){DateTime lastCheck = DateTime.Now;while (!token.IsCancellationRequested){using (SqlConnection connection = new SqlConnection(connectionString)){SqlCommand command = new SqlCommand(query, connection);command.Parameters.AddWithValue("@lastCheck", lastCheck);try{connection.Open();SqlDataReader reader = command.ExecuteReader();if (reader.HasRows){while (reader.Read()){StringBuilder result = new StringBuilder();for (int i = 0; i < reader.FieldCount; i++){string fieldName = reader.GetName(i);string fieldValue = reader[i].ToString();if (fieldMappings.ContainsKey(fieldName)){result.AppendLine($"{fieldMappings[fieldName]}: {fieldValue}");}else{result.AppendLine($"{fieldName}: {fieldValue}");}}UpdateUIWithResult(result.ToString(), reader);}lastCheck = DateTime.Now;}reader.Close();}catch (Exception ex){UpdateUIWithResult("Error: " + ex.Message, null);}}Thread.Sleep(1000); // 1 second interval}}private void UpdateUIWithResult(string text, SqlDataReader reader){if (InvokeRequired){Invoke(new Action(() => CreateLabelsForData(reader)));}else{CreateLabelsForData(reader);}}private void CreateLabelsForData(SqlDataReader reader){flowLayoutPanel1.Controls.Clear(); // 清空之前的控件int yOffset = 0; // 纵向偏移量for (int i = 0; i < reader.FieldCount; i++){string fieldName = reader.GetName(i);string fieldValue = reader[i].ToString();// 创建标题标签Label titleLabel = new Label{AutoSize = false,Width = 200,Height = 40,Location = new Point(0, yOffset),ForeColor = Color.Red,BackColor = Color.Black,Padding = new Padding(5),Font = new Font("Arial", 18, FontStyle.Bold),Text = fieldMappings.ContainsKey(fieldName) ? fieldMappings[fieldName] : fieldName};// 创建内容标签Label valueLabel = new Label{AutoSize = false,Width = 200,Height = 40,Location = new Point(210, yOffset),ForeColor = Color.Green,BackColor = Color.Black,Padding = new Padding(5),Font = new Font("Arial", 18, FontStyle.Regular),Text = fieldValue};// 将标题和内容标签添加到PanelflowLayoutPanel1.Controls.Add(titleLabel);flowLayoutPanel1.Controls.Add(valueLabel);yOffset += 50; // 更新纵向偏移量}}private void Form1_FormClosing(object sender, FormClosingEventArgs e){_cancellationTokenSource?.Cancel();}private void label1_Click(object sender, EventArgs e){}private void label2_Click(object sender, EventArgs e){}private void button5_Click(object sender, EventArgs e){_cancellationTokenSource?.Cancel();textBox2.BackColor = Color.WhiteSmoke; // 或者其他颜色表示断开连接label1.Text = "未连接";}private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e){}}
}
http://www.lryc.cn/news/359460.html

相关文章:

  • 【网络安全】Web安全基础 - 第一节:使用软件及环境介绍
  • Mac下载docker
  • k8s_设置dns
  • 翻译《The Old New Thing》- What a drag: Dragging a virtual file (HGLOBAL edition)
  • SA316系列音频传输模块-传输距离升级音质不打折
  • 【机器学习】智能选择的艺术:决策树在机器学习中的深度剖析
  • 电脑缺少运行库,无法启动程序
  • 【计算机软考_初级篇】每日十题2
  • HR人才测评,如何做营销人员岗位素质测评?
  • LabVIEW调用第三方硬件DLL常见问题及开发流程
  • datax实现MySQL数据库迁移shell自动化脚本
  • PostgreSQL的学习心得和知识总结(一百四十四)|深入理解PostgreSQL数据库之sendTuples的实现原理及功能修改
  • C++数据结构之:链List
  • 10.Redis之set类型
  • SpringBoot + mongodb 删除集合中的数据
  • 【日常记录】【JS】前端预览图片的两种方式,Base64预览和blob预览
  • 每日刷题——杭电2156.分数矩阵和杭电2024.C语言合法标识符
  • 爬虫学习--18.反爬斗争 selenium(3)
  • 如何评价GPT-4o?
  • 算能BM1684+FPGA+AI+Camera推理边缘计算盒
  • 不同厂商SOC芯片在视频记录仪领域的应用
  • 【Python入门学习笔记】Python3超详细的入门学习笔记,非常详细(适合小白入门学习)
  • 通用代码生成器应用场景三,遗留项目反向工程
  • 轻量级动态可监控线程池 - DynamicTp
  • 对于vsc中的vue命令 vue.json
  • Spring Boot 官方不再支持 Spring Boot 的 2.x 版本!新idea如何创建java8项目
  • 分享一个 ASP.NET Web Api 上传和读取 Excel的方案
  • 【算法实战】每日一题:将某个序列中内的每个元素都设为相同的值的最短次数(差分数组解法,附概念理解以及实战操作)
  • EXCEL数据透视图中的日期字段,怎样自动分出年、季度、月的功能?
  • 【设计模式深度剖析】【1】【行为型】【模板方法模式】| 以烹饪过程为例加深理解