代码中声明的控件事件 [英] Events for controls declared in code

查看:46
本文介绍了代码中声明的控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个面板阵列(昏暗面板(100)作为新的

面板)。我已经成功地将它们加载到表单上。


我应该编写什么代码才能访问每个面板的

mouseover事件(因为我不能
双击它们并选择此事件)。我希望能够通过INDEX识别它们来控制每个面板对

鼠标悬停事件的反应。

Just就像在VB 6.0中一样。 :P

I have created an array of panels (dim panels(100) as new
panel). I''ve succesfully loaded them on the form.

What code should I write in order to have acces to the
mouseover event for each of the panels (because I can''t
double-click on them and select this event). I want to be
able to control each of the panels'' reaction to the
mouseover event by identifying them through an INDEX.
Just like in VB 6.0. :P

推荐答案

嗨罗伯特,


这应该会给你一个想法。它是从两个不同的样品打印出来的,所以我这样做是为了打字错误


我希望这有帮助吗?


Cor

\\\

Private Sub Form5_Load(ByVal sender As Object,_

ByVal e As System.EventArgs)处理MyBase .Load

doset(Me)

End Sub

Private Sub doSet(ByVal parentCtr As Control)

For每个ctr作为控制在parentCtr.Controls

如果typeof ctr是Panel那么

AddHandler ctr.MouseLeave,AddressOf meMouseLeave

AddHandler ctr.GotFocus,地址:meMouseEnter

结束如果

doSet(ctr)

下一页

结束子

Private Sub meMouseLeave(ByVal sender _

As Object,ByVal e As System.EventArgs)Handles Panel1.MouseLeave

Me.Panel1.BackColor = Color.Blue
End Sub

Private Sub meMouseEnter(ByVal sender _

As Object,ByVal e As System.EventArgs)Handles

Me .Panel1.Back颜色=颜色。红色

结束次级

////

Hi Robert,

This should give you the idea. It is typed here from two different samples I
made so watch typos

I hope this helps?

Cor
\\\
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
For Each ctr as Control In parentCtr.Controls
if typeof ctr Is Panel then
AddHandler ctr.MouseLeave, AddressOf meMouseLeave
AddHandler ctr.GotFocus, AddressOf meMouseEnter
end if
doSet(ctr)
Next
End Sub
Private Sub meMouseLeave(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Me.Panel1.BackColor = Color.Blue
End Sub
Private Sub meMouseEnter(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles
Me.Panel1.BackColor = Color.Red
End Sub
////




" Robert" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:2e ***************************** @ phx.gbl ...

"Robert" <an*******@discussions.microsoft.com> wrote in message
news:2e*****************************@phx.gbl...
我创建了一个面板阵列(昏暗面板(100)作为新的面板)。我已经成功地将它们加载到表单上。

我应该编写什么代码才能访问每个面板的
mouseover事件(因为我不能<双击它们并选择此事件)。我希望能够通过INDEX识别它们来控制每个面板对鼠标悬停事件的反应。
就像在VB 6.0中一样。 :P
I have created an array of panels (dim panels(100) as new
panel). I''ve succesfully loaded them on the form.

What code should I write in order to have acces to the
mouseover event for each of the panels (because I can''t
double-click on them and select this event). I want to be
able to control each of the panels'' reaction to the
mouseover event by identifying them through an INDEX.
Just like in VB 6.0. :P




您将使用AddHandler语句连接您添加到SINGLE

过程中的每个面板供应。然后在该程序的代码中你将b / b
检查你的数组的发送者参数,如下所示:


对于x = 0到100

如果发件人是面板(x)那么

''现在你知道你有哪个面板x

结束如果

下一页


这是漫长的道路。设置每个面板的标签会更有效率,然后将其用作HashTable的一个键,而不是每次通过一个数组循环

。或者,现在我考虑更多,只需在添加它时将

标记设置为面板的索引,然后使用标记将

编入数组中。是的,这是票......



You''d use the AddHandler statement to wire up each panel you add to a SINGLE
procedure that you''ll supply. Then in the code of that procedure you''ll
check the sender argument against your array, like this:

For x = 0 To 100
If sender Is panels(x) Then
'' Now you know which panel you have based on x
End If
Next

That''s the long way. It would more efficient to set the Tag of each panel
you add and then use that as a key into a HashTable instead of looping
through an array each time. Or, now that I think about it more, just set the
Tag to the index of the panel as you add it and then use the tag to index
into the array. Yeah, that''s the ticket....


因为我输入它当然是一个很大的错误,最后一部分必须

可以是
\\\

Private Sub meMouseLeave(ByVal sender _

As Object, ByVal e As System.EventArgs)

directcast(发送者,面板).BackColor = Color.Blue

dim myPanelName as string = directcast(sender,panel).text

结束子

私人子meMouseEnter(ByVal发件人_

作为对象,ByVal e As System.EventArgs)

directcast (发件人,面板).BackColor = Color.Blue

dim myPanelName as string = directcast(sender,panel).text

End Sub

///

Cor
And because I typed it over of course a big error in it the last part has to
be something as

\\\
Private Sub meMouseLeave(ByVal sender _
As Object, ByVal e As System.EventArgs)
directcast(sender, panel).BackColor = Color.Blue
dim myPanelName as string = directcast(sender, panel).text
End Sub
Private Sub meMouseEnter(ByVal sender _
As Object, ByVal e As System.EventArgs)
directcast(sender, panel).BackColor = Color.Blue
dim myPanelName as string = directcast(sender, panel).text
End Sub
///
Cor


这篇关于代码中声明的控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