Iterate through list of labels and assign text through the code behind - c#

The labels aren't populating with text and I'm not sure why. I need lbl0 to lbl6 filled with an incremented year.
int year= Convert.ToInt32(hdnYear.Value);
List<Label> lbl = new List<Label>();
for (int i = 0; i < 6; i++)
{
Label lbls = new Label();
lbl.Add(lbls);
int yearValue = (FY + i);
string lblID = "lbl" + i;
lbl[i].ID = lblID;
lbl[i].Text = yearValue.ToString();
}

you want to do something like this:
List<Label> labels = new List<Label>();
for(int i = 0; i < 6; i++)
{
Label newLabel = new Label();
//set Properties of newLabel like ID, Content etc
labels.Add(newLabel);
}
fyi: A TextBlock has a Text property, a Label has Content property

Related

Having dynamically created label on separate lines

The following method is used by me to create a set of dynamic widgets on a button click and display the contents of an array in the labels!
public void addLabel()
{
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i]+"\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
}
}
The problem I face is that some labels display on the same line but I want only one label in each line! How can I modify my code?
You should use flowLayoutPanel1.SetFlowBreak(lbl, true); like this:
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text += array[i] + "\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
flowLayoutPanel1.SetFlowBreak(lbl, true);
}
However currently you are creating the label in each iteration of the loop. If you just need one label with line breaks you can change your code like below:
Label lbl = new Label();
for (int i = 0; i < array.Length; i++)
{
lbl.Text += array[i] + "\n";
}
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
You can use this. This is simple...
public void addLabel()
{
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i] + "\n";
lbl.AutoSize = true;
flowLayoutPanel1.Controls.Add(lbl);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
}
}
Try this
int lblStartPosition = 100;
int lblStartPositionV = 25;
for (int i = 0; i < array.Length; i++)
{
Label lbl = new Label();
lbl.Text = array[i]+"\n";
lbl.AutoSize = true;
lbl.Location = new System.Drawing.Point(lblStartPosition , lblStartPositionV);
flowLayoutPanel1.Controls.Add(lbl);
lblstartPositionV += 30;
}

Creating Labels at run time from a variable

Im trying to create a label in runtime. the number of labels depends on the number of items of another variable and the labels do not show. The code is as follows.
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems + 1; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[0].Text;
this.Controls.Add(lblitems);
}
some help would be appreciated thanks.
You should change tillfrm.lvbasket.Items[0].Text to tillfrm.lvbasket.Items[i].Text.
And i < NoofItems + 1 to i < NoofItems, because array size is NoofItems.
Try it like this, create a function, make the array GLOBAL,
protected void myFunction()
{
int NoofItems = tillfrm.lvbasket.Items.Count;
for (int i = 0; i < NoofItems; i++)
{
Label lblitems = new Label();
lblitems.Name = "lblItems" + i;
lblitems.Font = new Font ("Calibri",lblitems.Font.Size);
lblitems.Location = new Point(95, (152 + (19 * i)));
lblitems.ForeColor = System.Drawing.Color.Black;
lblitems.Show();
lblitems.AutoSize = true;
lblitems.Text = tillfrm.lvbasket.Items[i].Text;
this.Controls.Add(lblitems);
}
}
Then call this function in Form_load() function or Page_load() function like this
protected void Form_Load(Object sender , EventArgs e)
{
myFunction();
}

Split the content of textbox into respective individual textboxes at run time in c#?

I want the text contents of a TextBox to split into respective individual Textbox carrying a single character using c# windows application form.
Eg : A single Textbox containing text as-[Orange]
Expected output:-
in 20 separate textboxes
[o][r][a][n][g][e][][][][][][][][][][][][][][]
Till now I have done this..
`string name = textBox1.Text;
string str = name;
int chunkSize = 1;
Int32 stringLength = str.Length;
for (int i = 0; i < stringLength; i += chunkSize)
{
TextBox txtbox = new TextBox();
//if (i + chunkSize > stringLength)
//chunkSize = stringLength - i;
string singlechar = str.Substring(i, chunkSize);
txtbox.TextAlign = HorizontalAlignment.Center;
txtbox.BorderStyle = BorderStyle.FixedSingle;
txtbox.Font = new Font(txtbox.Font, FontStyle.Bold);
txtbox.Text = singlechar;
//txtbox.MaxLength = 1;
int a = 30;
int x = (i + 10) * a;
txtbox.Text = txtbox.Text.ToUpper();
txtbox.Location = new System.Drawing.Point(x, 100);
txtbox.BackColor = Color.White;
txtbox.Size = new System.Drawing.Size(30, 20);
this.Controls.Add(txtbox);
}`
I think you want this:-
textBox1 = mainTextBox.Text[0] ;
textBox2 = mainTextBox.Text[1] ;
// and so on..
assuming other TextBoxs in Panel named panel1 you can write something like :
for (int i = 0; i < textBox1.Text.Length; i++)
((TextBox)panel1.Controls[i]).Text = textBox1.Text[i].ToString();

Create a defined number of Label in a for loop

I want to create dynamically 10 Labels inside a for loop
string labelName;
for(int i = 0; i < 10; i++)
{
labeName = "Label" & i;
// Creata & Instanciate the label here, How ?
}
How would you create a bunch of objects which weren't UI elements? Use a collection:
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
Label label = new Label();
// Set properties here
labels.Add(label);
}
You'll presumably want to add these labels to a form or page or whatever too...
List<string> labelName = new List<string>();
for(int i = 0; i < 10; i++)
{
labeName.Add(string.Concat("Label", i));
}

How to create list of checkboxes with scroll?

I'm trying this code:
arrList = new List<CheckBox>();
for (int j = 0; j < 20; j++)
{
CheckBox check = new CheckBox();
arrList.Add(check);
}
CheckBox[] cb = arrList.ToArray();
for (int i = 0; i < 20; i++)
{
cb[i].Text = "sometext";
cb[i].Location = new System.Drawing.Point(10, 15 + i * 20);
cb[i].BackColor = System.Drawing.Color.Silver;
cb[i].Name = "somename";
cb[i].Size = new System.Drawing.Size(59, 17);
cb[i].Checked = true;
groupBox1.Controls.Add(cb[i]);
}
How can I add a vertical scroll bar there? (maybe I should use something instead of groupbox?)
You could use a Panel control and set it's AutoScroll property to True

Categories