У меня есть класс (форма).Я хочу создать объект myclass (форма) с событием щелчка мыши на форме во время выполнения.Я добавляю это, но событие щелчка не работает.в основном: ClickEvent не работает.
Я видел это в C #, но не могу преобразовать его в vb.net.
Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Shape
Inherits Control
Public x1 As Integer
Public x2 As Integer
Public y1 As Integer
Public y2 As Integer
Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, ByRef g As Graphics)
MyBase.New()
Me.x1 = x1 : Me.x2 = x2 : Me.y1 = y1 : Me.y2 = y2
draw(g)
AddHandler Me.Click, New EventHandler(AddressOf ClickEvent)
End Sub
Public Sub draw(ByRef egraphics As Graphics)
Dim _mybrush As Brush ' brush to fill vertex
_mybrush = New System.Drawing.SolidBrush(Color.White)
Dim _mypen As New Pen(Color.Black, 5) ' make pen to draw
_mypen.DashStyle = DashStyle.Solid
egraphics.FillRectangle(_mybrush, New Rectangle(x1, y1, (x2 - x1), (y2 - y1))) ' dont change between this line and next line if change you can not to see border
egraphics.DrawRectangle(_mypen, New Rectangle(x1, y1, (x2 - x1), (y2 - y1))) ' dont change between this line and next line if change you can not to see border
End Sub
Private Sub ClickEvent(sender As Object, e As System.EventArgs) 'Handles MyBase.Click
MsgBox("Hi")
End Sub
End Class
в форме1 Я добавляю этот код.
Public Class Form1
Private x1 As Integer, y1 As Integer
Dim WithEvents s As Shape
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
x1 = e.X
y1 = e.Y
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
s = New Shape(x1, y1, e.X, e.Y, Me.CreateGraphics)
Me.Controls.Add(s)
End Sub
End Class