Creating a win form label in c# code using variable? - c#

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

Related

Can c# windows form list view have item template with 2 rows and 2 columns

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

Restrict the numbers of controls in a winform

i have a button in my form and for every button click it is adding groupBox. but i want a winform contain only 8 groupboxes. when the numbers of groupBox reach 8 it will automatically do Visible=false all 8 before groupBox and again adds a groupBox named(groupBox9). what must i do?
private void butonYeni_Click(object sender, EventArgs e)
{
//creating Font
Font font = new Font("Microsoft Sans Serif", 10.0f, FontStyle.Bold);
Font font2 = new Font("Microsoft Sans Serif", 9.0f, FontStyle.Bold);
int sayGB = 0;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(GroupBox))
{
sayGB++;
}
for (int i = sayGB; i < 1000; i++)
{
//creating groupbox
GroupBox Group = new GroupBox();
Group.Width = 767;
Group.Height = 179;
Group.Text = "Soru & Cevap";
Group.Font = font;
Group.ForeColor = Color.Maroon;
Group.Location = new Point(200,66);
//creating label
Label Soru = new Label();
Soru.Text = "Soru: ";
Soru.Font = font2;
Soru.ForeColor = Color.Maroon;
Soru.Location = new Point(6,33);
Soru.Width = 53;
Soru.Height = 13;
//creating textbox
TextBox soruText = new TextBox();
soruText.Width = 685;
soruText.Height = 20;
soruText.Font = font2;
soruText.ForeColor = Color.Black;
soruText.Multiline = true;
soruText.Location = new Point(70,31);
//creating label
Label Cevap = new Label();
Cevap.Text = "Cevap:";
Cevap.Font = font2;
Cevap.ForeColor = Color.Maroon;
Cevap.Location = new Point(6, 92);
Cevap.Width = 53;
Cevap.Height = 25;
//creating textbox
TextBox cevapText = new TextBox();
cevapText.Width = 685;
cevapText.Height = 69;
cevapText.Font = font2;
cevapText.ForeColor = Color.Black;
cevapText.Multiline = true;
cevapText.Location = new Point(70,67);
//creating button
Button btn = new Button();
btn.Width = 75;
btn.Height = 25;
btn.Text = "Kaydet";
btn.BackColor = Color.Maroon;
btn.Font = font2;
btn.ForeColor = Color.White;
btn.Location = new Point(682,148);
//kontrolleri ekleme
Group.Controls.Add(btn);
Group.Controls.Add(Soru);
Group.Controls.Add(soruText);
Group.Controls.Add(Cevap);
Group.Controls.Add(cevapText);
this.Controls.Add(Group);
}
}
}
define an integer class variable and increase it everytime the button is clicked. After increasing it, check if it's greater than 8. If it is, set your stuff to Visible = false.
Mock up:
public class MyClass
{
private int groupboxCounter = 0;
public MyClass()
{
}
private void btn_click(...)
{
// add a new groupbox here
groupboxCounter++;
if (groupboxCounter > 8)
{
//make stuff invisible here
}
}
}

TableLayoutPanel changing it's sizing

