0

i have little app where I have one panel containing 6 more panels (inside this panels pictures), and when i click on the button, botton this panel dynamically created couple panels, and when panels create whole this panels are blinking... it's looks not good... please, tell me, what to do?

a cycle where I created the panel, clicking on a button to created panel adds the same number of panels

for (int i = 0; i < list_afy_add.Count; i++)
{
    Panel main_panel = new Panel();
    main_panel.Name = i.ToString();
    main_panel.Width = 308;
    main_panel.BackColor = Color.Transparent;
    main_panel.Location = new Point(x, y);

    Panel panel = new Panel();
    panel.Name = i.ToString();
    panel.MouseEnter += new EventHandler(panel_MouseEnter);
    panel.Width = 300;
    panel.Location = new Point(3, 5);

    Label textBox_date = new Label();
    panel.Controls.Add(textBox_date);
    textBox_date.Name = "textBox_date" + i.ToString();
    textBox_date.Location = new Point(220, 8);
    textBox_date.Size = new System.Drawing.Size(70, 15);
    textBox_date.BorderStyle = BorderStyle.None;
    textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
    textBox_date.TextAlign = ContentAlignment.MiddleRight;
    textBox_date.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
    textBox_date.ForeColor = SystemColors.InactiveCaption;

    Label textBox_name = new Label();
    panel.Controls.Add(textBox_name); 
    textBox_name.Size = new System.Drawing.Size(100, 15);
    textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
    textBox_name.Location = new Point(5, 8);
    textBox_name.BorderStyle = BorderStyle.None;
    textBox_name.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_name.ForeColor = SystemColors.InactiveCaption;

    Label textBox_msg = new Label();
    panel.Controls.Add(textBox_msg);
    textBox_msg.Location = new Point(5, 30);
    textBox_msg.Name = i.ToString();
    textBox_msg.Tag = list_afy_add[i]._AddEventNotification.eventNotificationId;
    textBox_msg.BorderStyle = BorderStyle.None;
    textBox_msg.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    textBox_msg.ForeColor = SystemColors.Highlight;
    textBox_msg.Cursor = Cursors.Arrow;
    textBox_msg.MaximumSize = new System.Drawing.Size(280, 100);
    textBox_msg.MinimumSize = new System.Drawing.Size(280, 14);
    textBox_msg.AutoSize = true;
    textBox_msg.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;

    Label labelOpenChat = new Label(); 
    labelOpenChat.Size = new System.Drawing.Size(90, 15);
    labelOpenChat.Text = "Открыть чат";
    labelOpenChat.Font = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(200)));
    labelOpenChat.Location = new Point(10, 8);
    labelOpenChat.BorderStyle = BorderStyle.None;
    labelOpenChat.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
    labelOpenChat.ForeColor = SystemColors.InactiveCaption;
    panel.Controls.Add(labelOpenChat);


    main_panel.Controls.Add(panel);
    panel1.Controls.Add(main_panel);
    panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
    main_panel.Dock = DockStyle.Top;

    date_and_msg_check(list_afy_add[i]._AddEventNotification.eventNotificationName, list_afy_add[i]._AddEventNotification.eventNotificationDate, list_afy_add[i]._AddEventNotification.eventNotificationMsg); 
    textBox_name.Text = valForName; 
    textBox_date.Text = valForDate;
    textBox_msg.Text = valForMsg;

    int height = textBox_msg.Size.Height + textBox_name.Height + 19;
    panel.Height = height; 
    main_panel.Height = height + 10; 
    int panHei = panel.Height;


    /*--------------------------------*/
    Panel panel_top_left = new Panel();
    Panel panel_top_right = new Panel();
    Panel panel_bottom_left = new Panel();
    Panel panel_bottom_right = new Panel();

    Bitmap btm_msg_panel_top_left = new Bitmap(Properties.Resources.blue_t_l1);
    panel_top_left.BackgroundImage = btm_msg_panel_top_left;
    Bitmap btm_msg_panel_top_right = new Bitmap(Properties.Resources.blue_t_r);
    panel_top_right.BackgroundImage = btm_msg_panel_top_right;
    Bitmap btm_msg_panel_bottom_left = new Bitmap(Properties.Resources.blue_b_l);
    panel_bottom_left.BackgroundImage = btm_msg_panel_bottom_left;
    Bitmap btm_msg_panel_bottom_right = new Bitmap(Properties.Resources.blue_b_r);
    panel_bottom_right.BackgroundImage = btm_msg_panel_bottom_right;

    panel_top_left.Width = panel.Width;
    panel_top_left.Height = height + 2;
    panel_top_right.Width = btm_msg_panel_top_right.Width;
    panel_bottom_left.Height = btm_msg_panel_bottom_left.Height;
    panel_bottom_left.Width = btm_msg_panel_bottom_left.Width;
    panel_bottom_right.Height = btm_msg_panel_bottom_right.Height;
    panel_bottom_right.Width = btm_msg_panel_bottom_right.Width;

    panel_top_right.Location = new Point(panel_top_left.Width - 4, 0);
    panel_bottom_left.Location = new Point(0, panel_top_left.Height - panel_bottom_left.Height);
    panel_bottom_right.Location = new Point(panel_top_left.Width - 4, panel_top_left.Height - panel_bottom_right.Height);


    panel_top_right.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    panel_bottom_right.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
    panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
    panel_top_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    /*--------------------------------*/


    Button delButt = new Button();
    delButt.Name = list_afy_add[i]._AddEventNotification.eventNotificationId;
    delButt.Click += new EventHandler(delButt_Click);
    delButt.Size = new System.Drawing.Size(12, 12);
    delButt.Location = new Point(292, 6);
    delButt.Anchor = AnchorStyles.Right | AnchorStyles.Top;
    delButt.BackColor = Color.Transparent;
    delButt.FlatStyle = FlatStyle.Flat;
    delButt.FlatAppearance.BorderSize = 0;
    delButt.FlatAppearance.MouseDownBackColor = Color.Transparent;
    delButt.FlatAppearance.MouseOverBackColor = Color.Transparent;
    delButt.BackgroundImage = bmp_close_normal;
    panel_top_left.Controls.Add(delButt);
    delButt.Hide();

    panel.Controls.Add(panel_bottom_right);
    panel.Controls.Add(panel_top_right);
    panel.Controls.Add(panel_bottom_left);
    panel.Controls.Add(panel_top_left);
}
dandan78
  • 13,328
  • 13
  • 64
  • 78
