Fit Panel Height to Label Height - c#

I have panels and each of them has 1 label. Everything works fine except 1 thing:
I can't fit the Panel Height to the Label Height...
I'm using this code:
Point location = new Point(0, 0);
ColorConverter cc = new ColorConverter();
foreach (var item in temp)
{
Panel pan = new Panel();
pan.AutoSize = false;
pan.Width = this.Width-75;
pan.Location = location;
pan.BackColor = (Color)cc.ConvertFromString("#" + item.Item3);
Label lbl = new Label();
lbl.Font = new Font("Arial", 12);
lbl.ForeColor = Color.White;
lbl.Text = item.Item2;
lbl.AutoSize = true;
lbl.MaximumSize = new Size(pan.Width - 5, 0);
lbl.Width = pan.Width - 10;
lbl.Location = new Point(lbl.Location.X + 5, lbl.Location.Y + 5);
//pan.Height = lbl.Height + 5;
pan.Controls.Add(lbl);
flowLayoutPanel1.Controls.Add(pan);
location = new Point(location.X - pan.Height, location.Y);
}
I tried doing this:
pan.Height = lbl.Height + 5;
But it the panel is then way too small...

It seems to me, that you are using a panel in order to get a margin around the label within the FlowLayoutPanel. If this is the case, set the Label's margin instead and don't use a Panel:
lbl.Margin = new Padding(5, 5, 80, 5);
or
lbl.Margin = new Padding(5); // If all margins are equal
the constructors are declared like this
public Padding(int left, int top, int right, int bottom)
public Padding(int all)

You could try docking the label in the panel, set the panel AutoSize to true and set AutoSizeMode to GrowAndShrink. Then you can set the panel padding to 5. That way you won't have to worry about the label size or location
foreach (var item in temp)
{
Panel pan = new Panel();
pan.Padding = new Padding(5);
pan.AutoSize = true;
pan.AutoSizeMode = AutoSizeMode.GrowAndShrink;
pan.BackColor = (Color)cc.ConvertFromString("#" + item.Item3);
Label lbl = new Label();
lbl.Dock = DockStyle.Fill;
lbl.Font = new Font("Arial", 12);
lbl.ForeColor = Color.White;
lbl.Text = item.Item2;
lbl.AutoSize = true;
lbl.MaximumSize = new Size(pan.Width - 5, 0);
pan.Controls.Add(lbl);
flowLayoutPanel1.Controls.Add(pan);
location = new Point(location.X - pan.Height, location.Y);
}
Edit : forgot the padding.

Related

Layer object c#

I create a panel and fill it on all the surface of the form and i create a label too for bring it to front of the panel but the only thing it's showing is the panel, i see the hitbox of the label that's all.
private void drawPanels()
{
// I setup my label
Label username = new Label();
Label usernameText = new Label();
var usernameSize = new Size(84, 21);
var usernamePos = new Point(11, 4);
int usernameOffset = 60;
// I setup my panel here
Panel panel = new Panel();
var panelSize = new Size(490, 50);
var panelPos = new Point(12, 12);
int panelVertOffset = 60;
for (int i = 0; i < 2; i++)
{
// I draw the panel
panel.Size = panelSize;
panel.Location = panelPos;
panel.BackColor = Color.FromArgb(35, 35, 35);
parent.Controls.Add(panel);
parent.Controls.Add(username);
// I draw the label
username.Size = new Size(84, 21);
username.Location = new Point(11, 4);
username.BackColor = Color.FromArgb(35, 35, 35);
username.ForeColor = Color.FromArgb(35, 35, 35);
username.Text = "Username:";
// Im trying to fix the problem with this function but it isn't work
username.BringToFront();
// Duplicate the panel and place it down ( y+60 )
panelPos.Offset(0, panelVertOffset);
usernamePos.Offset(0, usernameOffset);
}
}
That's the result i get

How do I dynamically layer objects ontop of eachother in Windows Forms?

