当前位置: 首页 > 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){}}
}

相关文章:

  • 【CPP】栈简介及简化模拟实现
  • C语言学习笔记之结构体(一)
  • Android 车载 Audio 中 有关系统按键无声的问题排查小结
  • qi5uxeel算法分析流程记录libmsec.so
  • 14.微信小程序之地理定位功能
  • OSG学习记录
  • uniapp条件编译
  • object对象列表使用sorted函数按照对象的某个字段排序
  • 运维开发.Kubernetes探针与应用
  • 详细介绍运算符重载函数,清晰明了
  • 使用matplotlib绘制折线条形复合图
  • C语言| 求1到100之间所有能被3整除的数之和
  • 小程序发成绩
  • FinalShell 配置SSH密钥登陆
  • 云阅读(小说系统)看小说,管理小说
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • Android 控件背景颜色处理
  • bootstrap创建登录注册页面
  • eclipse(luna)创建web工程
  • Git的一些常用操作
  • iOS小技巧之UIImagePickerController实现头像选择
  • Js基础知识(四) - js运行原理与机制
  • js正则,这点儿就够用了
  • Ruby 2.x 源代码分析:扩展 概述
  • Web标准制定过程
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 排序算法之--选择排序
  • 入手阿里云新服务器的部署NODE
  • 算法-插入排序
  • 我的业余项目总结
  • 一道面试题引发的“血案”
  • 一个完整Java Web项目背后的密码
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • $(this) 和 this 关键字在 jQuery 中有何不同?
  • $NOIp2018$劝退记
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (void) (_x == _y)的作用
  • (zhuan) 一些RL的文献(及笔记)
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (附源码)计算机毕业设计大学生兼职系统
  • (十五)使用Nexus创建Maven私服
  • (转)Oracle 9i 数据库设计指引全集(1)
  • **python多态
  • .gitignore文件设置了忽略但不生效
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .NET HttpWebRequest、WebClient、HttpClient
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .Net组件程序设计之线程、并发管理(一)
  • /*在DataTable中更新、删除数据*/
  • @javax.ws.rs Webservice注解
  • [.net]官方水晶报表的使用以演示下载
  • [8-23]知识梳理:文件系统、Bash基础特性、目录管理、文件管理、文本查看编辑处理...
  • [Asp.net MVC]Bundle合并,压缩js、css文件
  • [bzoj1901]: Zju2112 Dynamic Rankings
  • [C#]DataTable常用操作总结【转】