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
Related
Why not lable_2 and lable_3 are not displayed?
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
The Y-position of the 2nd and 3rd labels is outside the bounds of the panel. Instead of giving the location, you can use the Dock property of the Label.
Label lbl = new Label();
lbl.Text = "Label test";
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Dock = DockStyle.Fill;
panel1.Controls.Add(lbl);
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.
I need to program about adding rooms. Its like this, when I press a Add button, there has to show a label, a textbox, a checkbox and a button(to reset the others). But when I dynamically create a button and press reset, it will reset the other textboxes and checkboxes also.
What do I have to type in my 'New button event'?
Can you guys help me out?
//this is my code
private void btnAdd_Click(object sender, EventArgs e) {
Label label = new Label();
int count = tableLayoutPanel1.Controls.OfType<Label>().ToList().Count;
label.Location = new Point(10, (25 * count) + 2);
label.Size = new Size(40, 20);
label.Name = "lbl" + (count + 1);
label.Text = "Kamer " + (count + 1);
label.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(label);
TextBox textbox = new TextBox();
int count1 = tableLayoutPanel1.Controls.OfType<TextBox>().ToList().Count;
textbox.Location = new System.Drawing.Point(60, 25 * count1);
textbox.Size = new System.Drawing.Size(80, 20);
textbox.Name = "textbox" + (count1 + 1);
textbox.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(textbox);
CheckBox checkbox = new CheckBox();
int count2 = tableLayoutPanel1.Controls.OfType<CheckBox>().ToList().Count;
checkbox.Location = new System.Drawing.Point(60, 25 * count2);
checkbox.Size = new System.Drawing.Size(80, 20);
checkbox.Name = "checkbox" + (count2 + 1);
checkbox.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(checkbox);
Button button = new Button();
int count3 = tableLayoutPanel1.Controls.OfType<Button>().ToList().Count;
button.Location = new Point(10, (25 * count3) + 2);
button.Size = new Size(140, 25);
button.Name = "button" + (count3 + 1);
button.Text = "Reset";
button.Anchor = AnchorStyles.None;
button.Click += new System.EventHandler(this.Button_Click);
tableLayoutPanel1.Controls.Add(button);
}
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.
I have multiple GroupBox's, and that's why I set AutoScroll to true. I create all controls in Form_Load. How to place one button after all GroupBox'es?
The code, where I create GroupBoxes:
for (int i = 0; i < 10; i++)
{
GroupBox gb = new GroupBox();
gb.Name = "GroupBox" + (i + 1);
gb.Size = new Size(500, 200);
gb.Location = new Point(40, loc);
gb.BackColor = System.Drawing.Color.Aquamarine;
Label q_text = new Label(); // текст питання
q_text.Name = "label" + (i + 1);
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(10, 10);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
if (i <= 5)
{
foreach (string key in questions[i].answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}else
if (i > 5)
{
foreach (string key in questions[i].answers.Keys)
{
CheckBox rb = new CheckBox();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}
this.Controls.Add(gb);
loc += 200;
Place all the scrollable group boxes in one panel which is set to AutoScroll = true. Below this panel is another one containing the fixed button. This panel is docked to the bottom.
Since you are using a loc variable, you can do:
btnMyButton.Locaction= new Point(40, loc);
Anyway, if you want to find the position of the last group box dynamically, try this:
double leftPos=0,topPos=0;
foreach(Control c in Forms.Controls)
{
if(c.Name=="GroupBox")
{
if(c.Left>leftPos)
leftPos=c.Left;
if(c.Top>topPos)
topPos=c.Top;
}
}