简体   繁体   中英

How do I convert picturebox.click event into a button.click event

I'm trying to have two options to draw a border around a picture box. I can click on the picturebox to highlight but now would like to be able to use a button to do the same thing.

 Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click

Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle BorderBounds.Inflate(-1, -1)

ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics, BorderBounds, Color.Orange, ButtonBorderStyle.Solid)

If Not (HighLightededPictureBox Is Nothing) Then
    HighLightededPictureBox.Invalidate()
End If

'Rememeber the last highlighted PictureBox  
HighLightededPictureBox = CType(sender, PictureBox)

When I try to add a button click I get the Exception Unhandled - System.windows.forms.button to type System.windows.forms.picturebox error.

I have tried to add the button click event after "Handles" which causes the above error.

 Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click, button1.click

I'm pretty new to programming and my searching results are turning anything up of value. I don't fully understand Ctypes/Directcasts.

Any help is greatly appreciated.

Ok, so I ended up going with this...

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim boxsize As New Size(192, 169)
    Dim recpoint As New Point(0, 0)
    Dim myrectangle As New Rectangle(recpoint, boxsize)
    myrectangle.Inflate(-3, -3)
    Dim G As Drawing.Graphics = PictureBox1.CreateGraphics
    Dim Pen As New Pen(Color.Orange, 5)
    G.DrawRectangle(Pen, myrectangle)
End Sub

Seems to be working ok, but requires a lot of manual entry of points. I have 6 more of these.

You could try

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    imgLabel_Click(imgLabel, nothing)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM