如题。
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 也可以实现吗?
最好能列出实例!
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 也可以实现吗?
最好能列出实例!
{
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;
}
foreach(Control con in groupBox3.Controls) con.Enabled=true;
参考
http://www.cnblogs.com/dudu/archive/2004/09/10/41655.html
{
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));
}
}
}
}
}
}
=================================================
Delphi中Edit是这样的:
i := 1;
TEdit(FindComponent('Edit' + IntToStr(i) + '1')).Text := 'TEXT';
这样的话 Edit11.Text 的值就是TEXT了。
=================================================
在这里,TEdit(FindComponent('Edit' + IntToStr(i) + '1'))与Edit11所拥有的功能与操作是一样的。
其实我是想在C#中获取得到类似于上面Delphi中的TEdit(FindComponent('Edit' + IntToStr(i) + '1'))这样一个表达式,这样我在后面输入“.“后能出来Edit的所有功能列表。
而不是逐个去找出来是不是存在(label[i]是已经存在了的)。
if(textbox != null)
textbox.Text = "TEXt";
能不能完整点?谢谢了!
提示出错如下:
"System.Windows.Forms.Control.ControlCollection"并不包含对"Find"的定义。
Member of System.Windows.Forms.Control.ControlCollectionSummary:
Searches for controls by their System.Windows.Forms.Control.Name property and builds an array of all the controls that match.Parameters:
searchAllChildren: true to search all child controls; otherwise, false.
key: The key to locate in the System.Windows.Forms.Control.ControlCollection. Returns:
An array of type System.Windows.Forms.Control containing the matching controls.Exceptions:
System.ArgumentException: The key parameter is null or the empty string ("").
{
TextBox textbox = FindControl(this,"TextBox" + i.ToString() + "1");
if(textbox != null)
textbox.Text = "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;
}
无法将类型"System.Windows.Forms.Control"隐式转换为"System.Windows.Forms.TextBox"
似乎这两点的差距很大啊,看来我又错了,又没说清楚。
=======================================================
private void button1_Click(object sender, EventArgs e)
{
TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");
if(textbox != null)
textbox.Text = "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;
}
================================================================
以上代码能运行,但没有效果出来。当i为1时,按下button1后TextBox11.text所显示出来的值并未变成TEXT。
另:
在private void button1_Click(object sender, EventArgs e)中加上MessageBox.Show(textbox.Text);成为下面的代码后执行并点南button1后却总是提示:未处理的“System.NullReferenceException”类型的异常出现在 循环数.exe 中。
其他信息: 未将对象引用设置到对象的实例。
================================================================
private void button1_Click(object sender, EventArgs e)
{
i = 1;
TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");
if(textbox != null)
textbox.Text = "TEXT";
MessageBox.Show(textbox.Text);
}
TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");你这行代码的实际效果是FindControl("TextBox11")
~~~~~你需要学会基本的调试方法
TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");你这行代码的实际效果是FindControl("TextBox11")==============================================
我知道现在运行到这后对textbox的操作实际上就是对TextBox11的操作了,对不?
但用
if(textbox != null)
textbox.Text = "TEXT";
操作后,TextBox11的Text的值并未变成TEXT呀!
textbox.Text = "TEXT";
else
MessageBox.Show("没有找到控件");
但接着问题又来了,这是不是只能对TextBox有用?
我把当中的TextBox 改成Label或Button后就无效了。
private void button1_Click(object sender, EventArgs e)
{
Label label = (Label)FindControl(this,"label11");
if(label != null)
label.Text = "TEXT";Button btn = (Button)FindControl(this,"button1");
if(btn != null)
btn.Text = "TEXT";
}
注:label11、button1都是存在的。
唉.............