I'm using a FlowLayoutPanel to dynamically display an array of PictureBoxes and Labels. They're going in the order: PictureBox-Label-PictureBox-Label and so on. I need those labels to appear above each picture so they'll match, basically layring them ontop of picturebox margins. I tried using Controls.SetChildIndex(temp, 2); but it seem to just swap the postion of the picturebox. I also tried using temp.BringToFront(); but then all the pictureboxes being displayed at the top of the panel and all the labels are below (I need each label to match each picturebox above them). Here's the code:
public void RunMeta()
{
Label mostPickedLabel = new Label();
mostPickedLabel.Text = "Most picked heroes";
flowLayoutPanel1.Controls.Add(mostPickedLabel);
mostPickedLabel.Margin = new Padding(15, 0, 1000, 0);
mostPickedLabel.Font = new Font("Lucida Sans Unicode", 15);
mostPickedLabel.ForeColor = Color.DarkCyan;
mostPickedLabel.Size = new Size(200, 30);
foreach (var mostPickedHero in FetchDataFromDota2Site.MostUsedHeroesAndImages)
{
PictureBox temp = new PictureBox();
temp.ImageLocation = mostPickedHero.ImageSource;
temp.SizeMode = PictureBoxSizeMode.StretchImage;
temp.Left = temp.Width * flowLayoutPanel1.Controls.Count;
temp.Margin = new Padding(15, 30, 15, 30);
flowLayoutPanel1.Controls.Add(temp);
flowLayoutPanel1.AutoScroll = true;
Label heroName = new Label();
heroName.Text = mostPickedHero.MostPickedHeroName;
heroName.Font = new Font("Lucida Sans Unicode", 8);
heroName.ForeColor = Color.White;
flowLayoutPanel1.Controls.Add(heroName);
}
}

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

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

Strange location of panels

I have a program which is some kind of tests. In this test, I have function AddQuestion that adds one panel with question. To place these panels one by one, I have a variable loc that saves location of next panel. First two Panel's with questions adds correct, but next ones are located wrong(far away at the bottom). What can it be?
public void AddQuestion(int number, Question quest)
{
Panel p = new Panel();
p.Name = "panel" + (number);
p.Size = new Size(550, 400);
p.Location = new Point(40, loc);
p.BackColor = System.Drawing.Color.Bisque;
p.AutoScroll = true;
Panel pict_block = new Panel();
pict_block.Size = new Size(480, 200);
pict_block.Location = new Point(10, 10);
PictureBox pict = new PictureBox();
pict.Image = quest.image;
pict.Size = new Size(240, 180);
pict.SizeMode = PictureBoxSizeMode.StretchImage;
pict.Location = new Point(130, 1);
pict_block.Controls.Add(pict);
p.Controls.Add(pict_block);
Label number_text = new Label(); //номер питання
number_text.Text = "Питання № " + number ;
number_text.Font = new Font("Aria", 8, FontStyle.Bold);
number_text.AutoSize = false;
number_text.Location = new Point(400, 210);
p.Controls.Add(number_text);
Label q_text = new Label(); // текст питання
q_text.Text = quest.question_text;
q_text.Font = new Font("Aria", 9, FontStyle.Bold);
q_text.AutoSize = false;
q_text.Size = new Size(400, 50);
q_text.Location = new Point(5, 220);
p.Controls.Add(q_text);
int iter = q_text.Location.Y + 60;
if (CheckIfMuliple(number))
{
foreach (string key in quest.answers.Keys)
{
CheckBox rb = new CheckBox();
rb.Text = key;
rb.AutoSize = true;
rb.Size = new Size(300, 25);
rb.Location = new Point(q_text.Location.X + 15, iter);
iter += 30;
p.Controls.Add(rb);
}
}
else
{
foreach (string key in quest.answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(300, 25);
rb.AutoSize = true;
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
p.Controls.Add(rb);
}
}
questions_panel.Controls.Add(p);
loc += 450;
}
Good location:
Bad Location:
Also I noticed that when I add some panels, then scrool at the middle of the form and add new question, it's not located at the bottom, but somewhere at the center. From next screenshot, 6 question and then 15 question:
p.Location = new Point(40, loc);
This will not work correctly when the outer panel is scrolled. You have to offset it by the that panel's scroll position. Fix:
p.Location = new Point(40 + questions_panel.AutoScrollPosition.X,
loc + questions_panel.AutoScrollPosition.Y);
loc += 450;
This will not work correctly when your program runs on a machine with a video adapter that runs with a different dots-per-inch setting. Pretty common these days, modern versions of Windows make it very easy to change. The panel will automatically be rescaled to match the DPI setting. Fix:
loc += p.Height + 50;
Getting this right manually is very difficult.
I suggest that you change questions_panel to a TableLayoutPanel. It takes care of positioning new controls automatically for you.
TableLayoutPanel
Represents a panel that dynamically lays out its contents in a grid
composed of rows and columns.
I was able to duplicate your problem, and was able to fix it by always offsetting the top of the new Panel based on the location of the last panel. I changed your code:
questions_panel.Controls.Add(p);
loc += 450;
to:
if (questions_panel.Controls.Count > 0)
{
//Location of Top of last panel added then offset vertically by 450
p.Location = new Point(p.Location.X, questions_panel.Controls[questions_panel.Controls.Count-1].Location.Y +450);
questions_panel.Controls.Add(p);
}
else
questions_panel.Controls.Add(p);

Categories