涉及:控件数据绑定,动画效果

![]()
using System;
using System.Windows.Forms;
namespace PLCUI
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 方式2:基于事件的方式,将控件和数据进行绑定,优点:在任何地方改变变量的值,所绑定的控件也能同时改变
lblValue_D100.DataBindings.Add("Text", Program.PlcData, "D100");
ucRightBelt1.DataBindings.Add("Data", Program.PlcData, "D100");
}
private void btnSimulate_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// 模拟数据
Program.PlcData.D100++;
if (Program.PlcData.D100 > 15)
{
Program.PlcData.D100 = 0;
}
// 方式1:基于轮询的方式,查询到plc数据,然后进行更新
//lblValue_D100.Text = Program.PlcData.D100.ToString();
//int status = 0;
//if (Program.PlcData.D100 >= 0 && Program.PlcData.D100 <= 5)
//{
// status = 0;
//}
//else if (Program.PlcData.D100 > 5 && Program.PlcData.D100 <= 10)
//{
// status = 1;
//}
//else if (Program.PlcData.D100 > 10 && Program.PlcData.D100 <= 15)
//{
// status = 2;
//}
//ucRightBelt1.ShowImage(status);
}
}
}
![]()
![]()
using System.ComponentModel;
namespace PLCUI
{
public class PLCData : INotifyPropertyChanged
{
static int d100;
static int d101;
public int D100
{
get { return d100; }
set
{
d100 = value;
OnPropertyChanged(nameof(D100));
}
}
public int D101
{
get { return d101; }
set
{
d101 = value;
OnPropertyChanged(nameof(D101));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
![]()
![]()
using System;
using System.Windows.Forms;
namespace PLCUI.customcontrol
{
public partial class ucRightBelt : UserControl
{
int temp = 0;
public ucRightBelt()
{
InitializeComponent();
}
public object data;
public object Data
{
get
{
return data;
}
set
{
data = value;
int plcdata = (int)data; int status = 0;
if (plcdata >= 0 && plcdata <= 5)
{
status = 0;
}
else if (plcdata > 5 && plcdata <= 10)
{
status = 1;
}
else if (plcdata > 10 && plcdata <= 15)
{
status = 2;
}
ShowImage(status);
}
}
public void ShowImage(int status)
{
if (temp == status) return;
temp = status;
this.pictureBox1.Invoke(new Action(() =>
{
switch(status)
{
case 0:
this.pictureBox1.Image = Properties.Resources.右皮带机待机;
break;
case 1:
this.pictureBox1.Image = Properties.Resources.右皮带机工作1;
break;
case 2:
this.pictureBox1.Image = Properties.Resources.右皮带机工作2;
break;
}
}));
}
}
}



















