Width of C# Label before it is shown? - c#

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;

Related

Create GroupBox and labels inside it programmatically

I want to create a groupbox programmatically and inside it put labels. Now I created this but with design and I have like this:
I'm trying but I don't know how can I assign labels in correct position and how can I assign it to a specific group box
GroupBox groupBox1 = new GroupBox();
Panel grid1 = new Panel();
Label lbl1 = new Label { Text = "Completed" };
Label lbl2 = new Label { Text = "label" };
Label lbl3 = new Label { Text = "In progress" };
Label lbl4 = new Label { Text = "label" };
//etcetera
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height = 185;
grid1.Width = 160;
How can I achieve that? Regards
Update
As the comments below I try
GroupBox groupBox1 = new GroupBox();
this.Controls.Add(groupBox1);
Panel grid1 = new Panel();
groupBox1.Controls.Add(grid1);
groupBox1.Location = new Point(20, 250);
grid1.Location = new Point(20, 250);
Label lbl1 = new Label { Text = "test" };
Label lbl2 = new Label { Text = "Test2" };
groupBox1.Name = "TESTTT";
groupBox1.Width = 222;
groupBox1.Height = 149;
grid1.Height = 218;
grid1.Width = 145;
grid1.Controls.Add(lbl1);
grid1.Controls.Add(lbl2);
Result:
But my group box it just clear without name and without labels, why it happen?
Controls in WinForms are arranged so that they reside inside one another. So your Form has a Controls collection which is essentially a Collection of type Control. So if you add a GroupBox to the form, then you must add it to the Controls collection of the form. Then if you add a control to your GroupBox then you need to add it to the GroupBox collection of controls.
With that in mind, you can do something like this:
private void AddGroupBoxAndLables()
{
GroupBox groupBox1 = new GroupBox();
groupBox1.SetBounds(50, 50, 300, 200);
this.Controls.Add(groupBox1);
Label lblCompleted = new Label { Name = "lblCompleted", Text = "Completed" };
lblCompleted.Location = new Point(20, 20);
groupBox1.Controls.Add(lblCompleted);
Label valCompleted = new Label { Name = "valCompleted" };
valCompleted.Location = new Point(80, 20);
groupBox1.Controls.Add(valCompleted);
Label lblInProgress = new Label { Name = "lblInProgress", Text = "In Progress" };
lblInProgress.Location = new Point(20, 60);
groupBox1.Controls.Add(lblInProgress);
Label valInProgress = new Label { Name = "valInProgress" };
valInProgress.Location = new Point(80, 60);
groupBox1.Controls.Add(valInProgress);
}
simplest way for solution is that your code is already created when you did design. Code is created automatically in respective form's designer.cs or designer.vb file. To see this file, in solution explorer click on button 'Show all files'. Still for your understanding let me explain code
You have created groupbox using
GroupBox groupBox1 = new GroupBox();
To see this group box on form you need to add this groupbox to form
this.Controls.Add(groupBox1);
Similar in case of panel. If you want to add panel inside of groupbox then
groupbox1.Controls.Add(grid1);
Then add all labels inside of panel.
You will find similar kind of code in form's designer.cs.

Push down control automatically not working

Sometime ago I asked this question. I get an answer and it worked fine at time. But now, I'm trying to do the same but isn't working. I have a Form and a FlowLayoutPanel setted in the same way as the answer but it isn't working. Both Form has FLowLayoutPanel has set AutoSize to true and FlowDirection set to TopDown but the form is growing vertically without pushing down the progressBar control and label itself. Here's what's like my form after click on button a couple of times(the button's code is the same as in the accepted question in the link I have linked):
What am I missing?
Try this and see if it works!
public Form1()
{
InitializeComponent();
Size = new Size(400, 150);
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Size = new Size(200, 150);
panel.MaximumSize = new System.Drawing.Size(panel.Width, int.MaxValue);
panel.FlowDirection = FlowDirection.TopDown;
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Controls.Add(panel);
Label label = new Label();
label.Text = "Starting text!\n";
label.Padding = new System.Windows.Forms.Padding(0, 0, 0, 50);
label.AutoSize = true;
panel.Controls.Add(label);
ProgressBar progressBar = new ProgressBar();
progressBar.Location = new Point(0, 125);
progressBar.Size = new Size(190, 25);
panel.Controls.Add(progressBar);
Button button = new Button();
button.Location = new Point(275, 50);
button.Text = "Click me!";
button.Click += (object sender, EventArgs e) => { label.Text += "some more text, "; };
Controls.Add(button);
}
Ok, I've tested the solution suggested in the earlier post you made and it's working fine for me...
Test these things:
Make sure both the Label and the ProgressBar are located inside the FlowLayoutPanel
If you mean that it's growing horizontally <---->, then set the MaximumSize-Width of the FlowLayoutPanel to how wide it can be before switching to a new row (and from there growing vertically instead!)
Otherwise please provide more information so that I can help you from there.

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

TextBlock Alignment inside a radiobutton Windows Phone

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

setting DockStyle.Fill for dynamic controls does not resize when i move the splitter

I have a split container on panel 1 I have added a groupbox, in that groupbox is a flowcontrol which has dynamic number of textboxes, i have set both the groupbox and flowcontrol to dockstyle to fill.
In code i have also set the textboxes to dock style to fill, but they wont resize when i move the splitter, while the parent flowcontrol does resize.
Label labelInput = new Label();
TextBox listBoxNewInput = new TextBox();
listBoxNewInput.Name = ce.ToString();
labelInput.AutoSize = true;
labelInput.Font = new Font(labelInput.Font, FontStyle.Bold);
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Text = ts.ToString();
//listBoxNewInput.Width = 150;
listBoxNewInput.MinimumSize = new Size(200,150);
listBoxNewInput.MaximumSize = new Size(1000, 150);
listBoxNewInput.Dock = DockStyle.Fill;
listBoxNewInput.TextChanged += new EventHandler(listBoxNewInput_TextChanged);
//Add the newly created text box to the list of input text boxes
inputTextBoxesList.Add(listBoxNewInput);
//Add the labels and text box to the form
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.Controls.Add(listBoxNewInput);
if i try to put controls directly in to spliter panel 1 only the first two controls appear, which do resize when i move the splitter
splitContainer1.Panel1.Controls.Add(labelInput); splitContainer1.Panel1.Controls.Add(listBoxNewInput);
->if the controls when i put them in flow control resize, when i move the splitter that would be good
OR
->All controls appear when i put them directly into the splitter panel 1
Based on your comments and what I think you are trying to accomplish, I think you need to replace the FlowLayoutPanel with a TableLayoutPanel because it sounds like you are just stacking one TextBox below another.
Create a TableLayoutPanel with 1 column and 1 row.
Here is a working example:
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 150));
for (int i = 0; i < 4; i++) {
AddTextBox("TextBox #" + i.ToString());
}
private void AddTextBox(string info) {
TextBox tx = new TextBox();
tx.Multiline = true;
tx.Text = info;
tx.ScrollBars = ScrollBars.Vertical;
tx.WordWrap = true;
tx.Height = 150;
tx.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
tableLayoutPanel1.Controls.Add(tx);
}
Instead of docking, I set the height of the TextBox and then I set the Anchors so that when the SplitPanel resizes, the TextBoxes resize appropriately.

Categories

Resources