Scale constrols inside TableLayoutPanel - c#

Why tableLayoutPanel.Dock = DockStyle.Fill; do not work and tableLayoutPanel does not fill all available space in Form?
How to scale button.Text = "Button";?
namespace Scalability
{
static class Program
{
/// <summary>
/// Главная точка входа для приложения.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ViewForm viewForm = new ViewForm();
Application.Run(viewForm);
}
}
}
namespace Scalability.Forms
{
class ViewForm:Form
{
public ViewForm()
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
Button button = new Button();
button.Text = "Button";
button.Dock = DockStyle.Fill;
Label label = new Label();
label.Text="Label";
label.Dock = DockStyle.Fill;
TextBox textBox = new TextBox();
textBox.Text = "textBox";
textBox.Dock = DockStyle.Fill;
tableLayoutPanel.Controls.Add(button, 0, 0);
tableLayoutPanel.Controls.Add(label, 0, 1);
tableLayoutPanel.Controls.Add(textBox, 1, 0);
tableLayoutPanel.Dock = DockStyle.Fill;
this.Controls.Add(tableLayoutPanel);
}
}
}

configurate RowStyles of TableLayoutPanel to make them stretch
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50));

But it does work. :)
1) Try to attach colors to your controls like this.
Button button = new Button();
button.Text = "Button";
button.BackColor = Color.Orange;
button.Dock = DockStyle.Fill;
Label label = new Label();
label.Text = "Label";
label.BackColor = Color.Yellow;
label.Dock = DockStyle.Fill;
TextBox textBox = new TextBox();
textBox.Text = "textBox";
textBox.BackColor = Color.Green;
textBox.Dock = DockStyle.Fill;
Now you will see that the controls use your entire window exactly in the way you put them. So button is col 0, row 0 (left, top). Textbox is col 1, row 0 (right, top). Label is col 0, row 1 (left bottom). And there is nothing in col 1 row 1 (right bottom "central place" is empty).
If you add a button to 1,1 it will stretch in the remaining space. Like this
// Blue button.
Button bbutton = new Button();
bbutton.Text = "Button";
bbutton.BackColor = Color.Blue;
bbutton.Dock = DockStyle.Fill;
tableLayoutPanel.Controls.Add(button, 0, 0);
tableLayoutPanel.Controls.Add(label, 0, 1);
tableLayoutPanel.Controls.Add(textBox, 1, 0);
// We added this one.
tableLayoutPanel.Controls.Add(bbutton, 1, 1);
tableLayoutPanel.Dock = DockStyle.Fill;
So there you have it.

Related

Fit Panel Height to Label Height

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.

TableLayoutPanel has incorrect row height

I have auto-sized 2x2 table layout and long auto-sized labels in each cell.
This layout is in other table layout with no-auto-sized cells.
Minimal project to reproduce the problem:
using System;
using System.Windows.Forms;
namespace TestForms {
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
class TestForm : Form {
public TestForm() {
var childPanel = new TableLayoutPanel();
var label8 = new Label();
var label9 = new Label();
var label10 = new Label();
var label7 = new Label();
var rootPanel = new TableLayoutPanel();
childPanel.AutoSize = true;
childPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
childPanel.BackColor = System.Drawing.Color.Silver;
childPanel.ColumnCount = 2;
childPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
childPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
childPanel.Controls.Add(label8, 1, 0);
childPanel.Controls.Add(label9, 0, 1);
childPanel.Controls.Add(label10, 1, 1);
childPanel.Controls.Add(label7, 0, 0);
childPanel.Dock = DockStyle.Top;
childPanel.RowCount = 2;
childPanel.RowStyles.Add(new RowStyle());
childPanel.RowStyles.Add(new RowStyle());
label8.AutoSize = true;
label8.Text = "2ggggggggggggggggg";
label9.AutoSize = true;
label9.Text = "label9";
label10.AutoSize = true;
label10.Text = "label10";
label7.AutoSize = true;
label7.Text = "label7";
rootPanel.ColumnCount = 1;
rootPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
rootPanel.Controls.Add(childPanel, 0, 0);
rootPanel.Dock = DockStyle.Fill;
rootPanel.RowCount = 1;
rootPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
ClientSize = new System.Drawing.Size(205, 197);
Controls.Add(rootPanel);
}
}
}
I get the following result:
Why last row got the wrong height? Is there a workaround?
Interestingly, seems like you have hit some bug since it doesn't happen if some of the labels AutoSize is false, or making the first column style SizeType.AutoSize etc.
You can use the following workaround. It's ugly, but that's the only way I've found. The idea is to add invisible row containing fake Label with AutoSize set to false. For instance, after setting up the normal layout, add the following lines:
childPanel.RowCount++;
childPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
childPanel.Controls.Add(new Label { AutoSize = false }, 0, childPanel.RowCount - 1);

