Weird Label behaviour in C# - 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);

Related

Update text in WinForms Label symmetrically from center

How to update text in WinForms Label control symmetrically from the center if each line here is value update, not the single text, first shows "A", then "New" etc:
A
New
Year
Comes
again
and again
to spread
the spirit and
Celebration have
a wonderful New Year
party and Happy New Year
with joy and peace
Use a StringBuilder with the label's TextAlign property to MiddleCenter and AutoSize to true.
StringBuilder sb = new StringBuilder();
sb.AppendLine("A");
sb.AppendLine("New");
sb.AppendLine("Year");
sb.AppendLine("Comes");
sb.AppendLine("again");
sb.AppendLine("and again");
sb.AppendLine("to spread");
sb.AppendLine("the spirit and");
sb.AppendLine("Celebration have");
sb.AppendLine("a wonderful New Year");
sb.AppendLine("party and Happy New Year");
sb.AppendLine("with joy and peace");
Label l = new Label();
l.AutoSize = true;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Text = sb.ToString();
Controls.Add(l);
A single label having TextAlign = MiddleCenter will do the trick. Just make sure you don't put extra space before or after each line of text:
label1.AutoSize = true;
label1.TextAlign = ContentAlignment.MiddleCenter;
label1.Text =
#"
A
New
Year
Comes
again
and again
to spread
the spirit and
Celebration have
a wonderful New Year
party and Happy New Year
with joy and peace
";
You can set the labels AutoSize to false and let them have the same width and same left:
foreach(var lbl in labels)
{
lbl.SuspendLayout();
lbl.Left = 0;
lbl.Width = 500;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.ResumeLayout();
}
For getting all labels you can
var labels = parent.Controls.OfType<Label>().ToList();
//where parent is the container of the labels
// note that this would select any other label on the container
You can also add them manually:
Label[] labels = new[] {label1, label2, ....};

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.

Why is autosize function setting the size of width wrong?

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

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 center a Textbox inside a Border in C#

In my WP7 app, I am creating a Textbox inside a Border. How to align the Textbox exactly at the center of the Border?
Border rectangleborder = new Border();
rectangleborder.Background = new SolidColorBrush(Colors.Transparent);
rectangleborder.BorderBrush = new SolidColorBrush(Colors.Black);
rectangleborder.BorderThickness = new Thickness(2);
rectangleborder.Width = width;
rectangleborder.Height = width;
TextBox textbox = new TextBox();
textbox.Text = "1";
textbox.Background = new SolidColorBrush(Colors.Transparent);
textbox.Foreground = new SolidColorBrush(Colors.Yellow);
textbox.BorderBrush = new SolidColorBrush(Colors.Transparent);
this.canvas1.Children.Add(rectangleborder);
rectangleborder.SetValue(Canvas.LeftProperty, 30 + (j - 1) * width);
rectangleborder.SetValue(Canvas.TopProperty, 30 + (i - 1) * width);
rectangleborder.Child = textbox;
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
You can also allign the text inside using:-
textBox.TextAlign = HorizontalAlignment.Center;
You need set the HorizontalAlignment to align horizontally and the VerticalAlignment to align vertically:
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
And the result should look something like this:

Categories

Resources