当焦点从textbox1离开的时候, 创建
textbox2 于 piont(10,20);
textbox3 于 point(20,20);
textbox4 于 point(30,20);
同时生成textbox4与textbox1一样的leave事件.就是说 我写完textbox1里的内容,这是会自动创建3个新的textbox2,textbox3,textbox4,同时在新的textbox4里面写完内容后,又会自动创建textbox5,texbox6,texbox7,textbox8;
textbox8也会同样实现上面的循环.
如果用textbox n = new textbox();每个textbox的location(x,y)值  是根据n值计算出来的.这个循环怎么实现啊?我不是太会用datagridview,不知道怎么保存datagridview的数据(非绑定数据)到数据库.以及按照用户的输入先后顺序给行排序,就是新增一行的时候 新行的第一个字段会出现一个行号,行号=上面的行号+1.以上2个问题给出好答案的追加分数,,,把分都散了,

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        struct TextBoxList
        {
            Point p;
            public TextBox tb0;
            public TextBox tb1;
            public TextBox tb2;
            public void Init(Point pt)
            {
                p = pt;
                tb0 = new TextBox();
                tb0.Location = pt;
                tb1 = new TextBox();
                tb1.Location = new Point(pt.X, pt.Y+30);
                tb2 = new TextBox();
                tb2.Location = new Point(pt.X , pt.Y+60);
            }
            public void AddToControls(Form frm)
            {
                frm.Controls.Add(tb0);
                frm.Controls.Add(tb1);
                frm.Controls.Add(tb2);
            }
        }
       
        public partial class Form1 : Form
        {
            List<TextBoxList> list = new List<TextBoxList>();
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                TextBoxList tbl = new TextBoxList();
                tbl.Init(pt);
                tbl.AddToControls(this);
                tbl.tb0.LostFocus += new EventHandler(tb0_LostFocus);
                list.Add(tbl);
            }
            Point pt = new Point(0, 0);
            void tb0_LostFocus(object sender, EventArgs e)
            {
                foreach (TextBoxList tb in list)
                {
                    if(tb.tb0 == (TextBox)sender)
                    {
                        //Do what you like with tb0
                    }
                    if (tb.tb1 == (TextBox)sender)
                    {
                        //Do what you like with tb1
                    }
                    //and so on...
                }
                TextBoxList tbl = new TextBoxList();
                tbl.Init(pt);
                tbl.AddToControls(this);
                tbl.tb0.LostFocus += new EventHandler(tb0_LostFocus);
                pt = new Point(pt.X + 40, pt.Y);
                list.Add(tbl);
              
            }
        }
    }
      

  2.   

    用Leave事件未必好,判断用户是否按了回车可能更好,下面的案例可以创建10个文本框
            private void CreateTextBox()
            { 
                TextBox t = new TextBox();
                t.Name = "textBox" + ++i;
                t.Location = new System.Drawing.Point(0, (i - 1) * 20);
                t.TabIndex = i;
                t.Visible = true;
                t.KeyDown += new KeyEventHandler(textBox1_KeyDown);
                this.Controls.Add(t);
                t.Focus();
            }        private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                TextBox t = sender as TextBox;
                if ( int.Parse(t.Name.Substring(t.Name.Length - 1, 1)) != i)
                    return;            if (e.KeyCode == Keys.Enter)
                {
                    CreateTextBox();
                }
            }
      

  3.   

    我觉得你这个最好是在回车后产生新textBox比较好using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private int x = 20;
            private ArrayList al = new ArrayList();
            public Form1()
            {
                InitializeComponent();
            }        private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if(e.KeyCode == Keys.Enter)
                {   
                    int i = al.Add(new TextBox());
                    ((TextBox)al[i]).Location = new System.Drawing.Point(20, al.Count*x);
                    ((TextBox)al[i]).Size = new System.Drawing.Size(100, 20);
                    ((TextBox)al[i]).KeyDown += textBox1_KeyDown; 
                }
            }
        }
    }
      

  4.   

    第二个问题1、在SQL server中,增加ID字段,设置标识,标识增量1
      访问时,可通过INSERT INTO tbable1 (User,Money) Values('张三',1000);SELECT @@IDENTITY的方式获得
      

  5.   

    按照输入先后顺序读取数据
    select * from table1 order by id asc
      

  6.   


    namespace WindowsFormsApplication33
    {
        public partial class Form1 : Form
        {
            int Count = 0;
            public Form1()
            {
                InitializeComponent();
                MakeTextBox(true); // 一开始只生成textBox1一个输入框
            }        void MakeTextBox(bool FirstTime)
            {
                TextBox TB = new TextBox();
                TB.Name = "textBox" + (Count + 1).ToString();
                TB.Text = "textBox" + (Count + 1).ToString();
                TB.Location = new Point(Count % 3 * 100, Count / 3 * 30);
                TB.Parent = this;
                if (Count % 3 == 0)
                    TB.KeyPress += new KeyPressEventHandler(TextBoxKeyPress);
                if ((++Count - 1) % 3 != 0 && !FirstTime)
                    MakeTextBox(false);
            }        void TextBoxKeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == Convert.ToChar((int)Keys.Enter))
                    MakeTextBox(false);
            }
        }
    }