change the Label text in runtime inside a panel

I am creating List of Panel. Each panel Contain a Label and Button . I have also a Button(button1). I want to change the label text of (panels[0]) when click button1. How can I do this.This is my c# code:
List<Panel> panels = new List<Panel>();
private void Panel()
{
var x = 0;
for (int i = 1; i < 5; i++)
{
x += 60;
var panel1 = new Panel() { Size = new Size(60, 140), Location = new Point(x, 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Name = "pan" + i;
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = i.ToString();
lbl.Location = new Point(10, 20);
panel1.Controls.Add(lbl);
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
panel1.Controls.Add(button);
panels.Add(panel1);
Controls.Add(panel1);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var p in panels)
{
}
}
Output:
But i want, When i Click button1 it will change Label text of zero index panels(I have pointed it using red mark).
Can anyone help me...
Ok, so you've got a button and a label within a panel. When you click a button of a panel, you wanna do something to the label in the same panel, right ?
So
private void BtnClick(object sender, EventArgs e) {
var button = (Button)sender;//you've got the button clicked
var panel = button.Parent;//you've got the panel.
//var label = panel.Controls.OfType<Label>().FirstOrDefault();//but don't think you get this in c# 3.0
var label = GetFirstLabel(panel);
if (label != null)
label.Text = "something";
}
private Label GetFirstLabel(Control parent) {
foreach (var control in parent.Controls) {
if (control is Label) return control as Label;
}
return null;
}
Usage
When you add your buttons, you can now do
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
button.Click += BtnClick;
And this will work on all panels.
If you don't store reference to that Label then you can find it in controls of the first Panel by type for example:
panels[0].Controls.OfType<Label>().First().Text = "New Text";
or by name
panels[0].Controls.OfType<Label>().Single(l => l.Name == "lbl1").Text = "New Text";
you can do this with a simple method:
private void button1_Click(object sender, EventArgs e)
{
Label l = panels[0].Controls.Find("lbl1", false).FirstOrDefault() as Label;
l.Text = "TEXT";
}

How to take away vertical space for programmatically added menu?

I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. After I add the menu to my form and add more controls they overlap each other. So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu.
How do I do that ?
If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip.
Edit: this doesn't work:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = 50;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 25, 128, 50);
Controls.Add(b);
While this works like I would like it to do with MenuStrip:
Menu = new MainMenu();
Menu.MenuItems.Add("File");
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
When you SetBounds(0, 25, 128, 50), you are actually setting b.Top to 25 (the second parameter). In order to set the top bound relative to the menu control, use:
b.SetBounds(0, menu.Bottom, 128, 50);
[UPDATE]
Alternatively, you could use:
public partial class Form1 : Form
{
private int menuStripHeight = 50;
public Form1()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
}
private void Form1_Load(object sender, EventArgs e)
{
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
// note that the position used is 0,0
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
}
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
e.Control.Top += menuStripHeight;
}
}
[UPDATE 2]
Or you could just use a Panel:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);
Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. So without resorting to private constants and event handlers:
MenuStrip menu = new MenuStrip() {
AutoSize = false,
Dock = DockStyle.Top
};
menu.Items.Add("File");
Panel p = new Panel(){
Dock = DockStyle.Top
};
Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;
Button b = new Button(){
Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);

