首先感谢以前回答我问题的朋友们!这里有两个问题还想请朋友们帮忙解决:问题一:
        有三个TextBox横向并排TextBox1、TextBox2、TextBox3,后面跟一个按钮Button1,当我点击按钮Button1时,我想在三个TextBox下面对应的位置上再新建三个TextBox,和上面的三个TextBox一样也是横向并排,当我再点击按钮Button1时,在新建三个TextBox,还是同上面一样的排列,以此类推可以无限新建,这个应该怎么写。问题二:
        这三个TextBox里面前两个TextBox1、TextBox2分别填上数据以后TextBox3要自动作相应的变化(TextBox显示的是TextBox1的数据*TextBox2的数据),TextBox3要即时显示的,就是说我如果修改TextBox1或者TextBox2的数据TextBox3要相应的跟随变化,这个应该怎么写。就这两个问题有点啰嗦,希望朋友们帮忙,还是老规矩在线等回复,呵呵。

解决方案 »

  1.   

    第一个问题,点击之后直接调用from.controls.add应该可以实现。具体位置根据需要来设置就OK
    第二个问题,写一个textchanged函数,都给textbox1,textbox2的textchanged事件绑定上就可以,具体变化在textchanged里面写
      

  2.   

    for (int i = 0; i < 1; i++)
    {
      TextBox tb = new TextBox();
      tb.Location =new Point(10,10);//根据位置设置
      tb.Text = "text" + i.ToString();
      this.Controls.Add(tb);
    }
    动态添加textbox事件
    TextBox tb =sender as TextBox
      

  3.   

    同意1楼
    第一个问题比较麻烦的就是定位的问题,在增加文本框时,以第一排的位置位置,算好新增文本框的大小和间距;
    第二个问题,就是在txt1和txt2的textchanged的事件里添加相同的算术代码即可。
      

  4.   


    麻烦再详细一点好吗,我是新手,看不太明白啊。详细一点,比如说:第一个问题,点击之后直接调用from.controls.add应该可以实现。应该怎么调用。第二个问题,写一个textchanged函数,都给textbox1,textbox2的textchanged事件绑定上就可以。应该怎么写。
      

  5.   


    第一个问题3楼的代码已经可以了;
    第二个问题,在添加textbox的时候动态的绑定textchanged事件,跟普通窗体的事件绑定没什么不同的,
    textbox1.TextChanged += new ***
    需要操作的textbox最好都用一个list保存起来,这样会操作会方便点。
      

  6.   


    这个一次只能增加一个TextBox,我想一次增加三个在一排上(同时出来这三个),不是点一次出来一个。
      

  7.   


    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace DongTaiText
    {
    /// <summary>
    /// Form1 の概要の説明です。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows フォーム デザイナ サポートに必要です。
    //
    InitializeComponent(); //
    // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
    //
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(16, 8);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(120, 8);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "textBox2";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(224, 8);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 2;
    this.textBox3.Text = "textBox3";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(336, 8);
    this.button1.Name = "button1";
    this.button1.TabIndex = 3;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(424, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// アプリケーションのメイン エントリ ポイントです。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    int Floor=Convert.ToInt32(this.button1.Tag.ToString()); if(Floor==5)//允许五层
    return;
    foreach(Control txt in this.Controls)
    {
    if(txt is TextBox)
    {
    int j=Convert.ToInt32(txt.Name.Replace("textBox",""));
    if(j>=3*Floor-2 || j<=3*Floor)
    {
    TextBox tb=new TextBox();
    tb.Name="textBox"+Convert.ToString(j+3);
    tb.Text=tb.Name;
    tb.Location=new Point(txt.Location.X,txt.Location.Y+txt.Height);
    this.Controls.Add(tb);
    }
    }
    }
    this.button1.Tag=Floor+1;
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.button1.Tag=1;//第一层
    }

    }
    }
      

  8.   


    一个跟三个道理是一样的啊for (int i = 0; i < 3; i++)
    {
    TextBox tb = new TextBox();
    tb.Location =new Point(x,y);//这里要根据i的不同设置位置坐标
    tb.Name = "txt" + i.ToString();//这里最好根据一定的规则赋值,textchanged的时候容易找得到对应的textbox
    this.Controls.Add(tb);
    }[/code]
      

  9.   

    明天来结贴,今天还没弄明白,看看有没有再详细一点的答案了。我太笨了,呵呵。越详细越好。刚才8楼的 那段代码,我运行了一下,好像提示有错误
    就在这一段int j=Convert.ToInt32(txt.Name.Replace("textBox",""));
    好像提示有错误。
      

  10.   

    我在这边调试后,全部贴上来的,textBox的名字是这样的
    textBox1,textBox2,textBox3,
    textBox1下面生成的控件名字是 textBox(1+3)
    textBox2下面生成的控件名字是 textBox(2+3)
    textBox3下面生成的控件名字是 textBox(3+3)