Create a defined number of Label in a for loop - c#

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

Related

Form only recognizes one Groupbox property

I am trying to populate a run-time created Form.
First I create my text-properties that's supposed to each button
public static List<string> GetDialogs()
{
dialogs = new List<string>();
var tempString = "";
for (int i = 1; i <= FormSize.getHorizontalButtonCount(); i++)
{
for (int j = 1; j <= FormSize.getVerticalButtonCount(); j++)
{
//fx
tempString = $"{i}x{j*multiplier}";
dialogs.Add(tempString);
}
}
return dialogs;
}
and my list ends up like "1x5", "1x10", "1x15", "2x5" etc
Then I create all my Radiobuttons
public static List<RadioButton> CreateRadioButtons()
{
List<RadioButton> radioButtons = new List<RadioButton>();
for (int i = 0; i < FormSize.getRadioButtonCount(); i++)
{
var tempName = $"btn{i}";
radioButtons.Add(new RadioButton());
radioButtons[i].Name = tempName;
}
return radioButtons;
}
Which just creates a lot of buttons with some names like btn1 etc
Then I populate my Radiobutton-list with my dialog-list
private List<RadioButton> PopulateRadiobuttons()
{
dialogs = FormDialogs.GetDialogs();
List<RadioButton> tempRadioButtons = RadioButtonCreator.CreateRadioButtons();
for (int i = 0; i < FormSize.getRadioButtonCount(); i++)
{
tempRadioButtons[i].Text = dialogs[i];
}
return tempRadioButtons;
}
Now my Radiobuttons both contain a name like btn20 and a text like 4x30
Then I first populate a GroupBox with all my RadioButton elements before lastly populating my form with the groupbox.
public Form PopulateForm()
{
_box = new GroupBox();
radioButtons = PopulateRadiobuttons();
for (int i = 0; i < radioButtons.Count; i++)
{
_box.Controls.Add(radioButtons[i]);
}
_form.Controls.Add(_box);
return _form;
}
Besides all this, when I call this method to create a form it's only the first radiobutton that appears, the 1x5 one.
How could I go on about including all my buttons in my Form?
Here's what I do so far
private void Bt1_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
{
FormCreator fm = new FormCreator();
f1 = fm.PopulateForm();
f1.Show();
}

i want to access 'ids' of asp.net server controls in for loop.so with single line i can set value to all controls

for(int i = 1; i < 5;i++)
{
label[i].InnerText = info.books[0].title;
}
I want something like this to access id's of 5 server controls in loop.
label1,label2,.....,label5. It is a code behind file.
If you really wanted you can use FindControl
for (int i = 1; i < 5; i++)
{
Label lbl = Page.FindControl("Label" + i) as Label;
lbl.Text = info.books[0].title;
}

Iterate through list of labels and assign text through the code behind

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

Persisting state of nested dynamic controls

I have a page where I create many dynamic controls, the controls are deeply nested. I have no idea how to maintain/persist their value/state. The controls are created upon dropdown value changed event:
for(int i = 0; i < n; i++)
{
// Here I create radio button and add them
RadioButton rd = new RadioButton();
rd.id = "rd" + i;
for(int j = 0; j < 5; j++)
{
// Here I create textbox and add them to page
TextBox tbx = new TextBox();
tbx.id = "tbx" + j + i;
}
}

Issue while creating Nested TableLayoutPanel

I am try to implement the Nested TableLayoutPanel. I am try to dynamically Create/load the child TableLayoutPanel inside parent TableLayoutPanel.
for this I take the parent TableLayoutPanel and draw it from visual studio toolbox.
one DropDownList for dynamically to create child TableLayoutPanel I assign some values to dropdownlist such as 2*2,2*3,3*3,4*4 when the selected index change is fire is draws the child TableLayoutPanel.
My code is below
private void cmbRowsColumns_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedPair = new KeyValuePair<string, string>();
selectedPair = (KeyValuePair<string, string>)cmbRowsColumns.SelectedItem;
string[] rowcolumn = selectedPair.Value.Split('*');
string strRowCount = rowcolumn[0];
int rowCount = Convert.ToInt32(strRowCount);
string strColumnCount = rowcolumn[1];
int columnCount = Convert.ToInt32(strColumnCount);
DynamicallyGenerateColumn(rowCount, columnCount);
}
private void DynamicallyGenerateColumn(int rowCount, int columnCount)
{
parentTableLayoutPanel.Controls.Clear();
parentTableLayoutPanel.ColumnStyles.Clear();
parentTableLayoutPanel.RowStyles.Clear();
parentTableLayoutPanel.ColumnCount = columnCount;
parentTableLayoutPanel.RowCount = rowCount;
for (int i = 0; i < columnCount; i++)
{
parentTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
for (int j = 0; j < rowCount; j++)
{
if (i == 0)
{
parentTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
TableLayoutPanel objTableLayoutPanel = new TableLayoutPanel();
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);
}
}
}
but actually problem is when I create child TableLayoutPanel the formatting is not properly
I guess you want to fill each child panels So you need to add objTableLayoutPanel.Dock=DockStyle.Fill;
TableLayoutPanel objTableLayoutPanel = new TableLayoutPanel();
objTableLayoutPanel.Dock = DockStyle.Fill;
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);

Categories