TableLayoutPanel Height Property not working - c#

TableLayoutPanel t = new TableLayoutPanel();
t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lbl = new Label();
lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
lbl.Text = "Hello";
t.Controls.Add(lbl, 0, 0);
this.Text = t.Size.Height.ToString();
this.Controls.Add(t);
Why the t.Size.Height property gives me 100 always ?

The reason this always was returning 100 is that you need:
AutoSize = true
AutoSizeMode =AutoSizeMode.GrowAndShrink
t.RowCount >= 1
t.ColumnCount >= 1
TableLayoutPanel t = new TableLayoutPanel();
t.AutoSize = true; //added
t.AutoSizeMode =AutoSizeMode.GrowAndShrink; //added
t.RowCount = 1; //added
t.ColumnCount = 1; //added
t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lbl = new Label();
lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
lbl.Text = "Hello";
t.Controls.Add(lbl, 0, 0);
this.Controls.Add(t);
this.Text = t.Size.Height.ToString(); //moved
You also need to move your Height check to after you have added the Table to the form, else no layout operations will have happened.

Related

Label controls added in code to a Panel are not displayed

Why not lable_2 and lable_3 are not displayed?
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
The Y-position of the 2nd and 3rd labels is outside the bounds of the panel. Instead of giving the location, you can use the Dock property of the Label.
Label lbl = new Label();
lbl.Text = "Label test";
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Dock = DockStyle.Fill;
panel1.Controls.Add(lbl);

How can i add Events to hard coded forms in c#?

I've tried to create a form that will create buttons based on the number of items in database
my int count is equal to the number of count in my database
I'd like to add events or functions to the created label, that when i click the label it will display in the MesssageBox the name of the label
below is my code,
int Count;
List<Panel> pnl = new List<Panel>();
List<Label> tasklevel = new List<Label>();
List<Label> taskStatus = new List<Label>();
List<Label> taskname = new List<Label>();
List<PictureBox> image = new List<PictureBox>();
for (int i = 0; i < Count; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
pan.BackColor = Color.White;
pan.Dock = DockStyle.Top;
pan.Padding = new Padding(10, 0, 10, 10);
pan.Height = 80;
pnl.Add(pan);
Label lbl = new Label()
{
Name = "lbl" + i,
ForeColor = Color.Black,
//Dock = DockStyle.Left,
AutoSize = false,
Width = 0,
BackColor = Color.Silver,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(10, 10, 10, 10),
Text = "Designing",
}; tasklevel.Add(lbl);
Label lblname = new Label()
{
Name = "lblname" + i,
ForeColor = Color.Black,
Dock = DockStyle.Left,
AutoSize = false,
Width = 0,
BackColor = Color.Gray,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(10, 10, 10, 10),
Text = "Designing",
}; taskStatus.Add(lblname);
Label tskname = new Label()
{
Name = "tskName" + i,
ForeColor = Color.FromArgb(31, 31, 31),
Font = new Font("Segoe UI", 11, FontStyle.Regular),
Dock = DockStyle.Left,
AutoSize = false,
Width = 200,
BackColor = Color.Gainsboro,
TextAlign = ContentAlignment.MiddleCenter,
Padding = new Padding(10, 10, 10, 10),
Text = "Task Name",
}; taskname.Add(tskname);
PictureBox picBox = new PictureBox()
{
Name = "picBox" + i,
Dock = DockStyle.Fill,
AutoSize = false,
Image = DreametryMIS.Properties.Resources.f1,
SizeMode = PictureBoxSizeMode.StretchImage,
BackColor = Color.FromArgb(240,240,240),
Padding = new Padding(10, 10, 10, 10),
}; image.Add(picBox);
pan.Controls.Add(picBox);
pan.Controls.Add(lblname);
pan.Controls.Add(lbl);
pan.Controls.Add(tskname);
FlowPanel.Controls.Add(pan);
}
I'm new at c#, I hope some can help me
Implement an event handler like this:
public void label_Clicked(object sender, EventArgs e)
{
Label label = sender as Label;
if (label == null) return;
MessageBox.Show("Name: " + label.Name + " Text: " + label.Text);
}
You can now add this handler to the Click event of your labels, for example:
lblname.Click += label_Clicked;
tskname.Click += label_Clicked;
When one of the labels is clicked now, the method label_Clicked gets called with the label as argument for sender. So by casting the sender to Label you can easily access the clicked label's properties.
Note that you cannot add the handler inside the label's object initializer as you did with the other properties, but need to do it in an extra statement like I showed.
You can too:
lblname.Click += delegate { nameOfUrFunction (lblname) };
private void nameOfUrFunction (Label lbl)</code>
{
}
TableCell cel2 = new TableCell();
Label lbl2 = new Label();
lbl2.Text = s;
cel2.Controls.Add(lbl2);
tr.Cells.Add(cel2);
TableCell cel3 = new TableCell();
DropDownList ddlcountry = new DropDownList();
ddlcountry.ID = s;
cel3.Controls.Add(ddlcountry);
tr.Cells.Add(cel3);
table.Rows.Add(tr);
form1.Controls.Add(table);
DropDownList ddl1 = (DropDownList)form1.FindControl("CountryId");
DropDownList ddlstate = (DropDownList)form1.FindControl("StateId");
ddl1.AutoPostBack = true;
ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
}

