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.
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 trying to add label in runtime when you click the button,
this is my code but when you click the button it just add new label in new position and the label you made before by click that button is not showing
and i want to show all of them one by one after click button
how can i fix it?
Label lbl;
int number;
int locationX = 2;
int locationY = 4;
public void CreateRuntimeControl(PictureBox pic)
{
lbl; = new Label();
number++;
locationX++;
locationY++;
lbl.Name = "lbl" + number.ToString();
lbl.Size = new System.Drawing.Size(100, 50);
lbl.Location = new System.Drawing.Point(10 + locationX, 10 + locationY);
lbl.Text = number.ToString();
lbl.BackColor = Color.Gray;
pic.Controls.Add(lbl);
}
Best Regards
You should move the lbl = new Label(); statement to the body of the CreateRuntimeControl() function, otherwise you are re-using the same label and just moving it instead of creating new one
Try:
int number;
int locationX = 2;
int locationY = 4;
public void CreateRuntimeControl(PictureBox pic)
{
Label lbl = new Label();
number++;
locationX++;
locationY++;
lbl.Name = "lbl" + number.ToString();
lbl.Size = new System.Drawing.Size(100, 50);
lbl.Location = new System.Drawing.Point(10 + locationX, 10 + locationY);
lbl.Text = number.ToString();
lbl.BackColor = Color.Gray;
pic.Controls.Add(lbl);
}
**************************************************************************************************************i found my answer and i want to put it here to if anyone else has my problem find this page and fix the problem
int number;
int locationX;
int locationY;
public void CreateRuntimeControl(PictureBox pic)
{
Label lbl = new Label();
number++;
locationX++;
locationY = locationY + 100;
lbl.Name = "lbl" + number.ToString();
lbl.Size = new System.Drawing.Size(100, 50);
lbl.Location = new System.Drawing.Point(10 + locationX, 10 + locationY);
lbl.Text = number.ToString();
lbl.BackColor = Color.Gray;
pic.Controls.Add(lbl);
}
Best Regards
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.
Good afternoon.
There was a problem with the rendering of dynamically created panel, all panels initially were 100px and I their renders (y + = 100), but now the production has changed a bit, these panels can be of different sizes, and the distance between them actually remains unchanged 100rh ...
Pliz tell me how to make, that they are somehow drawn at equal (10px) distances from each other. Read that you can somehow make a method SetBounds, but did not understand.
http://pastebin.com/TnSuFTti
for (int i = data_list.Count - 1; i >= 0; i--)
{
Panel panel = new Panel(); //создание блока сообщения и наложение картинки
panel.Name = i.ToString();
panel.MouseEnter += new EventHandler(panel_MouseEnter);
Label textBox_name = new Label();
Label textBox_date = new Label();
Label textBox_msg = new Label();
panel.Width = 308;
Bitmap btm_msg = new Bitmap(
Properties.Resources.NotificationCenterWindow_msg_box);
panel.BackgroundImage = btm_msg;
panel.BackgroundImageLayout = ImageLayout.Stretch;
panel.Location = new Point(5, 5);
panel.Controls.Add(textBox_date);
textBox_date.Name = "textBox_date" + i.ToString();
textBox_date.Location = new Point(232, 8);
textBox_date.Size = new System.Drawing.Size(70, 15);
textBox_date.BorderStyle = BorderStyle.None;
textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
textBox_date.TextAlign = ContentAlignment.MiddleRight;
textBox_date.BackColor = Color.DarkGray;
textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
textBox_date.ForeColor = SystemColors.ScrollBar;
panel.Controls.Add(textBox_name);
textBox_name.Size = new System.Drawing.Size(100, 15);
textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
textBox_name.Location = new Point(5, 8);
textBox_name.BorderStyle = BorderStyle.None;
textBox_name.BackColor = Color.DarkGray;
textBox_name.ForeColor = SystemColors.ScrollBar;
panel.Height = textBox_name.Height + 19;
panel1.Controls.Add(panel);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
y += 100;
}
panel.Location = new Point(5, y);
//.....
y = panel.Bottom + 10;
Just set the position based on the previous panel...
// This will put the panel to the right of an existing one
panel_b.Left = panel_a.Left + panel_a.Width + 10;
.
// This will put the panel below an existing one
panel_b.Top= panel_a.Top+ panel_a.Height+ 10;
I am working on an interface that uses a Wcf to obtain data from out database, I am having the following issue, I am using a dynamic window to display information as well as some buttons for date times, and close, etc...
However when I close the window and reopen it, it adds everything back to the form that was already there, which creates double and is causing some problems, any advice would be great, thanks in advance
Here is some code to better describe what I have....
This is the button I have on my primary form, which creates the new window
private void butMoreInf_Click(object sender, RoutedEventArgs e)
{
winMain.DataContext = null;
winMainContainer.DataContext = null;
winMainContainerLeft.DataContext = null;
winMainContainerRight.DataContext = null;
datGrid.DataContext = null;
//Generate the new window and panel to go within the window
winMain.Height = 750;
winMain.Width = 950;
winMainContainer.Width = 1000;
winMainContainer.HorizontalAlignment = HorizontalAlignment.Left;
winMainContainerLeft.Width = 120;
winMainContainerLeft.HorizontalAlignment = HorizontalAlignment.Left;
winMainContainerRight.Width = 880;
winMainContainerRight.HorizontalAlignment = HorizontalAlignment.Left;
//Generate the datagrid to contain the date to be changed etc.
datGrid.Margin = new Thickness(0, 12, 12, 12);
datGrid.HorizontalAlignment = HorizontalAlignment.Right;
//Generate a datepicker (start) and label
Label labS = new Label();
labS.Content = "Pick a start date";
labS.Width = 100;
labS.Margin = new Thickness(12, 1, 0, 1);
labS.HorizontalAlignment = HorizontalAlignment.Left;
DatePicker datPickStart = new DatePicker();
datPickStart.Width = 100;
datPickStart.Margin = new Thickness(12, 12, 1, 0);
datPickStart.HorizontalAlignment = HorizontalAlignment.Left;
datPickStart.SelectedDate = DateTime.Now.AddDays(-7);
datStartPick = datPickStart.SelectedDate == null ? DateTime.Now : Convert.ToDateTime(datPickStart.SelectedDate);
//Generate a datepicker (end) and label
Label labE = new Label();
labE.Content = "Pick an end date";
labE.Width = 100;
labE.Margin = new Thickness(12, 1, 0, 1);
labE.HorizontalAlignment = HorizontalAlignment.Left;
DatePicker datPickEnd = new DatePicker();
datPickEnd.Width = 100;
datPickEnd.Margin = new Thickness(12, 12, 1, 0);
datPickEnd.HorizontalAlignment = HorizontalAlignment.Left;
datPickEnd.SelectedDate = DateTime.Now;
datEndPick = datPickEnd.SelectedDate == null ? DateTime.Now : Convert.ToDateTime(datPickEnd.SelectedDate);
//Generate dropdown and label for that box
Label labY = new Label();
labY.Content = "";
labY.Width = 100;
labY.Margin = new Thickness(12, 1, 0, 1);
labY.HorizontalAlignment = HorizontalAlignment.Left;
ComboBox txtY = new ComboBox();
txtY.Width = 100;
txtY.Margin = new Thickness(12, 12, 1, 0);
txtY.HorizontalAlignment = HorizontalAlignment.Left;
txtY.SelectedIndex = 0;
txtY.SelectionChanged += CLLoadErrors;
txtY.SelectedIndex = 0;
//Generate error list button
Button butError = new Button();
butError.Width = 100;
butError.Margin = new Thickness(12, 12, 1, 0);
butError.HorizontalAlignment = HorizontalAlignment.Left;
butError.Content = "Get Errors";
butError.Click += CLLoadErrors;
//Generate clear button
Button butClear = new Button();
butClear.Width = 100;
butClear.Margin = new Thickness(12, 12, 1, 0);
butClear.HorizontalAlignment = HorizontalAlignment.Left;
butClear.Content = "Clear Grid";
butClear.Click += clearDataGrid;
//Generate close button
Button butClose = new Button();
butClose.Width = 100;
butClose.Margin = new Thickness(12, 12, 1, 0);
butClose.HorizontalAlignment = HorizontalAlignment.Left;
butClose.Content = "Close";
butClose.Click += CLHide;
//Add elements to the stackpanel / Also updates them before each instance
winMainContainerLeft.UpdateLayout();
winMainContainerLeft.Children.Add(labS);
winMainContainerLeft.Children.Add(datPickStart);
winMainContainerLeft.Children.Add(labE);
winMainContainerLeft.Children.Add(datPickEnd);
winMainContainerLeft.Children.Add(labY);
winMainContainerLeft.Children.Add(txtY);
winMainContainerLeft.Children.Add(butError);
winMainContainerLeft.Children.Add(butClear);
winMainContainerLeft.Children.Add(butClose);
winMainContainerRight.UpdateLayout();
winMainContainerRight.Children.Remove(datGrid);
winMainContainerRight.Children.Add(datGrid);
winMainContainer.UpdateLayout();
winMainContainer.Orientation = Orientation.Horizontal;
winMainContainer.Children.Remove(winMainContainerLeft);
winMainContainer.Children.Add(winMainContainerLeft);
winMainContainer.Children.Remove(winMainContainerRight);
winMainContainer.Children.Add(winMainContainerRight);
winMain.ResizeMode = ResizeMode.NoResize;
//Display the new form
winMain.UpdateLayout();
winMain.Content = winMainContainer;
winMain.Show();
datGrid.MouseDoubleClick += datGridDClick;
txtY.SelectionChanged += new SelectionChangedEventHandler(txtY_SelectionChanged);
}
Will provide more code if needed.
You are reusing the same window again and again (why?), so you need to clear out the children collections before you add new elements, otherwise every click on your button will show the window + every control you add, resulting in double/triple/... controls.
Update: I assume you use StackPanels (from your comment / having Children enumerations). The Children enumeration is a UIElementCollection which allows to invoke the Clear method. Just call this prior to adding new controls. (on winMainContainer, winMainContainerRight and winMainContainerLeft)
Where do you initialize winMain? Is it a static variable or does it live within your current form?
I'm guessing that you keep adding controls to an existing object(which causes the duplication effect). To fix this you could either reinitialize the form object
winMain = new WinMainForm();
or you could remove all controls on the winMain variable right before adding the new children
foreach(var c in winMainContainer.children){
winMainContainer.children.remove(c)
}
It looks like you aren't creating a new window at all, rather just performing operations on 'winMain' which exists at some higher scope. So every time you call the function you are just adding more and more children to the stackpanel.
You can solve the problem by doing something like this in the beginning of your method.
winMainContainerLeft.Clear();
winMainContainerRight.Clear();
winMainContainer.Children.Clear();
I also noticed that you have included the following lines in your example:
winMainContainer.Children.Remove(winMainContainerLeft);
winMainContainer.Children.Add(winMainContainerLeft);
winMainContainer.Children.Remove(winMainContainerRight);
winMainContainer.Children.Add(winMainContainerRight);
But you don't need to remove them before adding them (this doesn't really make sense anyway.)