Click here to Skip to main content
15,305,508 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

Can anybody tell me how to avoid flickering of rectangle shape in windows form. I have used rectangle shape control in my form & i have a serial port communication in the same form, so when the communication is in process i just minimized the form & when i restored the rectangle shape flickers every 500ms since i have set the serial port to receive data every 500ms. So as & when i receive data it starts flickering. How to avoid such a thing...?

I have used this.doublebuffered = true; in the constructor &
C#
protected override CreateParams CreateParams
       {
           get
           {
               CreateParams cp = base.CreateParams;
               cp.ExStyle |= 0x02000000;
               return cp;
           }
       }


Still found no improvements, so plz help me out in resolving this issue.

Thanks in advance.

I have a mdi form in which i have opened 1 of the childform, containing rectangle shape, serial port communication & many other controls in it. What i do is, i will select the comport & press start button to start communicating through serial port. Once the communication starts the received data is processed & UI is updated accordingly. What i feel is while updating the UI the rectangle shape is being infected. The following is my code.

To fix the rectangle shape @ the centre of the screen i have used the following code:

C#
private void DisplayWindow_MaximumSizeChanged(object sender, EventArgs e)
  {
    rectangleShape1.Left = (Screen.PrimaryScreen.Bounds.Width / 2)-183;
    rectangleShape2.Left = (Screen.PrimaryScreen.Bounds.Width / 2) - 153;
  }

private void rectangleShape1_ParentChanged(object sender, EventArgs e)
  {
     rectangleShape1.Anchor = AnchorStyles.Top;
     rectangleShape2.Anchor = AnchorStyles.Top;
   }


Serial Port Comm part:

C#
private void comstartbtn_Click(object sender, EventArgs e)
        {
            sp.PortName = cmpprtcmbbox.SelectedItem.ToString();
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            timer = new System.Threading.Timer(new TimerCallback(Receive_Data), null, 0, 500);

            try
            {
                if (sp.IsOpen == false)
                {
                    sp.Open();
                    comstartbtn.Enabled = false;
                    cmpprtcmbbox.Enabled = false;
                    comstpbtn.Enabled = true;
                    ((MainWindow)this.MdiParent).toolStripStatusLabel1.Text = "STATUS : " + sp.PortName+ " Opened successfully";  

                }

            }
            catch (Exception ex)
            {

            }
        }
 
 private void Receive_Data(object obj)
    {           
            
            try
            {
                int btr = sp.BytesToRead;
                data_9 = new byte[btr];
                sp.Read(data_9, 0, btr);

                for (int i = 0; i < btr; i++)
                {
                    if (data_9[i] == 36 && start_collction == 0)
                    {
                        start_collction = 1;
                    }

                    if (start_collction == 1)
                    {
                        serialData.Append(data_9[i].ToString() + ","); 
                        if (serialData.ToString().Contains("65"))
                        {
                            start_collction = 0;
                            q = serialData.ToString().Split(',');
                            List<string> list = new List<string>(q);
                            list.Remove("65");
                            list.Remove("");

                            if (q[1] == "1") 
                            {
                             UV1ledbulb.Blink(0);
                             }
                           }
                         }


This UV1ledbulb lies on the rectangle shape.
Posted
Updated 18-Sep-13 20:36pm
v3
Comments
   
Can you create a minimized but complete code sample still manifesting this problem?
—SA
   
Oh... First, it's unreadable in the comment, use "Improve question". And is it really complete? I mean, it should compile and run. Do you know how to create UI in just one file, same as Main?..
—SA
Jagadisha_Ingenious 19-Sep-13 2:33am
   
@Sergey Alexandrovich Kryukov: Sir, I dont know how to create UI in one file. What is the use of it & how is it helpful in this case...?
   
Basically, by cutting code to bare minimum and not using the designer:

public class MyWindow : Window {/*...*/}

class Program {
void Main();
//...
Application.Run(new MyWindow());
}

—SA
Jagadisha_Ingenious 18-Sep-13 2:50am
   
@Sergey Alexandrovich Kryukov: Sir i forgot to mention that the flickering occurs when i minimize & restore the MDI Parentform containg the childform in it, & not when i minimize & restore the childform.
   
Oh, gosh. All I can say: get rid of MDI, this is old ugly lame. And what, are you minimizing and restoring the form every 500 ms? Why?
—SA
Jagadisha_Ingenious 19-Sep-13 2:31am
   
@Sergey Alexandrovich Kryukov: Sir i am not minimizing & restoring the form every 500ms i just did it once & found this type of flicker occurring while the communication is in process & if communication is stopped the flickering stops too.
   
Still not clear... But I tell you: don't use MDI.
—SA
Jagadisha_Ingenious 19-Sep-13 2:39am
   
@Sergey Alexandrovich Kryukov: Sir if not MDI, then any other alternatives for it...? Since i have multiple forms in my project...

Your code overriding CreateParams is irrelevant, and hard-coded immediate constant is bad. Instead, you can just use DoubleBuffered, or, alternatively, use SetStyle and set the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].

If this does not help, try to create a minimized complete code sample and post it. Please see my comment to the question.

—SA
   
Jagadisha_Ingenious asked:
Sir if not MDI, then any other alternatives for it…? Since i have multiple forms in my project…
Sure. I can explain what to do instead. Please see my past answers:

How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
   

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