zuh4n
  • 448
  • 1
  • 4
  • 11
  • Welcome to the world of overlapping paint calls, you can try setting double buffer property to true, set the extended style to ClipChildren, or basically just try and see if there is a better layout you can use, or [this](http://stackoverflow.com/q/487661/1324033) – Sayse Sep 18 '13 at 13:27
  • First off, I'd replace "main_panel" with a `TableLayoutPanel`...it'll make layout and positioning much easier to control. Beyond that, `DoubleBuffered` is a good idea. You might even consider taking over the `Paint` yourself. – DonBoitnott Sep 18 '13 at 13:29
  • Thank's, but I tried DoubleBuffered... doesn't help.. – zuh4n Sep 18 '13 at 13:31
  • @Zuhan what exactly have you tried? setting `DoubleBuffered` to `true` or using `TableLayoutPanel`? – King King Sep 18 '13 at 13:32
  • @DonBoitnott , probably it's good think, but i alredy finish to positioning panels... so, i don't want make code refactoring :) – zuh4n Sep 18 '13 at 13:34
  • @KingKing setting __DoubleBuffered__ to __true__ – zuh4n Sep 18 '13 at 13:36
  • @Zuhan how do you set the `DoubleBuffered` to true? I doubt you don't know how to do that. You should set `DoubleBuffered` for both the `container` and the `panels`. `DoubleBuffered` is exposed only from `Form` class, other kinds of control don't expose that `protected` property. – King King Sep 18 '13 at 13:41
  • @KingKing This panels created and showing on the __UserControl__, so i set __DoubleBuffered__ to __true__ at the constructor __UserContril__. It's what you mean? – zuh4n Sep 18 '13 at 13:45
  • 2
    @Zuhan that's just for the `UserControl` (the container), what about your `panels`? – King King Sep 18 '13 at 13:46
  • @KingKing can you please show me example?? – zuh4n Sep 18 '13 at 13:53
  • ahhhh.... who vote my post "-1" ??? what the problems ?? – zuh4n Sep 18 '13 at 13:54
  • @Zuhan see my temporary answer if it helps. – King King Sep 18 '13 at 13:58

2 Answers2

1

You can try using Reflection to set DoubleBuffered to true for all your panels like this:

public static class PanelExtensions {
   public static void EnableDoubleBuffered(this Panel panel){
     typeof(Panel).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance)
                  .SetValue(panel, true, null);
   }
}
//Use it
yourPanel.EnableDoubleBuffered();

NOTE: I'm not sure if this works, just post here to help you try some approach which can solve your problem, otherwise I have no idea to make it better and will remove this answer.

King King
  • 61,710
  • 16
  • 105
  • 130
0

Try to use the Control's SuspendLayout() method before adding the child controls, and don't forget to call the ResumeLayout() after you have finished. This should help to reduce or eliminate the blinking.

toderik
  • 479
  • 4
  • 8
  • The others mentioned making the panels double buffered. Does something like this help anything: http://gregback.net/double-buffered-panels-in-c.html ? – toderik Sep 18 '13 at 13:49