能给一个例子吗?比如已经有一个PictureBox控件,名为pic,哪么怎样得到一组属性都一样的PictureBox控件?

解决方案 »

  1.   

    创建当前 Object 的浅表副本。[Visual Basic]
    <Serializable>
    <ClassInterface(ClassInterfaceType.AutoDual)>
    Protected Function MemberwiseClone() As Object
    [C#]
    [Serializable]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    protected object MemberwiseClone();
    [C++]
    [Serializable]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    protected: Object* MemberwiseClone();
    [JScript]
    protected
       Serializable
       ClassInterface(ClassInterfaceType.AutoDual)
    function MemberwiseClone() : Object;
    返回值
    当前 Object 的浅表副本。
      

  2.   

    感谢您使用微软产品。
     
    在C#中提供了更加灵活的方式来创建控件数组,和VB6.0有很大的不同。在C#中,认为控件数组是包含控件的标准数组,是对象数组的一种。在C#中创建控件数组,需要您编写代码明确定义一个特定的控件类型的数组,然后添加控件到该数组中。
    下面这段简单的代码定义一个包含2个PictureBox的控件数组,在Button的Click事件中动态加载,可供您参考:通过赋值的方式使动态生成的pictureBox2和pictureBox3的属性和已经存在的pictureBox1的属性一致。
    private void button1_Click(object sender, System.EventArgs e)
    {
    PictureBox[] picArray;
    picArray = new PictureBox[2];
    for( int i = 0 ; i < 2 ; i++)
    {
    picArray[i]= new PictureBox();
    picArray[i].Location = new System.Drawing.Point(pictureBox1.Location.X ,(i+1)*100);
    picArray[i].Image = pictureBox1.Image;
    picArray[i].BorderStyle = pictureBox1.BorderStyle;
    picArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
    picArray[i].Name = "pictureBox" + (i+2).ToString();
    picArray[i].Size = pictureBox1.Size;

    }
    this.Controls.AddRange(picArray);

     — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。 
      

  3.   

    还有问题是如何在控件数组的事件中区分出个体控件的事件?比如基于上面的代码
    private void picArray_Click(object sender, System.EventArgs e)
    只有两个参数,并没有数组的索引参数,如何区别是哪个picArray的Click事件?
      

  4.   

    区分事件如下 :
    如建立的控件数组名为 deskArray;
    建立时为每一个控件添加事件:
    deskArray[i].Click += new System.EventHandler(this.deskArray_Click); 
    事件处理如下:
    DeskCtl CurDesk;private void deskArray_Click(object sender, System.EventArgs e)
    {
        CurDesk = (DeskCtl)sender;
        //CurDesk 就是你要的那个控件了}
      

  5.   

    控件数组?不就是一个普通数组,然后其类型为控件吗?
    控件也是个类型了!比如TButton
      

  6.   

    上例中DeskCtl是控件   :)
    大家可以用button试一下, 不是数组也行!几个button执行一个事件
    在事件中加上
    Button btn = (Button)sender;
    messagebox.show(btn.text);
    就能看到效果了!
      

  7.   

    你可以判断控件的.Name属性或是.text属性,这样你就可以通过程序判断知道是第几个控件了。
    比如 acptvb(微软全球技术中心 VB技术支持)给出的例子,控件数组的name就不一样
    picArray[i].Name = "pictureBox" + (i+2).ToString();
    通过判断将.name 的后面数字串截取下来就知道了。
      

  8.   

    这样或许可以,但是总觉得好像不是一种正统的办法,至少在vb6中处理的方法要比这样好的多,不知道c#是否只能这样处理。
      

  9.   

    重载控件,在其中加入一个属性用于保存控件的序号.myTestGraph.cs  编译成:myTestGraph.dllusing System;
    using System.Windows.Forms;namespace myTestGraph
    {
    public class myPictureBox : System.Windows.Forms.PictureBox
    {
    private int myIndex;

    public myPictureBox()
    {

    }

    public int Tindex
    {
    get
    {
    return myIndex;
    }
    set
    {
    myIndex=value;
    }
    }
    }
    }测试用窗体,点击相应Picturebox,将弹出其序号
    myTestG.cs  编译为:myTestG.exeusing System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using myTestGraph;namespace myTestGraph
    {
    public class myTestG:System.Windows.Forms.Form
    {
    private myPictureBox[] myP; public  myTestG()
    {

    myP=new myPictureBox[3];
    for (int i=0;i<myP.Length;i++)
    {
    myP[i]=new myPictureBox();
    myP[i].Location=new Point(i*30,100);
    myP[i].Size=new Size(20,20);
    myP[i].BackColor=Color.Red ;
    myP[i].Tindex=i;
    myP[i].Click+=new System.EventHandler(myP_Click);
    this.Controls.Add(myP[i]);
    this.Name="TEst";
    }
       
    } public void myP_Click(object sender,System.EventArgs e)
    {
    MessageBox.Show  (((myPictureBox)sender).Tindex.ToString());
    }

    static void Main()
    {
    Application.Run(new myTestG());
    }
    }
    }
      

  10.   

    其实C#中的事件处理机制很有效,其返回的调用参数包括对象及
    事件参数,相对于VB中简单的返回控件的序号而言,有用得多。
    上面只是简单的调用了对象的一个属性而已。