WinForm LED循环显示信息,使用定时器Threading.Timer

2020-11-20 10:48

阅读:627

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsThreading
{
public partial class FormTimer : Form
{

//线程暂停时间 单位:毫秒
static int _timespan = 2000;

//每页显示行数 范围1-5
static int _pagerowcount = 4;

//每行固定显示5个
static int _pagerownumber = 5;

//总页数
static int _pagetotal = 0;

//当前页
static int _pageindex = 1;

static int _rowindex = 0;

//获取正在处理列表
private static IListstring> _namelist;

//声明定时器
private static System.Threading.Timer _timer;

//定义委托
private delegate void SetTLPNameListDelegate();

//声明委托变量
private SetTLPNameListDelegate _settlpnamelist;

public FormTimer()
{
InitializeComponent();
_settlpnamelist = LoadLEDForm;//设置委托变量的方法
BeginTimer();
}

private void BeginTimer()
{
//TimerCallback 委托,指定timer定时器需要执行的方法 ThreadMethod
TimerCallback tmrcallback = new TimerCallback(ThreadMethod);
//示例化定时器
_timer = new System.Threading.Timer(tmrcallback, null, 0, _timespan);
}

private void ThreadMethod(object state)
{
if (this.InvokeRequired)
this.Invoke(_settlpnamelist);
}

///


/// 设置正在处理列表
///
private void LoadLEDForm()
{
SetQueueOrder();
SetName();
SetNameList();
}
///
/// 设置等待列表
///
private void SetNameList()
{
if (_pageindex == 1)
{
GetNameList();
_pagetotal = (int)Math.Ceiling((float)_namelist.Count / (_pagerowcount * _pagerownumber));

if (_pagetotal 1) { _pagetotal = 1; }
}
if (_namelist != null && _namelist.Count > 0)
{
int column = 0;
tlpaNameList.Controls.Clear();
_rowindex = 0;
int i = 0;
int startIndex = (_pageindex - 1) * (_pagerowcount * _pagerownumber);
int endIndex = _pageindex * (_pagerowcount * _pagerownumber);
for (; i {
if (i >= startIndex && i {
if (_rowindex > _pagerowcount - 1)
break;
if (column > _pagerownumber - 1)
{
column = 0;
_rowindex++;
}
Addlabel(_namelist[i], column);
column++;
}
}
lblPageNumber.Text = string.Format("页码 {0}/{1}", _pageindex, _pagetotal);
_pageindex++;
if (_pageindex > _pagetotal)
{
_pageindex = 1;
}
}
}

///


/// 设置排队叫号
///
private void SetQueueOrder()
{
lblOrder.Text = _pageindex + "";
}

///


/// 设置催缴费
///
private void SetName()
{
lblName.Text = "唐鑫瑞" + _pageindex;
}

private void Addlabel(String text, int columnindex)
{
Label label = new Label();
label.Dock = DockStyle.Top;
label.ForeColor = Color.Yellow;
label.BackColor = Color.Black;
label.Font = new System.Drawing.Font("宋体", 12F);
label.Text = text;
label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
tlpaNameList.Controls.Add(label, columnindex, _rowindex);
}

///


/// 模拟一个列表(列表中有30个对象),每个列表显示完成后,自动新增一个对象,用来模拟等待列表的人数在增加
///
public void GetNameList()
{
if (_namelist == null || _namelist.Count 1)
{
_namelist = new Liststring>();
for (int i = 1; i 31; i++)
{
_namelist.Add("唐鑫瑞" + i);
}
}
else
{
if (_pageindex == 1)
_namelist.Add("唐鑫瑞" + (_namelist.Count + 1));
}
}

private void FormTimer_Load(object sender, EventArgs e)
{

}
}
}


评论


亲,登录后才可以留言!