无法订阅PowerPacks.Shape.Click事件

时间:2016-09-11 19:35:58

标签: vb.net

帮助捕获OvalShape的鼠标点击。

我有一个带有图片框的表单(myPictureBox)。

单击myPictureBox上的任意位置时,myPictureBox.click将触发 - 此时将绘制OvalShape1。

第二次点击时,会触发myPictureBox.click并在此时绘制OvalShape2。

...除非您单击OvalShape1的内部或边框。然后选择OvalShape1并且不会引发myPictureBox.click。

如何捕获OvalShape1.Click事件?

MSDN代码无用:https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.shape.click.aspx

B / C我得到了一个:
错误BC30506 Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量。

我正在努力弄清楚如何将处理程序添加到OvalShape1

完整代码:

Public Class Form1

Public canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
Public OvalShape1, OvalShape2, OvalShape3 As New Microsoft.VisualBasic.PowerPacks.OvalShape

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  canvas.Parent = myPictureBox
  OvalShape1.Parent = canvas
  OvalShape2.Parent = canvas
  OvalShape3.Parent = canvas
 End Sub

 Private Sub myPictureBox_Click(sender As Object, e As EventArgs) Handles myPictureBox.Click

 ..additional code to start drawing ovalshapes which works fine

 End Sub


Private Sub OvalShape1_Click() Handles OvalShape1.Click
  msgbox("you have cicked the OvalShape1")
End Sub


End Class

1 个答案:

答案 0 :(得分:0)

你得到的错误告诉你究竟发生了什么。每当您使用Handles clause时,您订阅的事件的变量必须声明为WithEvents

将其添加到您的OvalShape#声明中,您应该很高兴:

Public WithEvents OvalShape1, OvalShape2, OvalShape3 As New Microsoft.VisualBasic.PowerPacks.OvalShape

另外,您还应该在事件处理程序方法中包含事件的参数:

Private Sub OvalShape1_Click(sender As Object, e As EventArgs) Handles OvalShape1.Click