如题。
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 也可以实现吗?
最好能列出实例!

解决方案 »

  1.   

    delphi的FindComponent是通过遍历子控件实现的,你可以参照vcl源代码写
      

  2.   

    --------大概像这样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;
            }
      

  3.   

    ((Label)容器.FindControl("Label" + i)).Text = XXX;
      

  4.   

    比如 遍历groupBox控件的每一个子控件的写法 
    foreach(Control con in groupBox3.Controls)              con.Enabled=true;
    参考
    http://www.cnblogs.com/dudu/archive/2004/09/10/41655.html
      

  5.   

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

  6.   

    好象大家的都有些理解错误了,可能是我的表达有误吧,请见谅!
    =================================================
    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]是已经存在了的)。
      

  7.   

    TextBox textbox = this.Controls.Find("TextBox" + i.ToString() + "1", true);
    if(textbox != null)
    textbox.Text = "TEXt";
      

  8.   

    TO:icehawk(流浪他乡) 
    能不能完整点?谢谢了!
    提示出错如下:
    "System.Windows.Forms.Control.ControlCollection"并不包含对"Find"的定义。
      

  9.   

    public System.Windows.Forms.Control[] Find(string key, bool searchAllChildren)
        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 (""). 
      

  10.   

    用jinjazz(近身剪)的办法啊private void button1_Click(object sender, EventArgs e)
    {
       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;
    }
      

  11.   

    用jinjazz(近身剪)的办法出了这个么个错误:
    无法将类型"System.Windows.Forms.Control"隐式转换为"System.Windows.Forms.TextBox"
      

  12.   

    TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");
      

  13.   

    FindControl 是aspx里的   winform里是另一个,暂时忘了- -#
      

  14.   

    的确是做窗体而不是做网页。
    似乎这两点的差距很大啊,看来我又错了,又没说清楚。
    =======================================================
    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);
    }
      

  15.   

    i = 1;
    TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");你这行代码的实际效果是FindControl("TextBox11")
                                             ~~~~~你需要学会基本的调试方法
      

  16.   

    i = 1;
    TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");你这行代码的实际效果是FindControl("TextBox11")==============================================
    我知道现在运行到这后对textbox的操作实际上就是对TextBox11的操作了,对不?
    但用
    if(textbox != null)
      textbox.Text = "TEXT";
    操作后,TextBox11的Text的值并未变成TEXT呀!
      

  17.   

    if(textbox != null)
    textbox.Text = "TEXT";
    else
    MessageBox.Show("没有找到控件");
      

  18.   

    嗯,是TextBox textbox = (TextBox )FindControl(this,"TextBox" + i.ToString() + "1");这句中的错误,应该是TextBox textbox = (TextBox )FindControl(this,"textBox" + i.ToString() + "1");
    但接着问题又来了,这是不是只能对TextBox有用?
    我把当中的TextBox 改成Label或Button后就无效了。
      

  19.   

    例如:
    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都是存在的。
      

  20.   

    Delphi做多了,都忘记C#是分大小写的了。
    唉.............