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, ....};
Related
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);
i'm making a simple login-register program, i save usernames and passwords in lists.
i'm trying to show all the usernames under each other and that it wil repeat everytime a new user will be registered.
for some reason it only shows the last user, and that's it.
for (Int32 i = 0; i < frmLogin.reg_usernames.Count; i++ )
{
TextBox lbl = new TextBox { Location = new Point(15, 30), BorderStyle = BorderStyle.Fixed3D, BackColor = Color.AliceBlue, Font = new Font(Font.FontFamily.Name, 9), ScrollBars = ScrollBars.Vertical };
this.Controls.Add(lbl);
lbl.Text = frmLogin.reg_usernames[i];
}
You need to move the boxes down as you go:
TextBox lbl = new TextBox { Location = new Point(15, 30 * i), BorderStyle ....
Note that your form would need to be large enough to see them all, as well. You may need to set your height appropriately, or place the text boxes inside of a container which could scroll, instead of directly on the form itself.
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);
I need to know which width the label will have with AutoSize = true; before the form is shown so I can position other controls relatively to the label. Actually I have no access to the form only to the parent Control.
(Working completely without Designer. That means just code.)
(Measure string in Graphics is unreliable so I can't use that.)
Label label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);
label.Text = "hello world";
myParent.Controls.Add(label);
// more control generation follows, THEN form is shown
Ok, you cannot get the label width before adding it into its parent control. just place the location after Controls.Add
Label label = new Label();
label.AutoSize = true;
label.Text = "hello world";
myParent.Controls.Add(label);
label.Left = cntControl1.Left - label.Width;
label.Top = cntControl1.Top;
I want to create a textbox inside a radiobutton in Windows Phone 7 in order to show multi-line text.
RadioButton rb = new RadioButton();
rb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
rb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
rb.Height = 104;
rb.Width = 396;
TextBlock txt = new TextBlock();
txt.TextWrapping = TextWrapping.Wrap;
txt.Height = 72;
txt.FontSize = 22;
txt.Width = 300;
txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
txt.Text = "Some Text";
rb.Content = txt;
The Problem is that the Text in the Textblock can be either long or short. If it is long , it is fine. But if it is short, it aligns to the top border of the textblock, and it looks ugly. But I can't decrease the height of the textblock, because there will be not enough space in case of 2 or 3 lines of text.
How can I solve this problem?
You can set Height of the textbox to double.NaN in your code like txt.Height = double.NaN;.
you can set maxheight
make layout adapts the actual height of textblock by setting right alignment value