What's happening to my label?

Ok so I have this code to create controls on my form:
public CableID_DuplicateView(CableID_CreateView CView)
{
InitializeComponent();
Label lbl = new Label();
Button btn = new Button();
ComboBox cmb = new ComboBox();
TextBox txt = new TextBox();
if (CView.input == 1)
{
lbl.Text = "Please select a cable number to duplicate:";
lbl.Location = new Point(12, 9);
cmb.Location = new Point((lbl.Width + 17), 6);
cmb.Size = new System.Drawing.Size(125, 20);
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
this.Height = cmb.Height + 48;
this.Width = lbl.Width + cmb.Width + btn.Width + 34;
this.Controls.Add(lbl);
this.Controls.Add(cmb);
this.Controls.Add(btn);
}
}
Which produces this:
What is causing my label to get cut off like that? And why is the comboBox sitting in a strange location?
What is happening here is that label.width is 100 which is not covering the whole text do this instead:
lbl.Text = "Please select a cable number to duplicate:";
lbl.Location = new Point(12, 9);
lbl.Width = 200;
You can also do this:
lbl.AutoSize = true;
Try this to set AutoSize width Property:
lbl.Text = "Please select a cable number to duplicate:";
lbl.Location = new Point(12, 9);
lbl.AutoSize = true;
this.Controls.Add(lbl);
Width property will not be set until the control has been added to the parent container.
Problem : You didn't set Width for your Label Control.so it takes some default width for your Label
Solution: You need to set Width for your Label
Try This:
lbl.Size = new System.Drawing.Size(200, 20); //width = 200, height = 20
As others said the width for the label is too short.
Solution 1: Set the width to a larger fixed width:
lbl.Width = 200;
Solution 2: Set AutoSize to true:
lbl.AutoSize = true;
Solution 3: Combine a fixed width with the AutoEllipsis property:
lbl.Width = 100;
lbl.AutoEllipsis = true;
When using AutoEllipsis the label remains the set width,
but the shown text is followed by three dots to indicate that not all text is shown.

StackPanel margin issue

When I set a Margin from the Top to 15 for the TextBox:
x.Margin = new Thickness(100, 15, 0, 0);
This works fine and everything it ok, but then I want to make a ComboBox also appear 15px from the top - it doesn't work.
y.Margin = new Thickness(0, 15, 0, 0);
This is the code for the button I click to create the ComboBox and the TextBox:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
TextBox x = new TextBox();
x.Name = "Title" + t;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(100, 15, 0, 0);
spStandard.Children.Add(x);
ComboBox y = new ComboBox();
y.Name = "Combo" + t;
y.Text = (t + 1).ToString();
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(0, 15, 0, 0);
spStandard.Children.Add(y);
t++;
}
Here is a picture of what happens when I run the application - it shows where the ComboBox gets put:
Here is a solution for your problem, you shouldn't create controls in code behind but it will get the job done:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
//a new stackpanel is used to arrange items Horizontally for each line
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
TextBox x = new TextBox();
x.Name = "Title" + t;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.Margin = new Thickness(0, 15, 0, 0);
ComboBox y = new ComboBox();
y.Name = "Combo" + t;
y.Text = (t + 1).ToString();
y.Height = 25;
y.Width = 45;
y.Margin = new Thickness(0, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
spStandard.Children.Add(sp);
t++;
}

Cannot access dynamic controls

I have some trouble when i'm trying to click my dynamic controls in StackPanel.
I'm adding controls to Grid in this way...
void Opt()
{
TextBlock Title_1 = new TextBlock();
TextBlock Title_2 = new TextBlock();
CheckBox Kwota_exists = new CheckBox();
TextBox Title = new TextBox();
StackPanel Frame = new StackPanel();
Button OK = new Button();
Title_1.Text = "Dodaj kategorię";
Title_2.Text = "Aktywne kategorię";
Kwota_exists.Content = "Stała kwota?";
Title.Text = "Nazwa kategorii";
OK.Content = "Dodaj";
OK.IsEnabled = true;
OK.IsHitTestVisible = true;
OK.IsTabStop = true;
OK.ClickMode = ClickMode.Release;
Frame.IsHitTestVisible = true;
Kwota_exists.Checked +=Kwota_exists_Checked;
Title_1.FontSize = 50;
Title_2.FontSize = 50;
Title.FontSize = 20;
Frame.Height = 100;
Frame.Width = 400;
Title_1.Margin = new Thickness(0, 0, 0, 0);
Title_2.Margin = new Thickness(0, 220, 0, 0);
Frame.Margin = new Thickness(0, 70, 0, 0);
Frame.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
Frame.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
Frame.Orientation = Orientation.Horizontal;
Frame.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(145, 56, 234, 21));
Frame.Children.Add(Kwota_exists);
Frame.Children.Add(Title);
Frame.Children.Add(OK);
GrdContent.Children.Add(Frame);
GrdContent.Children.Add(Title_1);
GrdContent.Children.Add(Title_2);
}
But when i'm trying to click button or check checkbox controls doesn't seem to response (unclickable).
Looks like i can't access them or i'm doing something wrong. I would be gradefull if someone explain me where i'm doing mistake.

Categories

Resources