能给一个例子吗?比如已经有一个PictureBox控件,名为pic,哪么怎样得到一组属性都一样的PictureBox控件?
解决方案 »
- c#动态创建了一组按钮如何在按钮打开的界面显示点击按钮的名称
- 打开文件夹,输入文件名
- 关于数据库连接--大家帮我找找错误吧!~~~
- 是不是我的vs 2005有问题?
- WEB水晶报表登录问题~~~~着急!!!在线等 谢谢!!!!!!
- 如何实现点击确定是不马上关闭窗体。
- 想让自制C#组件加载时调入XML文件,退出时保存XML文件,怎么办?
- 在CSDN专家排名二是【孟子E章】,【孟子E章】对不起,对不起,但我这个事情是真实的。这边向【孟子E章】道谴了。
- 关于C#属性(attribute)的问题和理解
- .Net Core 2.1控制台程序作为Linux守护进程(服务)不能后台常驻
- 哪位大侠给解释一下接口吧,能具体做些什么呢?
- 選擇目錄(非文件)的對話框
<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 的浅表副本。
在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))。
private void picArray_Click(object sender, System.EventArgs e)
只有两个参数,并没有数组的索引参数,如何区别是哪个picArray的Click事件?
如建立的控件数组名为 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 就是你要的那个控件了}
控件也是个类型了!比如TButton
大家可以用button试一下, 不是数组也行!几个button执行一个事件
在事件中加上
Button btn = (Button)sender;
messagebox.show(btn.text);
就能看到效果了!
比如 acptvb(微软全球技术中心 VB技术支持)给出的例子,控件数组的name就不一样
picArray[i].Name = "pictureBox" + (i+2).ToString();
通过判断将.name 的后面数字串截取下来就知道了。
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());
}
}
}
事件参数,相对于VB中简单的返回控件的序号而言,有用得多。
上面只是简单的调用了对象的一个属性而已。