blinking panels with pictures by adding a few more - c#

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.

Related

LiveCharts: Show chart title in windows form [duplicate]

I'm using LiveCharts in WinForms. Reason why I'm not using WPF is because I don't want to rewrite the GUI in WPF, so I'm trying to see if I can make LiveCharts work in WinForms.
I'm saving the LiveCharts control as an image to a PDF, so the title needs to be on the chart itself.
I cannot find any functionality for adding a title on the chart. What I have tried is the following:
VisualElement title = new VisualElement();
title.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
title.VerticalAlignment = System.Windows.VerticalAlignment.Top;
title.X = 0.5;
title.Y = maxYVal;
TextBlock titleText = new TextBlock();
titleText.Text = chartName;
var newTitleFont = HelperFunctions.NewTypeFaceFromFont(titleFont);
titleText.FontFamily = newTitleFont.FontFamily;
titleText.FontStyle = newTitleFont.Style;
titleText.FontSize = titleFont.Size;
title.UIElement = titleText;
cartChart.VisualElements.Add(title);
The above code only adds a label on the chart itself (within the y axis range). The title needs to be independent (above the y axis). Any idea?
This seems to do the trick:
public static TableLayoutPanel AddTitleToChart(Control chart,string title, System.Drawing.Font titleFont)
{
Label label = new Label();
label.AutoSize = true;
label.Dock = System.Windows.Forms.DockStyle.Fill;
label.Font = titleFont;
label.Location = new System.Drawing.Point(3, 0);
label.Name = "label1";
label.Size = new System.Drawing.Size(1063, 55);
label.TabIndex = 0;
label.Text = title;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label.BackColor = chart.BackColor;
chart.Dock = System.Windows.Forms.DockStyle.Fill;
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.AutoSize = true;
tableLayoutPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
tableLayoutPanel.BackColor = System.Drawing.Color.White;
tableLayoutPanel.ColumnCount = 1;
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1069F));
tableLayoutPanel.Controls.Add(label, 0, 0);
tableLayoutPanel.Controls.Add(chart, 0, 1);
tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
tableLayoutPanel.Name = "tableLayoutPanel1";
tableLayoutPanel.RowCount = 2;
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.Size = new System.Drawing.Size(1069, 662);
tableLayoutPanel.TabIndex = 2;
return (tableLayoutPanel);
}

Setting anchor for dynamic created controls

I have winform on which are dynamically created 52 radio buttons.
This is method for creating them:
private void CreateRadioButton()
{
int rbCount = 52;
int numberOfColumns = 23;
radioButtons = new RadioButton[rbCount];
int y = 520;
for (int i = 0; i < rbCount; i++)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = Convert.ToString(i + 1);
if (i % numberOfColumns == 0)
y += 20;
var x = 11 + i % numberOfColumns * 50;
radioButtons[i].Location = new Point(x, y);
radioButtons[i].Size = new Size(40, 15);
//radioButtons[i].Anchor = AnchorStyles.Left;
//radioButtons[i].Anchor = AnchorStyles.Bottom;
radioButtons[i].Font = new Font(radioButtons[i].Font.FontFamily, 8, FontStyle.Bold);
radioButtons[i].UseVisualStyleBackColor = true;
radioButtons[i].Click += new EventHandler(rbtns_click);
xtraTab.Controls.Add(radioButtons[i]);
}
}
There is problem when form is maximized. Radio buttons disappear.
If I set
radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;
The radio buttons are overlayed.
What can I do to keep their position on the same place if form is resized?
these two lines
radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;
mean that Anchor value AnchorStyles.Left will be replaced by AnchorStyles.Bottom
AnchorStyles has Flags attribute set, enum values can be combined:
radioButtons[i].Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
if set via Designer, in "Windows Form Designer generated code" it looks like this:
this.radioButton1.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

WinForms Livecharts Chart Title

