Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
        }
    }







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;
}
}
Posted
Updated 22-Sep-16 0:05am
v2

1 solution

Try setting DoubleBuffered to true: Control.DoubleBuffered Property (System.Windows.Forms)[^] - start with the label, and then the form.
 
Share this answer
 
Comments
Ujjval Shukla 22-Sep-16 3:54am    
thanks for the reply OriginalGriff but problem is same after used doubleBuffered....
OriginalGriff 22-Sep-16 4:06am    
Did you remove the CreateParams override?
Ujjval Shukla 22-Sep-16 5:31am    
yes i have removed CreateParams override.
OriginalGriff 22-Sep-16 5:40am    
Start looking at where else you affect that label: the code you show is only on a button click, and won't actually show any data (because you read it before you open it) - so I'd suspect the data comes from somewhere else, or you aren't showing us the "real" code.
Ujjval Shukla 22-Sep-16 5:48am    
Here is the 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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900