winform关于控件数组的问题

qyq520wei 2010-06-16 07:50:06
我的窗体一放有60多个pictureBox,从1到60依次排列的,怎么才能用数组来循环控制它们,对它们共同的click事件和mousemove事件进行操作,请高手帮一下忙,先谢谢了
...全文
306 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
倒霉熊 2010-06-17
  • 打赏
  • 举报
回复
UP 楼上都说完啦
zhuazhuqingchong 2010-06-17
  • 打赏
  • 举报
回复
List<Button> lb = new List<Button>();
for (int i = 0; i < lb.Count; i++)
{
lb[i].Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
do something....
}
spmzfz 2010-06-17
  • 打赏
  • 举报
回复

private void button7_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 60 ; i++)
{
PictureBox pb = this.Controls["pictureBox" + i] as PictureBox;
if(pb != null )
{
pb.Click += new EventHandler(pb_Click);
}
}
}



void pb_Click(object sender, EventArgs e)
{
PictureBox P = (PictureBox)sender;
MessageBox.Show(P.Name);

switch (P.Name)
{
case "pictureBox1":
this.Text = "1";
break;
case "pictureBox2":
this.Text = "2";
break;
//...
default:
this.Text =P.Name.Substring(10);
break;
}
}
江南小鱼 2010-06-16
  • 打赏
  • 举报
回复
WithEvents keyword

Specifies that one or more declared member variables refer to an instance of a class can raise events

AddressOf Operator

Creates a delegate procedure instance that references the specified procedure.

当窗体中同类控件存在多个的时候,要改变其中某一个此类控件的属性,或者确定该控件中的哪一个触发了某一事件…(有点啰嗦)

Procedure to display which button was pressed..

这样说吧,比如窗体中存在多个按钮,当单击某一个按钮的时候,改变该按钮的背景颜色;如果按钮数量比较少,可以使用条件语句捕捉按钮事件,如果按钮数量很多,这样子就会比较麻烦。可以考虑把按钮放在一个Panel中,然后为按钮定义统一事件,这样就可以确定哪个按钮被触发…


C#

private void Form1_Load(object sender, EventArgs e)

{

//控件数组功能

for (int i = 0; i < panel1.Controls.Count; i++)

{

panel1.Controls[i].Click += new System.EventHandler(this.btn_Click);

}

}

private void btn_Click(object sender, System.EventArgs e)

{

Button btn = (Button)sender;

btn.Text = "Change";

}


VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.AddEvent()

End Sub

Private Sub AddEvent()

For i As Integer = 0 To Panel1.Controls.Count - 1

AddHandler Panel1.Controls(i).Click, AddressOf Me.btn_Click

Next

End Sub

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

MessageBox.Show(CType(sender, Button).Text & " was clicked.")

End Sub

或者

多个按钮可以触发同一事件,不必逐个添加。

Private Sub bt10_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bt10.MouseClick, bt11.MouseClick, bt12.MouseClick, bt13.MouseClick, bt14.MouseClick, bt15.MouseClick, bt16.MouseClick, bt17.MouseClick, bt18.MouseClick, bt19.MouseClick, bt37.MouseClick, bt38.MouseClick, bt42.MouseClick

If e.Button = Windows.Forms.MouseButtons.Left Then

ContextMenuStrip1.Show(MousePosition)

End If

End Sub

sxldfang 2010-06-16
  • 打赏
  • 举报
回复
控件数组是vb中的一个概念。

在C#中你可以让60多个pictureBox共享同一个click事件或mousemove事件。事件中的参数object sender决定是哪个pictureBox激发了该事件。
CoCan168 2010-06-16
  • 打赏
  • 举报
回复
PictureBox[] pictureBoxes=new PictureBox[]{pictureBox1,pictureBox2....};

循环遍历pictureBoxes数组就行了。不明白“对它们共同的click事件和mousemove事件进行操作”你想要做什么,(用sender参数判断哪个picturebox控件被点击了)
wuyq11 2010-06-16
  • 打赏
  • 举报
回复
绑定同一个事件
this.pictureBox1.Click += new System.EventHandler(this.pictureBox_Click);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);

private void pictureBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
PictureBox pb=sender as PictureBox;
}

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