如何控制C#windows应用程序中的闪烁问题以显示串口数据? [英] How to control flickering problem in C# windows application to display data from serial port?
问题描述
大家好,
我在串口显示标签中的数据。数据显示正常,但数据闪烁的标签控件。如何停止这个闪烁的问题是为了正确显示数据。
这是我的代码 -
Hello all,
I am showing data in label from serial port.Data display is OK but the label control on which data is flickering.How can i stop this flickering problem to display data properly.
Here is my code-
private void button1_Click(object sender, EventArgs e)
{
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
SerialPort sp = (SerialPort)sender;
label1.Text = sp.ReadExisting();
mySerialPort.Open();
mySerialPort.Close();
}
这里是新代码 -
here is the new code--
public partial class MainForm : Form
{
SerialPortManager _spManager;
public MainForm()
{
InitializeComponent();
DoubleBuffered = true;
UserInitialization();
}
private void UserInitialization()
{
_spManager = new SerialPortManager();
SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
serialSettingsBindingSource.DataSource = mySerialSettings;
portNameComboBox.DataSource = mySerialSettings.PortNameCollection;
baudRateComboBox.DataSource = mySerialSettings.BaudRateCollection;
dataBitsComboBox.DataSource = mySerialSettings.DataBitsCollection;
parityComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_spManager.Dispose();
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
int maxTextLength = 1000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
string str = Encoding.ASCII.GetString(e.Data);
tbData.AppendText(str);
tbData.ScrollToCaret();
DoubleBuffered = true;
label1.Text = str;
}
private void btnStart_Click(object sender, EventArgs e)
{
_spManager.StartListening();
}
private void btnStop_Click(object sender, EventArgs e)
{
_spManager.StopListening();
}
}
< br $>
谢谢
我尝试了什么:
我用过以下代码 -
protected override CreateParams CreateParams {
得到{
CreateParams cp = base.CreateParams;
cp.ExStyle | = 0x02000000; //打开WS_EX_COMPOSITED
返回cp;
}
}
Thanks
What I have tried:
I have used below code-
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
推荐答案
尝试将DoubleBuffered设置为true:控制。 DoubleBuffered属性(System.Windows.Forms) [ ^ ] - 从标签开始,然后是表格。
Try setting DoubleBuffered to true: Control.DoubleBuffered Property (System.Windows.Forms)[^] - start with the label, and then the form.
这篇关于如何控制C#windows应用程序中的闪烁问题以显示串口数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!