I'm using LiveCharts in WinForms. Reason why I'm not using WPF is because I don't want to rewrite the GUI in WPF, so I'm trying to see if I can make LiveCharts work in WinForms.
I'm saving the LiveCharts control as an image to a PDF, so the title needs to be on the chart itself.
I cannot find any functionality for adding a title on the chart. What I have tried is the following:
VisualElement title = new VisualElement();
title.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
title.VerticalAlignment = System.Windows.VerticalAlignment.Top;
title.X = 0.5;
title.Y = maxYVal;
TextBlock titleText = new TextBlock();
titleText.Text = chartName;
var newTitleFont = HelperFunctions.NewTypeFaceFromFont(titleFont);
titleText.FontFamily = newTitleFont.FontFamily;
titleText.FontStyle = newTitleFont.Style;
titleText.FontSize = titleFont.Size;
title.UIElement = titleText;
cartChart.VisualElements.Add(title);
The above code only adds a label on the chart itself (within the y axis range). The title needs to be independent (above the y axis). Any idea?
This seems to do the trick:
public static TableLayoutPanel AddTitleToChart(Control chart,string title, System.Drawing.Font titleFont)
{
Label label = new Label();
label.AutoSize = true;
label.Dock = System.Windows.Forms.DockStyle.Fill;
label.Font = titleFont;
label.Location = new System.Drawing.Point(3, 0);
label.Name = "label1";
label.Size = new System.Drawing.Size(1063, 55);
label.TabIndex = 0;
label.Text = title;
label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label.BackColor = chart.BackColor;
chart.Dock = System.Windows.Forms.DockStyle.Fill;
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.AutoSize = true;
tableLayoutPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
tableLayoutPanel.BackColor = System.Drawing.Color.White;
tableLayoutPanel.ColumnCount = 1;
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1069F));
tableLayoutPanel.Controls.Add(label, 0, 0);
tableLayoutPanel.Controls.Add(chart, 0, 1);
tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
tableLayoutPanel.Name = "tableLayoutPanel1";
tableLayoutPanel.RowCount = 2;
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle());
tableLayoutPanel.Size = new System.Drawing.Size(1069, 662);
tableLayoutPanel.TabIndex = 2;
return (tableLayoutPanel);
}

Dynamically added winforms control Not displaying?

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());

How to make at panel couple pictures to one

I need to make 4 different images in one, and it will be in the panel. Panel size will vary from 180 to 320. I tried to do one main panel, and in her place 4, which are fixed by anchors...
What I have (source four pics)
What i need to get. Panel like this
What I got
private void Form1_Load(object sender, EventArgs e)
{
Panel main_panel = new Panel();
main_panel.BackColor = Color.Azure;
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_l);
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;
main_panel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
panel_top_left.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
panel_top_right.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left;
panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
panel_bottom_right.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
main_panel.Controls.Add(panel_top_left);
main_panel.Controls.Add(panel_top_right);
main_panel.Controls.Add(panel_bottom_left);
main_panel.Controls.Add(panel_bottom_right);
panel1.Controls.Add(main_panel);
}
Well... I will answer on my own post :))
private void Form1_Load(object sender, EventArgs e)
{
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_l);
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 = btm_msg_panel_top_left.Width;
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 - panel_top_right.Width, 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 - panel_bottom_right.Width, panel_top_left.Height - panel_bottom_right.Height);
panel1.Controls.Add(panel_bottom_right);
panel1.Controls.Add(panel_top_right);
panel1.Controls.Add(panel_bottom_left);
panel1.Controls.Add(panel_top_left);
}
This is result
See if this helps;
private void Form1_Load(object sender, EventArgs e)
{
Panel main_panel = new Panel();
main_panel.BackColor = Color.Azure;
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_l);
panel_top_left.BackgroundImage = btm_msg_panel_top_left;
Bitmap btm_msg_panel_top_right = new Bitmap(Properties.Resources.blue_t_l);
panel_top_right.BackgroundImage = btm_msg_panel_top_right;
Bitmap btm_msg_panel_bottom_left = new Bitmap(Properties.Resources.blue_t_l);
panel_bottom_left.BackgroundImage = btm_msg_panel_bottom_left;
Bitmap btm_msg_panel_bottom_right = new Bitmap(Properties.Resources.blue_t_l);
panel_bottom_right.BackgroundImage = btm_msg_panel_bottom_right;
main_panel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
panel_top_left.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
panel_top_right.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
panel_bottom_right.Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left;
main_panel.Controls.Add(panel_top_left);
main_panel.Controls.Add(panel_top_right);
main_panel.Controls.Add(panel_bottom_left);
main_panel.Controls.Add(panel_bottom_right);
panel1.Controls.Add(main_panel);
}

Categories