I need to create an array of label and text boxes dynamically.
I have a groupBox and I need to add the above to it.
What is the best way to align then correctly? How do you get the location?
below does not work
public void TestCreateInputLabelAndTextBox()
{
foreach (Parameter parameter in Params)
{
var lbl = new Label();
lbl.Name = "lbl" + parameter.Name;
lbl.Text = parameter.Name;
lbl.AutoSize = true;
lbl.Location = new Point(7, 30);
lbl.Name = "label1";
lbl.Size = new Size(35, 13);
var txtBox = new TextBox();
txtBox.Name = "txt" + parameter.Name;
txtBox.Text = parameter.Name;
txtBox.Location = new Point(20, 20);
txtBox.Location = new Point(49, 22);
txtBox.Size = new Size(100, 20);
groupBox1.Controls.Add(lbl);
groupBox1.Controls.Add(txtBox);
}
}
How do you do it?
you must make an array of textbox and lable
for example:
TextBox[] txt= new TextBox[10];
for (int i = 0; i <=10; i++) {
txt(i) = new TextBox();
txt(i).Text = i.Tostring();
if (i > 0) {
txt(i).Left = txt(i - 1).Right;
}
this.Controls.Add(txt(i));
}
Further to comments placed above I have used a tablePanelLayout.
I am sorry that my question seems not to have been clear.
I tried to be as concise as possible because I beleive that the more you say the more you can confuse somebody who might help you.
All I needed was some pointers.By using a tablePanellayout solved my problem.
thanks for your comments
Related
I have a list of items that is constantly updated from live readings, the newest items should be added on the top of the list.
is it possible to have an item template for the list view with 2 rows and 2 column for each item. (like attached photo).
I searched online i could not find a solution.
my second approach was to create Main Panel and append Panels to it sub panels to it containing the labels.I am only able to add the items in the bottom of the Main panel since i can calculate the location of the last added item.
Panel pan = new Panel();
pan.Name = "panel" + counter;
pan.BorderStyle = BorderStyle.FixedSingle;
Label label1 = new Label();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(0, 0);
label1.Name = "label" + counter;
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 1;
label1.Text = "label" + counter;
Label label2 = new Label();
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(0, 20);
label2.Name = "label2" + counter;
label2.Size = new System.Drawing.Size(35, 13);
label2.TabIndex = 2;
label2.Text = "label2" + counter;
Label label3 = new Label();
label3.AutoSize = true;
label3.Location = new System.Drawing.Point(40, 0);
label3.Name = "label3" + counter;
label3.Size = new System.Drawing.Size(35, 13);
label3.TabIndex = 3;
label3.Text = "label3" + counter;
Label label4 = new Label();
label4.AutoSize = true;
label4.Location = new System.Drawing.Point(40, 20);
label4.Name = "label4" + counter; ;
label4.Size = new System.Drawing.Size(35, 13);
label4.TabIndex = 4;
label4.Text = "label4" + counter;
pan.Location = new Point(0, counter * 50);
pan.Size = new Size(100, 50);
pan.Controls.Add(label1);
pan.Controls.Add(label2);
pan.Controls.Add(label3);
pan.Controls.Add(label4);
MainPanel.Controls.Add(pan);
counter++;
Thanks
I have dynamic Labels and TextBox's on one panel.
I can delete the Panel. No Problem but then I also don't know how to delete the Textboxes etc
and i hoped that i can refresh or clear the panel so that all labels and textboxes will deleted..
Label makeLabelC = new Label();
makeLabelC.Width = 100;
makeLabelC.Font = new Font(makeLabelC.Font.Name, 8, FontStyle.Bold | FontStyle.Underline);
makeLabelC.Location = new Point(400, 100);
makeLabelC.Name = e.Node.Text;
makeLabelC.Text = e.Node.Text;
this.Controls.Add(makeLabelC);
this.Controls.Add(panel1);
TextBox textboxC = new TextBox();
textboxC.Width = 100;
textboxC.Location = new Point(500, 100 );
textboxC.Name = e.Node.Text + "lbl";
textboxC.Text = "enter here";
this.Controls.Add(textboxC);
this.Controls.Add(panel1);
for (int z = 0; z < n; z++)
{
Label makeLabel = new Label();
makeLabel.Width = 100;
makeLabel.Location = new Point(400, 150 + 2 * z * makeLabel.Height);
makeLabel.Name = e.Node.Text;
makeLabel.Text = e.Node.Nodes[z].Text;
this.Controls.Add(makeLabel);
this.Controls.Add(panel1);
TextBox textbox = new TextBox();
textbox.Width = 100;
textbox.Location = new Point(500, 150 + 2 * z * textbox.Height);
textbox.Name = e.Node.Text + "lbl";
textbox.Text = "enter here";
this.Controls.Add(textbox);
this.Controls.Add(panel1);
}
}
is there a way with panel how to do this or an other solution?
I thought that the Panel can help me there...
thanks Janik
You are adding the controls to the form instead of the panel - which you also add multiple times
this.Controls.Add(panel1); // do this once
panel1.Controls.Add(textbox); // add the controls to the panel
Once you have done this, when you remove the panel, you will also remove its child controls.
Im trying to create event with different index for dynamically created GroupBox. With my actual code event for every groupbox is that same. How can i make event with different index for every groupbox? My Code:
public void LoadGry()
{
// GroupBox groupbox = new GroupBox();
Label nazwagry = new Label();
for(int i = 0; i < myCollection.Count; i++)
{
GroupBox groupbox = new GroupBox();
groupbox.Text = myCollection[i];
groupbox.Size = new Size(290, 131);
groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
groupbox.ForeColor = Color.White;
Label label1 = new Label();
label1.Text = groupbox.Text;
label1.AutoSize = true;
label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
groupbox.Controls.Add(label1);
Gry.Controls.Add(label1);
PictureBox picturebox = new PictureBox();
picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
picturebox.Size = new Size(75, 75);
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
picturebox.LoadAsync(myCollection3[i]);
groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
Label label2 = new Label();
label2.Text = "Status: " + "Aktualny";
label2.ForeColor = Color.Green;
label2.AutoSize = true;
label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
Gry.Controls.Add(label2);
Label zapiszopis = new Label();
zapiszopis.Text = myCollection4[i];
zapiszopis.Visible = false;
Gry.Controls.Add((Control)groupbox);
//MessageBox.Show("pokaz mi wysokosc");
}
}
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
{
groupbox.ForeColor = Color.Aqua;
this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
opishacka.Text = myCollection4[itest];
}
The problem is that the event setup is using the variable K value. For use the number instead you probably needs to create an expression manually to use the current value in each case.
BUT
You can easily do what you want using the following properties to attach values to controls.
1-) Tag in WinForms & WPF:
// Setup
pictureBox.Tag = i;
// Event
int i = (int) pictureBox.Tag;
2-) ViewState in WebForms
// Setup
ViewState[pictureBox.UniqueID] = i;
// Event
int i = (int) ViewState[pictureBox.UniqueID];
You can use many other techniques. I only post one for each popular framework. I guest that you are in a WinFors project.
Hope this help!
I have program with multi methods .
We create all the controls with methods .
One of methods is for creating textBox . It is like :
private TextBox textBox1;
public void CreateTextBox()
{
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(100, Position);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
Position += 30;
this.Controls.Add(this.textBox1);
}
There are several textBoxes in a form (the count of textbox might change between 10 to 20 ) .
So , If I want to create several textBoxes , Call methods like :
CreateTextBox();
CreateTextBox();
CreateTextBox();
If I want to have the Text of this textboxes , A code like this return me the last textBox Text :
MessageBox.Show(textBox1.Text);
My problem is ,,,, How can I detect the text of first called of CreateTextBox() and second called of CreateTextBox() ?
Thank u for read
You can use an array containing all TextBoxes:
var form = new Form();
var boxes = new TextBox[10];
for (int i = 0; i < boxes.Length; i++)
{
var box = new TextBox();
box.Location = new Point(10, 30 + 25 * i);
box.Size = new Size(100, 20);
form.Controls.Add(box);
boxes[i] = box;
}
var button = new Button();
button.Text = "Button";
button.Click += (o, e) =>
{
var message = String.Join(", ", boxes.Select(tb => tb.Text));
MessageBox.Show(message);
};
form.Controls.Add(button);
Application.Run(form);
I have this code to create a new label with a variable attached to it but I get an error (obviously). Is there a way to do this?
System.Windows.Forms.Label lbl_Task[i] = new System.Windows.Forms.Label();
I.e, if i == 4, the label would be called lbl_Task4. If i == 9, it would be lbl_Task9, etc.
Edit:
New code shows:
for (int i = 0; i < rows; i++)
{
//create a label for the Task Name
Label lbl = new Label();
lbl.Name = "lbl_Task" + i;
tableLayoutPanel1.SetColumnSpan(lbl, 4);
lbl.Dock = System.Windows.Forms.DockStyle.Fill;
lbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lbl.Location = new System.Drawing.Point(3, pointInt);
lbl.Size = new System.Drawing.Size(170, 24);
lbl.TabIndex = 8;
lbl.Text = Convert.ToString(dtTasks.Rows[i]["Task_Name"]);
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.Controls.Add(lbl);
pointInt += 24;
}
This compiles but does not show on my win form when I debug. Any ideas?
You are looking for something like this:
for(int i =0; i<10; i++)
{
Label lbl = new Label();
lbl.Name = "lbl_Task" + i;
// set other properties
this.Controls.Add(lbl);
}
Use a collection of Lables and try like this
List<Label> labels = new List<Label>();
for (int i = 0; i < 100; i++)
{
Label label = new Label();
// Set Lable properties
yourLayoutName.Controls.Add(label);//add the lable
labels.Add(label);
}