如何设置label[i]的值?i是程序中生成的
如题。
Delphi中Edit是这样的:
i := 1;
TEdit(FindComponent( 'Edit ' + IntToStr(i) + '1 ')).Text := 'TEXT ';
这样的话 Edit11.Text 的值就是TEXT了。
那如果我想在C#中实现当i的值为程序中生成的任意值时,都可以得到 label[i]呢?
假设 i = 1 ,j = 2 (j也是一程序中生成的值)
我想获得 label1 、label2 、label12 、label21 的操作,怎么弄?
其它如 textBox、button 也可以实现吗?
最好能列出实例!
------解决方案----------------------------大概像这样
private void button1_Click(object sender, EventArgs e)
{
this.Text = FindControl(this, "textBox1 ").Text;
}
private System.Windows.Forms.Control FindControl(System.Windows.Forms.Control control, string strName)
{
foreach (System.Windows.Forms.Control subControl in control.Controls)
{
if (subControl.Name == strName)
{
return subControl;
}
return FindControl(subControl, strName);
}
return null;
}
------解决方案--------------------for (int i =0; i <GlobalCategoryPanel.Controls.Count;i++)//GlobalCategoryPanel是个Panel控件
{
if(GlobalCategoryPanel.Controls[i] is AdvancedPanel)//AdvancedPanel是个自定义控件
{
AdvancedPanel ap=(AdvancedPanel)GlobalCategoryPanel.Controls[i];
for(int ap_i=0;ap_i <ap.Controls.Count;ap_i++)
{
if(ap.Controls[ap_i] is Panel)
{
Panel ap_panel=(Panel)ap.Controls[ap_i];
for(int ap_panel_i=0;ap_panel_i <ap_panel.Controls.Count;ap_panel_i++)
{
if(ap_panel.Controls[ap_panel_i] is RadioButtonList)
{
RadioButtonList rbl=(RadioButtonList)ap_panel.Controls[ap_panel_i];
al.Add(int.Parse(rbl.SelectedValue));
}
}
}
}
}
}
------解决方案--------------------TextBox textbox = this.Controls.Find( "TextBox " + i.ToString() + "1 ", true);
if(textbox != null)
textbox.Text = "TEXt ";