if(TextBox1.Text!="")
{
TextBox t1=new TextBox();
t1.ID=TextBox1.Text;
TableRow tr=new TableRow();
TableCell td=new TableCell();
td.Controls.Add(t1);
tr.Cells.Add(td);
Table1.Rows.Add(tr);
}

解决方案 »

  1.   

    if(this.TextBox1.Text != "")
    {
     TextBox t = new TextBox();
     t.Name = this.TextBox.Text;
     t.Location = new point(100,200);
     t.Size = new Size(20,20);
     this.Controls.Add(t)
    }
      

  2.   


    楼主的意思是不是:如果输入“button"也可以新建一个按钮,按照输入的文字建立控件?
    如果是的话我只好帮你顶了
      

  3.   

    http://www.yesky.com/SoftChannel/72342380468109312/20020114/213862.shtml看看这个咯原理是差不多的拉
      

  4.   

    老实说这样做几乎很难实现,
    首先你就输入一个字符串"TextBox1",编译器根本就只是把它当作一个字符串处理,这样是不可能实现的。
    如果你能再有一个字段入表名它是什么类型的控件,如"TextBox",
    那么用反射中的InvokeMemeber()还是可以实现的
      

  5.   

    楼上说得怎么这么高深阿,prettysammi(旻)说得就对!!!
      

  6.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    namespace CreateControl
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(328, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(448, 16);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(536, 342);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private Point _location = new Point(0,0);  private void button1_Click(object sender, System.EventArgs e)
    {
    Type type = Type.GetType(this.textBox1.Text);
    if(type == null)
    type = Type.GetType("System.Windows.Forms." + this.textBox1.Text + ",System.Windows.Forms,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
    if(type == null) return;
    if(typeof(Control).IsAssignableFrom(type))
    {
    Control control = System.Activator.CreateInstance(type, false) as Control;
    if(control == null ) return;
    control.Location = this._location;
    this._location.X += 10;
    this._location.Y += 10;
    this.Controls.Add(control);
    }
    }
    }
    }
      

  7.   

    我想知道楼主的意思是不是只要在TextBox框中输入一个字符串就产生一个相应的实例?
    比如如你所说输入一个"TextBox1"就产生一个TextBox的实例,且ID为TextBox1,
    如果真是想这样的话,那我就有几个问题想问你?
    第一:光根据一个字符串你怎么来判断它生成的是什么类型的控件,就如你所说的,输入一个“Button1"就应该要生成Button的控件实列,输入一个“Label1"就应该生成一个Label的空间实例!
    第二:或许你可以通过截取字符串来判定你要生成为什么控件的类型,比如"TextBox1"你可以截取出TextBox,从而知道它是一个TextBox类型的控件,但是现在截取的还是字符串,这时要么用switch case (这样的话,扩展性极差级差),还是最终会选择反射。
    讲了这些我也不知道是不是理解错了你的意思,我只说了说我的想法
      

  8.   

    我的意思是:
        现在FORM上有N个TEXTBOX控件,名为"TEXT1","TEXT2"..."TEXTN",我要给这N个控件赋值.不知哪有比较好的办法.
      

  9.   

    用反射一定能实现,如:InvokeMember(...),里面有很多参数,你看一下MSDN,找到你要的那个方法。