I have a few tablelayoutpanels in my program in order to lay out different controls in rows. Until recently these were all working correctly, but after a change in server this is no longer the case. A potential cause is a change in screen resolution as I noticed similar problems on other computers running my program.
An example of this problem is shown below
Before the data rows are loaded (from database), the sizing is incorrect. A measurement using the VS designer shows that the size in figure 2 is correct. Interestingly, if I add a blank row to this table then the resizing doesn't happen.
public JobOpMainTable()
{
DoubleBuffered = true;
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
DoubleBuffered = true;
BackColor = Color.White;
CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
headers();
}
private void headers()
{
string[] names = { "Operation", "Est Lab Hrs", "Est UM Hrs", "Act Lab Hrs", "Act UM Hrs" };
for (int i = 0; i < names.Length; i++) header(names[i], i);
}
private void header(string name, int col)
{
Panel pan = new Panel();
pan.Width = widths[col];
pan.Height = 25;
pan.BackColor = Color.Black;
pan.Dock = DockStyle.Fill;
pan.Margin = new System.Windows.Forms.Padding(0);
TextBox txt = new TextBox();
txt.Font = new System.Drawing.Font("Microsoft sans serif", 8, FontStyle.Bold);
txt.Text = name;
txt.ReadOnly = true;
txt.BackColor = Color.Black;
txt.ForeColor = Color.White;
txt.Dock = DockStyle.Fill;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None
pan.Controls.Add(txt);
Controls.Add(pan, col, 0);
}
private int newRow()
{
int row = Controls.Count / 5;
for (int i = 0; i <= 4; i++)
{
TextBox txt = new TextBox();
txt.Width = widths[i];
txt.BackColor = Color.White;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
txt.ReadOnly = true;
txt.Dock = DockStyle.Fill;
txt.Margin = new Padding(0);
txt.Enter += new EventHandler(enterCell);
txt.Leave += new EventHandler(leaveCell);
Controls.Add(txt, i, row);
}
return row;
}
Above is the code I believe is relevant to the problem. The resizing occurs the first time newRow() is called when loading in data: each new box makes the column wider. Again though, the odd thing is this doesn't occur if I add a row from the constructor

Align textbox after dynamic labels

This is my code:
for (int i = 0; i < gBoxes.Length; i++)
{
var LabelID = new Label();
gLabels[i] = LabelID;
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + i;
LabelID.Text = gColumns[i];
LabelID.Location = new System.Drawing.Point(12, StartLoc);
this.Controls.Add(LabelID);
iPanel.Controls.Add(LabelID);
var BoxID = new TextBox();
gBoxes[i] = BoxID;
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + i;
BoxID.Text = gContent[i];
BoxID.Location = new System.Drawing.Point(12, StartLoc);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
iPanel.Controls.Add(BoxID);
StartLoc += 25;
}
Which works fine, however, the labels overlap the boxes. Which would be the best method to place the boxes after the labels, and that the boxes are aligned together.
Result:
Set the Label.AutoSize property to true. (The default value when adding them in the designer is true but the default when adding by code is false.) Also set your TextBox.Location to have a larger x value than your label... you have the starting location of label and textbox at the same x value of 12.
You can also use the AutoSize property to determine how wide the labels are and then place the textboxes accordingly. Add your labels with AutoSize = true. Arrange the text boxes by determining the widest label and resetting the TextBox.Location just to the right of them. Here is an example:
public partial class Form1 : Form
{
public int YPos { get; set; }
List<string> Labels = new List<string>();
List<Label> LabelControls = new List<Label>();
List<TextBox> TextBoxControls = new List<TextBox>();
public Form1()
{
InitializeComponent();
AddRow("medium string", "medium");
AddRow("This is a longer string", "long");
AddRow("l", "little");
Arrange();
}
void AddRow(string label, string value)
{
Labels.Add(label);
var LabelID = new Label();
LabelID.AutoSize = true; // make sure to enable AutoSize
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + Labels.Count;
LabelID.Text = label;
LabelID.Location = new System.Drawing.Point(12, YPos);
this.Controls.Add(LabelID);
panel1.Controls.Add(LabelID);
LabelControls.Add(LabelID);
var BoxID = new TextBox();
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + Labels.Count;
BoxID.Text = value;
BoxID.Location = new System.Drawing.Point(12, YPos);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
panel1.Controls.Add(BoxID);
TextBoxControls.Add(BoxID);
// both controls have the same Y location
// and initially will have the same X location
YPos += 25;
}
void Arrange()
{
// determine the widest label sized by the AutoSize
int maxLabelX = 0;
for (int i = 0; i < Labels.Count; i++)
{
maxLabelX = Math.Max(maxLabelX, LabelControls[i].Location.X + LabelControls[i].Size.Width);
}
// move all the text boxes a little to the right of the widest label
for (int i = 0; i < Labels.Count; i++)
{
TextBoxControls[i].Location = new Point(maxLabelX + 10, TextBoxControls[i].Location.Y);
}
}
}
The above code generates properly placed TextBox controls:

Dynamically create array of textboxes and labels winforms

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

Categories