I have this custom control which is basically a panel:
class ResultPanel : Panel {
Label scoreValueLabel = new Label();
public ResultPanel() : base(){
scoreValueLabel.AutoSize = true;
scoreValueLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
scoreValueLabel.Location = new System.Drawing.Point(265, 99);
scoreValueLabel.Name = "scoreValueLabel";
scoreValueLabel.Size = new System.Drawing.Size(49, 25);
scoreValueLabel.TabIndex = 10;
scoreValueLabel.Text = "+10";
Controls.Add(scoreValueLabel);
}
}
And I'm trying to add it to a panel in an event handler:
private void ResultsReceivedHandler(object sender, List<QuestionResult> results) {
ResultPanel resultPanel = new ResultPanel();
allResultsPanel.Controls.Add(new ResultPanel());
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
}
I know that an instance of ResultPanel can be displayed in allResultsPanel because I have added(using designer view) a ResultPanel to allResultsPanel that has the same size as this one at the top of allResultsPanel and that displays.
allResultsPanel is just a normal Panel btw, and its big enough to fit the control because its height is 800.
So why can i see the control added through the design view but not one added dynamically?
While setting up resultPanel:
ResultPanel resultPanel = new ResultPanel();
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
You are adding another new panel to the allResultsPanel
allResultsPanel.Controls.Add(new ResultPanel());
Related
I am new to Windows Forms and am trying to create a custom Panel which has a header bar with two labels. I want to be able to set the text of those labels from the designer when this panel is added to another form.
Here is the code for my Panel:
public partial class TitledPanel : Panel
{
public string Title { get; set; } = "";
public string TitleNumber
{
get { return numberedLabel.Text; }
set { numberedLabel.Text = value; }
}
public TitledPanel()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(
new Pen(Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))))), 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
private void InitializeComponent()
{
this.titleLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel = new System.Windows.Forms.Panel();
this.numberedLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel.SuspendLayout();
this.SuspendLayout();
//
// titleLabel
//
this.titleLabel.AutoSize = true;
this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.titleLabel.Location = new System.Drawing.Point(75, 27);
this.titleLabel.Margin = new System.Windows.Forms.Padding(32,32,32,32);
this.titleLabel.Name = "titleLabel";
this.titleLabel.Size = new System.Drawing.Size(193, 30);
this.titleLabel.TabIndex = 2;
this.titleLabel.Text = "";
//
// mainHeaderPanel
//
this.mainHeaderPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))));
this.mainHeaderPanel.Controls.Add(this.numberedLabel);
this.mainHeaderPanel.Controls.Add(this.titleLabel);
this.mainHeaderPanel.Dock = System.Windows.Forms.DockStyle.Top;
this.mainHeaderPanel.ForeColor = System.Drawing.SystemColors.ControlText;
this.mainHeaderPanel.Location = new System.Drawing.Point(64, 140);
this.mainHeaderPanel.Margin = new System.Windows.Forms.Padding(64, 140, 64, 0);
this.mainHeaderPanel.Name = "mainHeaderPanel";
this.mainHeaderPanel.Size = new System.Drawing.Size(1126, 80);
this.mainHeaderPanel.TabIndex = 4;
//
// numberedLabel
//
this.numberedLabel.AutoSize = true;
this.numberedLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(4)))), ((int)(((byte)(103)))), ((int)(((byte)(198)))));
this.numberedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.numberedLabel.ForeColor = System.Drawing.Color.White;
this.numberedLabel.Location = new System.Drawing.Point(23, 26);
this.numberedLabel.Name = "numberedLabel";
this.numberedLabel.Size = new System.Drawing.Size(31, 32);
this.numberedLabel.TabIndex = 3;
this.numberedLabel.Text = "0";
var path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(-1, 0, numberedLabel.Width, numberedLabel.Height);
this.numberedLabel.Region = new Region(path);
//
// TitledPanel
//
this.AutoSize = true;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.mainHeaderPanel);
this.Name = "BeforeYouStartForm";
this.Size = new System.Drawing.Size(1254, 839);
this.Text = "GetStarted";
this.mainHeaderPanel.ResumeLayout(false);
this.mainHeaderPanel.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.Panel mainHeaderPanel;
private System.Windows.Forms.Label numberedLabel;
}
When I put this panel on a form and run the app, it looks like it should. However the fields Title and TitleNumber show up in the designer with the text Object reference not set to an instance of an object. next to them. Also the panel does not load in the designer, when I add it to a form, it looks like the default Panel would, and it only loads when I actually run the app.
How can I get the TitledPanel to load in the designer and how can I get Title and TitleNumber to show correctly in the designer.
Thanks
I'm working in a c# application in winform.
I saw that there is no Style for element (unlike WPF). But is there a way to simply set all the labels to a specific design ?
Actually I do :
public partial class myControl : UserControl
{
private Color LabelColor = Color.Indigo;
private Color LabelFont = new System.Drawing.Font("Arial",
18F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
public myControl()
{
InitializeComponent();
//Set design
designLabels();
}
private void designLabels()
{
List<Label> labelsToStyle = new List<Label>();
labelsToStyle.Add(labelName);
labelsToStyle.Add(labelAge);
labelsToStyle.Add(labelSize);
foreach (Label l in labelsToStyle)
{
l.ForeColor = LabelColor;
l.Font = LabelFont;
l.Dock = DockStyle.Fill;
}
}
}
It works but it doesn't display correctly in designer (I have to run application to see my design). And maybe it exists a simplest way ?
As per my comment the easiest is to create a custom control and use that one in your windows.
here how simple it is. Simply override the label and set in constructor the default value you want
public class DesignLabel : Label
{
public DesignLabel()
{
ForeColor = Color.Indigo;
Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
then compile once and go to your design view, open your toolbox and you will see the "DesignLabel", simply drag drop on the window and that's it. If you change default in the class it will be changed all over the place.
If you want to change the style already at design time so that you can see in in the Form.cs[design] you need to edit the Form.Designer.cs file! But this you have to do label for label by hand. I saved in this example the Font and Color in the project properties (sorry for the german version):
in my example I have 3 Labels. In the Form.Designer.cs file you can add the properties:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 15);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Font = Properties.Settings.Default.LabelFont;
this.label1.ForeColor = Properties.Settings.Default.LabelColor;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
this.label2.Font = Properties.Settings.Default.LabelFont;
this.label2.ForeColor = Properties.Settings.Default.LabelColor;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 122);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";
this.label3.Font = Properties.Settings.Default.LabelFont;
this.label3.ForeColor = Properties.Settings.Default.LabelColor;
The result looks like this:
DISCLAIMER
I would not recommend this approach! Editing the Form.Desginer.cs file is never a good idea! I would stick to the run time changes. If you want to change all Labels just filter the this.Controls for it and foreach through the collection like this:
this.Controls.OfType<Label>().ToList().ForEach(lbl =>
{
lbl.Font = LabelFont;
lbl.ForeColor = LabelColor;
//lbl.Dock = DockStyle.Fill; // uncommented, because I could see only 1 Label
});
The result will be the same.
i have a button in my form and for every button click it is adding groupBox. but i want a winform contain only 8 groupboxes. when the numbers of groupBox reach 8 it will automatically do Visible=false all 8 before groupBox and again adds a groupBox named(groupBox9). what must i do?
private void butonYeni_Click(object sender, EventArgs e)
{
//creating Font
Font font = new Font("Microsoft Sans Serif", 10.0f, FontStyle.Bold);
Font font2 = new Font("Microsoft Sans Serif", 9.0f, FontStyle.Bold);
int sayGB = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(GroupBox))
{
sayGB++;
}
for (int i = sayGB; i < 1000; i++)
{
//creating groupbox
GroupBox Group = new GroupBox();
Group.Width = 767;
Group.Height = 179;
Group.Text = "Soru & Cevap";
Group.Font = font;
Group.ForeColor = Color.Maroon;
Group.Location = new Point(200,66);
//creating label
Label Soru = new Label();
Soru.Text = "Soru: ";
Soru.Font = font2;
Soru.ForeColor = Color.Maroon;
Soru.Location = new Point(6,33);
Soru.Width = 53;
Soru.Height = 13;
//creating textbox
TextBox soruText = new TextBox();
soruText.Width = 685;
soruText.Height = 20;
soruText.Font = font2;
soruText.ForeColor = Color.Black;
soruText.Multiline = true;
soruText.Location = new Point(70,31);
//creating label
Label Cevap = new Label();
Cevap.Text = "Cevap:";
Cevap.Font = font2;
Cevap.ForeColor = Color.Maroon;
Cevap.Location = new Point(6, 92);
Cevap.Width = 53;
Cevap.Height = 25;
//creating textbox
TextBox cevapText = new TextBox();
cevapText.Width = 685;
cevapText.Height = 69;
cevapText.Font = font2;
cevapText.ForeColor = Color.Black;
cevapText.Multiline = true;
cevapText.Location = new Point(70,67);
//creating button
Button btn = new Button();
btn.Width = 75;
btn.Height = 25;
btn.Text = "Kaydet";
btn.BackColor = Color.Maroon;
btn.Font = font2;
btn.ForeColor = Color.White;
btn.Location = new Point(682,148);
//kontrolleri ekleme
Group.Controls.Add(btn);
Group.Controls.Add(Soru);
Group.Controls.Add(soruText);
Group.Controls.Add(Cevap);
Group.Controls.Add(cevapText);
this.Controls.Add(Group);
}
}
}
define an integer class variable and increase it everytime the button is clicked. After increasing it, check if it's greater than 8. If it is, set your stuff to Visible = false.
Mock up:
public class MyClass
{
private int groupboxCounter = 0;
public MyClass()
{
}
private void btn_click(...)
{
// add a new groupbox here
groupboxCounter++;
if (groupboxCounter > 8)
{
//make stuff invisible here
}
}
}
This is my code:
for (int i = 0; i < gBoxes.Length; i++)
{
var LabelID = new Label();
gLabels[i] = LabelID;
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + i;
LabelID.Text = gColumns[i];
LabelID.Location = new System.Drawing.Point(12, StartLoc);
this.Controls.Add(LabelID);
iPanel.Controls.Add(LabelID);
var BoxID = new TextBox();
gBoxes[i] = BoxID;
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + i;
BoxID.Text = gContent[i];
BoxID.Location = new System.Drawing.Point(12, StartLoc);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
iPanel.Controls.Add(BoxID);
StartLoc += 25;
}
Which works fine, however, the labels overlap the boxes. Which would be the best method to place the boxes after the labels, and that the boxes are aligned together.
Result:
Set the Label.AutoSize property to true. (The default value when adding them in the designer is true but the default when adding by code is false.) Also set your TextBox.Location to have a larger x value than your label... you have the starting location of label and textbox at the same x value of 12.
You can also use the AutoSize property to determine how wide the labels are and then place the textboxes accordingly. Add your labels with AutoSize = true. Arrange the text boxes by determining the widest label and resetting the TextBox.Location just to the right of them. Here is an example:
public partial class Form1 : Form
{
public int YPos { get; set; }
List<string> Labels = new List<string>();
List<Label> LabelControls = new List<Label>();
List<TextBox> TextBoxControls = new List<TextBox>();
public Form1()
{
InitializeComponent();
AddRow("medium string", "medium");
AddRow("This is a longer string", "long");
AddRow("l", "little");
Arrange();
}
void AddRow(string label, string value)
{
Labels.Add(label);
var LabelID = new Label();
LabelID.AutoSize = true; // make sure to enable AutoSize
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + Labels.Count;
LabelID.Text = label;
LabelID.Location = new System.Drawing.Point(12, YPos);
this.Controls.Add(LabelID);
panel1.Controls.Add(LabelID);
LabelControls.Add(LabelID);
var BoxID = new TextBox();
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + Labels.Count;
BoxID.Text = value;
BoxID.Location = new System.Drawing.Point(12, YPos);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
panel1.Controls.Add(BoxID);
TextBoxControls.Add(BoxID);
// both controls have the same Y location
// and initially will have the same X location
YPos += 25;
}
void Arrange()
{
// determine the widest label sized by the AutoSize
int maxLabelX = 0;
for (int i = 0; i < Labels.Count; i++)
{
maxLabelX = Math.Max(maxLabelX, LabelControls[i].Location.X + LabelControls[i].Size.Width);
}
// move all the text boxes a little to the right of the widest label
for (int i = 0; i < Labels.Count; i++)
{
TextBoxControls[i].Location = new Point(maxLabelX + 10, TextBoxControls[i].Location.Y);
}
}
}
The above code generates properly placed TextBox controls:
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);
}
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.
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.