如何在类点击事件vb.net中添加

时间:2018-06-13 06:22:43

标签: vb.net mouseevent

我有一个班级(形状)。我想在运行时在窗体上使用鼠标单击事件创建myclass(s​​hape)的对象。 我添加它但单击事件不起作用。基本上: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
在form1中

我添加了这段代码。

    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

1 个答案:

答案 0 :(得分:0)

问题在于您是通过图形绘制矩形,但该绘图与控件位置无关。该类应类似于以下内容,并设置Top,Left,Height和Width属性。此外,控件本身不需要实现click事件,因为它继承自“Control”。

形状类:

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)

        Me.Left = x1
        Me.Top = y1
        Me.Width = x2 - x1
        Me.Height = y2 - y1

    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

End Class

表格代码:

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

    Public Sub clickevt(sender As Object, e As EventArgs) Handles s.Click
        MsgBox("Hi")
    End Sub

End Class

希望这有帮助!