Winforms - how to show extra fields of an item on a form

I am trying to create a form for collecting information about an item. There are two types: default item and special item. The fields of item shows on the form by default.
public class Item
{
Id
Name
Quantity
Type //default, special
}
public class SpecialItem : Item //inherits from item
{
//extra fields here
ExpiryDate
SafeForChildren
}
How will I take the extra fields of SpecialItem to user.
I think the fields of SpecialItem should not show until the user indicates he wants to add special item by selecting the type.
I think of using a tab to show extra fields
A collapsible control - I don't if this exists
Hiding the controls and showing them when necessary
Any other idea
If the two class have a clear relation between them and this relation is well comprensible to your user, I think it's a good thing to show every field.
Put all the fields inside a groupbox and add two option buttons for the Item and SpecialItem class.
By default (at form load) the Item class will be selected and the two extra field are disabled.
If the user choose the SpecialItem class option button, enable the other two fields.
I have seen this behavior in many options dialogs when selecting an option will enable other specific options.
Try this out - it simply reflects the given type and then puts the controls onto a TableLayoutPanel adds a couple of buttons and then binds two event handlers to the click event of the buttons. It is by no means a masterpeice, but I think will get you started.
public MyForm(Type typeToDisplay)
{
InitializeComponent();
PropertyInfo[] settableProperties = typeToDisplay.GetProperties(BindingFlags.Instance | BindingFlags.Public);
TableLayoutPanel panel = new TableLayoutPanel();
panel.ColumnCount = 2;
panel.RowCount = settableProperties.Length+1;
panel.Name = "LayoutPanel";
this.Controls.Add(panel);
int rowIndex = 0;
foreach (PropertyInfo info in settableProperties)
{
Label propLabel = new Label();
propLabel.Text = info.Name;
TextBox propField = new TextBox();
panel.Controls.Add(propLabel, 0, rowIndex);
panel.Controls.Add(propField, 1, rowIndex);
rowIndex++;
}
panel.Controls.Add(new Button() { Text = "OK", Name="OK" }, 0, rowIndex);
panel.Controls.Add(new Button() { Text = "Cancel", Name="Cancel" }, 1, rowIndex);
panel.Controls["Cancel"].Click += new EventHandler(CloseForm);
panel.Controls["OK"].Click += new EventHandler(SaveChanges);
panel.Height = this.Height;
panel.Width = this.Width;
}
private void CloseForm(object sender, EventArgs e)
{
this.Close();
}
private void SaveChanges(object sender, EventArgs e)
{
MessageBox.Show("Save changes was clicked!");
this.Close();
}
Here is a full example solution to demonstrate my suggestion above. Note that it is all done in code and uses only a single column, but can work with designer-produced controls (of course) and multiple columns, too. Just be sure to set all the controls in a row (say, a label and its corresponding input control) to Visible = false to have the unused rows collapse properly.
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
TextBox b1 = new TextBox(); b1.Dock = DockStyle.Fill;
TextBox b2 = new TextBox(); b2.Dock = DockStyle.Fill;
TextBox b3 = new TextBox(); b3.Dock = DockStyle.Fill;
CheckBox special = new CheckBox(); special.Text = "Special?";
TextBox b4 = new TextBox(); b4.Dock = DockStyle.Fill; b4.Visible = false;
TextBox b5 = new TextBox(); b5.Dock = DockStyle.Fill; b5.Visible = false;
Button button = new Button(); button.Text = "Save";
special.CheckedChanged += new EventHandler((sender, args) => { b4.Visible = b5.Visible = special.Checked; });
tlp.Controls.Add(b1, 0, 0);
tlp.Controls.Add(b2, 0, 1);
tlp.Controls.Add(b3, 0, 2);
tlp.Controls.Add(special, 0, 3);
tlp.Controls.Add(b4, 0, 4);
tlp.Controls.Add(b5, 0, 5);
tlp.Controls.Add(button, 0, 6);
Controls.Add(tlp);
tlp.Dock = DockStyle.Fill;
tlp.BringToFront();

Categories