Why is autosize function setting the size of width wrong? - c#

I'm programmatically creating labels and a textbox beside it.
To create the textbox beside it I use the following calculation to determine the Location.X for the textbox:
label2.X+label2.Width+5;
Here's the issue; the label gets created and I have autosize turned on for it, then after enter the text it sizes appropriately. However, the textbox does not get created besides it but it overlapping it for some distance.
I debugged my code and the label was returning the width of 100, while after creating a duplicate label manually with autosize on and same string of text the width came to be 149. Why is this problem there and is there a solution to it other than manually doing it every time there's a problem?
My code below:
//Qd
//label
Label label2 = new Label();
label2.Location = new System.Drawing.Point(6, 68);
label2.Name = "label2";
//label2.Size = new System.Drawing.Size(24, 13);
label2.TabIndex = 16;
label2.Text = "Characteristic Strength Qd:";
label2.AutoSize = true;
label2.MouseHover += new EventHandler(BoucWen_Qd_MouseHover);
//textbox
TextBox textBox3 = new TextBox();
textBox3.Location = new System.Drawing.Point(/*90*/149+5+6, 65);
textBox3.Name = "Qd";
textBox3.Size = new System.Drawing.Size(197, 20);
textBox3.TabIndex = 17;
textBox3.Tag = "Characteristic Strength\r\n Link: )_Element";
textBox3.HelpRequested += new HelpEventHandler(Node_label1_HelpRequested);
//create units label
x_unit = textBox3.Location.X + textBox3.Width + 5;
y_unit = textBox3.Location.Y;
labelUnit = new Label();
labelUnit.Location = new System.Drawing.Point(x_unit, y_unit);
labelUnit.AutoSize = true;
labelUnit.Text = forceunit;
Fixidity_panel.Controls.Add(labelUnit);
//adding the above two label&textbox:
Fixidity_panel.Controls.AddRange(new Control[] {
comboBox2,
label11,
textBox11,
label10,
comboBox1,
label9,
textBox9,
label8,
textBox8,
label7,
textBox7,
label6,
textBox6,
label5,
textBox5,
label4,
textBox4,
label3,
textBox3,
label2,
textBox2,
Stiffness_label, });

When AutoSize property is set to true the calculation of the width will be proceeded only after it will be added to the panel. hence, you should first add it to the panel and then add the texbox and set its location .Location = label2.Location.X+label2.Width +5 to get the expected result. here is my suggestion, based on your code:
(please read my comments written in CAPITAL letters)
//label
Label label2 = new Label();
label2.Location = new System.Drawing.Point(6, 68);
label2.Name = "label2";
//label2.Size = new System.Drawing.Size(24, 13);
label2.TabIndex = 16;
label2.Text = "Characteristic Strength Qd:";
label2.AutoSize = true;
// ADD IT TO THE PANEL IN ORDER TO GAIN A WIDTH
Fixidity_panel.Controls.Add(label2);
label2.MouseHover += new EventHandler(BoucWen_Qd_MouseHover);
//textbox
TextBox textBox3 = new TextBox();
// NOW YOU CAN DO: .Location = label2.X+label2.Width+5
textBox3.Location = new System.Drawing.Point(label2.Location.X+label2.Width +5, 65);
textBox3.Name = "Qd";
textBox3.Size = new System.Drawing.Size(197, 20);
textBox3.TabIndex = 17;
textBox3.Tag = "Characteristic Strength\r\n Link: )_Element";
// NOW ADD THE TEXTBOX
Fixidity_panel.Controls.Add(textBox3);

Related

Dynamically created c# label not visible

I'm attempting to display some dynamically generated labels next to dynamically generated text boxes. The text boxes appear but the labels do not.
I've looked at several solutions and have tried to make sure I defined all the label properties. I looked at some threading related solution that seem unnecessary because i'm not changing visibility state, I would just like to pop up the labels next to the text boxes.
TextBox[] channelNames = new TextBox[numOfChannels];
GroupBox channelBox = new GroupBox();
Label[] labelNames = new Label[numOfChannels];
for (int currentChannelIndex = 0; currentChannelIndex < numOfChannels; currentChannelIndex++)
{
var txt = new TextBox();
channelNames[currentChannelIndex] = txt;
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32 + (currentChannelIndex * 28));
txt.Visible = true;
this.channelBox.Controls.Add(channelNames[currentChannelIndex]);
var lbl = new Label();
labelNames[currentChannelIndex] = lbl;
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32 + (currentChannelIndex * 28));
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.channelBox.Controls.Add(labelNames[currentChannelIndex]);
}
Here are a few things to check:
1) You are setting "AutoSize" and "Size". Trying removing "Size"
2) The default font is likely too large for the (55,13) size.
Try explicitly setting the font to something small to see if it shows up.
Does channelCollection[currentChannelIndex].PhysicalName actually contain a non-Empty string? e.g.:
class Something
{
public string PhysicalName { get; set; }
}
private void AddLabels()
{
Something[] channelCollection = new Something[]
{
//Applying this to Label.Text makes it "invisible"
new Something() { PhysicalName = "" }
};
var currentChannelIndex = 0;
var txt = new TextBox();
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32);
txt.Visible = true;
this.Controls.Add(txt);
var lbl = new Label();
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32);
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.Controls.Add(lbl);
}
I actually had an exception that downstream of this code block that was causing the issue. I assumed that i would see the labels since the exception was thrown after the call to the label. Thanks for your suggestions.

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

Weird Label behaviour in C#

I add a label to my form like this
l = new Label();
l.Location = new Point(520, 94);
l.Size = new Size(95, 20);
l.Text = "Pooling interval (s):";
f.Controls.Add(l);
If the text of the label is "Pooling interval (s):" the displayed text on the form will only be "Pooling interval" but if i change the text too "Pooling interval(s):" removing the second space the text gets printed correctly.
Any reason for this to be happening? There is also no overlap of other controls.
Just leave the row with size like that:
l = new Label();
l.Location = new Point(520, 94);
//l.Size = new Size(95, 20);
l.Text = "Pooling interval (s):";
f.Controls.Add(l);
your text is bigger than your label size. so you can use a bigger label, or change AutoSize property to true. try below code.
l = new Label();
l.Location = new Point(520, 94);
l.Size = new Size(95, 20);
l.AutoSize = true; // fixes the problem
l.Text = "Pooling interval (s):";
Controls.Add(l);

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.

how to add label on flowLayoutPanel1 at first index0?

how to add label on flowLayoutPanel1 at first index0?
i would like to put label control in first or secound index how?
that readonly?
Label lb = new Label();
lb.AutoSize = true;
lb.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(187)))), ((int)(((byte)(206)))), ((int)(((byte)(241)))));
lb.Location = new System.Drawing.Point(122, 3);
lb.Margin = new System.Windows.Forms.Padding(0, 3, 3, 0);
lb.Name = "label2";
lb.Size = new System.Drawing.Size(45, 13);
lb.TabIndex = 13;
lb.Text = "XXX";
flowLayoutPanel1.Controls[0] = lb?
Use Controls.Add to add the label to flowLayoutPanel1, and then use SetChildIndex to set the index of the control to whatever you want:
flowLayoutPanel1.Controls.Add(lb); // Adds lb to flowLayoutPanel1
flowLayoutPanel1.Controls.SetChildIndex(lb, 0); // Sets index of lb to 0

Categories

Resources