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;
}
I'm working on a WinForms application in which I have one static TabControl with a tab on which I need to add multiple levels of additional tabs. The number of these tabs will change depending on the data being loaded to the form.
I can add the first line of dynamic tabs tp the static tab like this for example:
TabControl tabControlWafers = new TabControl();
tabControlWafers.Dock = DockStyle.Fill;
int numwafers = wafers.Count();
for (int m = 0; m < numwafers; m++)
{
TabPage tabPage = new TabPage()
{
Name = wafers[m]
};
tabPage.Text = wafers[m].ToString();
tabControlWafers.TabPages.Add(tabPage);
}
tabPage1.Controls.Add(tabControlWafers);
My problem is that now I need to add another level of dynamically created tabs to each of the pages created above. After creating the next tabs like before:
TabControl tabControlStructure = new TabControl();
tabControlStructure.Dock = DockStyle.Fill;
int numstruct = structures.Count();
for (int n = 0; n < numstruct; n++)
{
TabPage tabPagestruct = new TabPage()
{
Name = structures[n]
};
tabPagestruct.Text = structures[n].ToString();
tabControlStructure.Controls.Add(tabPagestruct);
}
How do I get the tabs created here onto each of the first three tabs?
You should be able to accomplish what you need by iterating over the TabPageCollection in tabControlWafers.TabPages, then creating and adding one of your tabControlStructure objects at each iteration. See below for an example of how this could be done. Note the example assumes tabControlWafers has already been created.
foreach (TabPage tp in tabControlWafers.TabPages)
{
TabControl tabControlStructure = new TabControl()
{
Dock = DockStyle.Fill
};
int numstruct = structures.Count();
for (int i = 0; i < numstruct; i++)
{
TabPage tabPagestruct = new TabPage()
{
Name = structures[i],
Text = structures[i]
};
tabControlStructure.TabPages.Add(tabPagestruct);
}
tp.Controls.Add(tabControlStructure);
}
Edit:
Below is a generic example of the method by which I would generate the net nested TabPage structure. Note that if this were real, production code I would pull the addition of subpages off into its own method (something like addSubPages(TapPage parent, String[] names). This is nothing but a simple, paste and run example to give a better picture of what I am describing.
public Form1()
{
InitializeComponent();
TabControl tc1 = new TabControl()
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc1.TabPages.Add(i.ToString());
}
foreach (TabPage tp in tc1.TabPages)
{
TabControl tc2 = new TabControl
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc2.TabPages.Add(tp.Text + "." + i.ToString());
}
tp.Controls.Add(tc2);
foreach (TabPage tp2 in tc2.TabPages)
{
TabControl tc3 = new TabControl
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc3.TabPages.Add(tp2.Text + "." + i.ToString());
}
tp2.Controls.Add(tc3);
}
}
this.Controls.Add(tc1);
}
The above example represents the constructor of an otherwise blank form that looks like the following:
I have TableLayoutPanel that I programatically add Rows to. The User basically choses a Property and that is then displayed in the table along with some controls. I think I have a general understanding problem here and I will try to explain it.
One of the Controls in every row is a 'delete'-Button. That button should delete the row it is in. What I did is add an eventhandler to the button and set the current rowcount.
deleteTalent.Click += (sender, e) => buttonClickHandler(numberOfRows);
Code of the handler:
private void buttonClickHandler(int rowCount)
{
int count = rowCount - 1;
for (int i = count; i < (count + 5); i++)
{
balanceTable.Controls.RemoveAt(count);
}
balanceTable.RowStyles.RemoveAt(count);
balanceTable.RowCount--;
}
I looked at it for hours and played around. But I can't find a working clean solution. I'm also pretty new to C#
Here's the complete Function that creates a new row:
private void addBalanceItems(ToolStripMenuItem item)
{
int numberOfRows = balanceTable.RowCount;
if (numberOfRows > 1)
{
balanceTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize));
}
balanceTable.Height = numberOfRows * 45;
Steigerungsrechner rechner = new Steigerungsrechner();
string tag = item.Tag.ToString();
//change that asap :(
if (tag == "A") { rechner.column = 1; }
if (tag == "B") { rechner.column = 2; }
if (tag == "C") { rechner.column = 3; }
if (tag == "D") { rechner.column = 4; }
if (tag == "E") { rechner.column = 5; }
if (tag == "F") { rechner.column = 6; }
if (tag == "G") { rechner.column = 7; }
if (tag == "H") { rechner.column = 8; }
Label talentName = new Label();
talentName.Text = item.Text;
talentName.Height = standardHeight;
talentName.TextAlign = ContentAlignment.MiddleLeft;
talentName.AutoSize = true;
Label cost = new Label();
cost.TextChanged += (sender, e) => costChangeHandler(cost);
cost.Height = standardHeight;
cost.TextAlign = ContentAlignment.MiddleLeft;
TextBox startValue = new TextBox();
startValue.TextChanged += (sender, e) => startValueChangeHandler(rechner, startValue, cost);
startValue.Height = standardHeight;
startValue.TextAlign = HorizontalAlignment.Center;
TextBox endValue = new TextBox();
endValue.TextChanged += (sender, e) => endValueChangeHandler(rechner, endValue, cost);
endValue.Height = standardHeight;
endValue.TextAlign = HorizontalAlignment.Center;
Button deleteTalent = new Button();
deleteTalent.Text = "x";
deleteTalent.Click += (sender, e) => buttonClickHandler(numberOfRows);
deleteTalent.Height = standardHeight;
balanceTable.Controls.Add(talentName);
balanceTable.Controls.Add(startValue);
balanceTable.Controls.Add(endValue);
balanceTable.Controls.Add(cost);
balanceTable.Controls.Add(deleteTalent);
balanceTable.Visible = true;
balanceTable.RowCount++;
}
Any help would be greatly appreciated! :)
Yeah, removing an arbitrary row from a TableLayoutPanel is not at all intuitive. They really screwed up the design on this one.
The only way to remove rows is by setting the RowCount property. This alone is strange enough; that property sure seems like it should be read-only and code that does this looks wrong to me every time I see it.
But beyond that, the consequence of this design is that you cannot remove rows from the middle. Resetting the RowCount property will just cause rows to be lopped off of the bottom.
The workaround is a bit unwieldy, with multiple steps to get wrong:
Remove the controls from the row you want to delete
If applicable, move those controls to to another row.
Move all of the controls in the other rows that come after the row you wish to delete up a row.
Finally, remove the last row by decrementing the value of the RowCount property.
A quick Google search reveals that someone has written and shared code purporting to do this. It's in VB.NET, but that should be easily translated into your native dialect.
I'll admit that I've been known to just punt and set the RowHeight of the row I wish to "remove" to 0. This way, autosizing does the work for you. You probably still want to remove the controls it contains, though.
Here is a static class that can help you remove any row by it's index:
using System.Windows.Forms;
public static class TableLayoutHelper
{
public static void RemoveArbitraryRow(TableLayoutPanel panel, int rowIndex)
{
if (rowIndex >= panel.RowCount)
{
return;
}
// delete all controls of row that we want to delete
for (int i = 0; i < panel.ColumnCount; i++)
{
var control = panel.GetControlFromPosition(i, rowIndex);
panel.Controls.Remove(control);
}
// move up row controls that comes after row we want to remove
for (int i = rowIndex + 1; i < panel.RowCount; i++)
{
for (int j = 0; j < panel.ColumnCount; j++)
{
var control = panel.GetControlFromPosition(j, i);
if (control != null)
{
panel.SetRow(control, i - 1);
}
}
}
var removeStyle = panel.RowCount - 1;
if (panel.RowStyles.Count > removeStyle)
panel.RowStyles.RemoveAt(removeStyle);
panel.RowCount--;
}
}
One thing to mention: controls that we get via panel.GetControlFromPosition(...) must be visible or it will return null instead of invisible controls.
Remove existing controls of rowCount at first
for(int i = 0; i < panel.ColumnCount; i++){
Control Control = panel.GetControlFromPosition(i, rowCount);
panel.Controls.Remove(Control);
}
Then remove row
panel.RowStyles.RemoveAt(rowCount-1);
Removing complete Table -
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowStyles.Clear();
Set your Headline of the Table again -
tableLayoutPanel.RowCount = 1;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
tableLayoutPanel.Controls.Add(new Label() { Text = "MONTH", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 0, tableLayoutPanel.RowCount - 1);
tableLayoutPanel.Controls.Add(new Label() { Text = "YEAR", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 1, tableLayoutPanel.RowCount - 1);
tableLayoutPanel.Controls.Add(new Label() { Text = "MEASURED WAFERS", Font = new Font("Century Gothic", 12, FontStyle.Bold), ForeColor = Color.LightGray }, 2, tableLayoutPanel.RowCount - 1);
3 Columns - 1 Row
Maybe someone can use my codesnipped, works proper good...
You cannot completely delete a row on tablelayoutpanel but there is a workaround:
Remove all the controls in the row, easier if you know the names of the controls cause you can call the dispose method.
Set the height of the row to maybe 2px using the row style method
(e.g. tablelayoutpanel1.Rowstyle(index).height=2)
For me this worked wonders the, row was completely collapsed the row regardless of the row index.
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));
}
In C#, I want to assign all 10 Checklistboxes on my page with a value (e.g. 1). What is the syntax so that I can use a variable? I.e. checklistbox(i).selectedIndex = 1;
for (int i = 1; i < 11; i++)
{
checklistbox1.selectedIndex = 1;
checklistbox2.selectedIndex = 1;
checklistbox3.selectedIndex = 1;
...
checklistbox10.selectedIndex = 1;
}
I guess you should use "FindControl" method inorder to do that as shown below.
for (int i = 1; i <= 10; i++)
{
(Page.FindControl("checklistbox" + i) as CheckBox).SelectedIndex = 1;
}
"checklistbox" is assumed as "ID" that is prefixed for all the checkboxes.
Hope this helps!!
You can loop over all the controls on the page and pick out the ones you need as described in this blog post by Kris Steele:
foreach (Control masterControl in Page.Controls)
{
if (masterControl is MasterPage)
{
foreach (Control formControl in masterControl.Controls)
{
if (formControl is System.Web.UI.HtmlControls.HtmlForm)
{
foreach (Control contentControl in formControl.Controls)
{
if (contentControl is ContentPlaceHolder)
{
foreach (Control childControl in contentControl.Controls)
{
if(childControl is CheckBoxList)
{
((CheckBoxList)childControl).SelectedIndex = 1;
}
}
}
}
}
}
}
}
This may not be a good idea if you have a lot of controls on the page.
You should create a List<CheckBoxList>:
var cbs = new List<CheckBoxList> { thingy, otherThingy, ... };
foreach (var cb in cbs)
cb.SelectedIndex = 0;