我想要实现的东西大概是这样的,
   图片(label)  名称 (label2)   规格(label3)
    
Picturebox1     nametxtbox1       guigetxtbox1
………           ……             ……
Pictureboxn     nametxtboxn       guigetxtboxn
Picturebox 和nametxtbox的数目不定,我定义的事件是点label1就向下动态加载一个picturebox,点名称就加载一个nametxtbox。 n++这些实现了!
public void label1_MouseDown(object sender, MouseEventArgs e)
{  
            PictureBox pbox_n = new PictureBox();
            pbox_n.Size = new Size(70, 40);
            pbox_n.Location = new Point(125, 130 + 40 * n);
            this.Controls.Add(pbox_n);
}
public void label2_MouseDown(object sender, MouseEventArgs e)
           { TextBox name_n = new TextBox();
            name_n.TextChanged += new EventHandler(name_nTextChanged);
            name_n.Size = new Size(60, 21);
            name_n.Location = new Point(200, 140 + 40 * n);
            this.Controls.Add(name_n);
}
图片是根据输入的名称和规格到数据库中去调,为动态生成的控件nametxtbox加了一个事件nametxtbox_TextChange, 
private void name_nTextChanged(object sender, EventArgs e)
        {
         查询
}
在这个事件中,到库中查找要用nametxtboxn.Text ,放图片用Picturebox_n.Image. 在这里就出现了问题了,错误是上下文中不存在nametxtbox_n,picturebox_n。不在一个事件中就不存在啊?
我的想法能不能实现啊?
错误能解决吗?请各位大侠指点!多谢!
或者不这样,有更好的办法实现,多谢提个思路!

解决方案 »

  1.   

    如果你想用这个公共控件,你可以用属性或定义全局变量。
    如public TextBox NewBox
    {
    get{...}set{...}
    }
    然后你重新定义一个控件后就把这个新赋给这个属性
    然后在另一个地方调用该属性,就是相当于调用上一个局部控件。
      

  2.   

    你吧你这些东西放到 PictureBox pbox_n 
    类里面来声明, 然后在具体的方法里构造.如:public void label1_MouseDown(object sender, MouseEventArgs e)
    {   
      PictureBox pbox_n = new PictureBox();
      pbox_n.Size = new Size(70, 40);
      pbox_n.Location = new Point(125, 130 + 40 * n);
      this.Controls.Add(pbox_n);
    }=>PictureBox pbox_n=null;
    public void label1_MouseDown(object sender, MouseEventArgs e)
    {   
      pbox_n = new PictureBox();
      pbox_n.Size = new Size(70, 40);
      pbox_n.Location = new Point(125, 130 + 40 * n);
      this.Controls.Add(pbox_n);
    }
      

  3.   

    你还没搞明白事件委托都传递了什么参数sender就是textboxprivate void name_nTextChanged(object sender, EventArgs e)
    {
        TextBox tb=(TextBox)sender;
        string txt-=tb.text;
    }
      

  4.   

    private void name_nTextChanged(object sender, EventArgs e)
     {
      TextBox txt=sender as TextBox;
     }
      

  5.   

    LZ把要用的函数名都放到外面来,或者放到一个Public里 进行封装
    这样就可以调用了
      

  6.   

    这个就行了呀!
    我最近无聊写的软件里和你这个差不多!
    http://topic.csdn.net/u/20100529/12/6ff4d5cb-995a-4723-83e4-883cb54c3f0c.html