0

Once I developed a vb6 code to use transparent controls (Don't remember if I used Buttons or PictrureBoxes) with coordinates as invisible tags & invisible labels to show the names of eachone at groupal photos like Facebook does. Now I'm trying to recreate the same code at vb.net but I can't reach to get it work..

If I use Buttons with transparent .backcolor, no-text and no-borders, flat style, etc. to mark the photo area, they become opaque when I move the mouse over the control. if I disable becomes invisible for the mouse-over function.

If I use empty PictureBoxes instead for the same purpose, as are empty they became invisible at runtime also for the "mouse over" function...

I don't know which empty or invisible control must use to this finality. any suggestion?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
arc95
  • 97
  • 1
  • 8
  • Don't use controls. Just use a list of rectangles and see if the user is clicking inside one of those rectangles. To associate more data with a rectangle, create a class. – LarsTech Jan 26 '18 at 23:35
  • As suggested, the best way to use transparent controls as invisible triggers is to not do it. All you have to do is handle the `MouseClick` event of the form or any other control be notified when it was clicked and where. You then just test whether the location of the click corresponds to an area that is supposed to represent a "trigger". If each area is represented by a `Rectangle` then you simply call `Contains` on each one. One option would be to create a `Dictionary(Of Rectangle, Action)` and execute the `Action` associated with the `Rectangle` that contains the click location. – jmcilhinney Jan 27 '18 at 01:38
  • @jmcilhinney There is an easy way to draw the rectangle shape? i must install "powerpacks" to get the automatic shapes? those shapes created at runtime could be indexed at control array or are just drawings over the PictureBox?... I remember at vb6 code I just use write/read from txt file 4 coordinates to draw the transparent rectangle or control, and using the .tag property to save the names into the label. but vb.net seems more complex and requires hard work! – arc95 Jan 27 '18 at 02:53
  • There's nothing to draw. I'm not talking about a `RectangleShape`. I'm talking about a `Rectangle`. It's not a control; it has no UI. It's just a representation of a rectangle, just as a `Point` is a representation of a point. – jmcilhinney Jan 27 '18 at 03:30

1 Answers1

1

Here is an example of what I was talking about in my comments:

Public Class Form1

    Private ReadOnly actionsByRectangle As New Dictionary(Of Rectangle, Action)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'If the user clicks near the top, left corner, display a message.
        actionsByRectangle.Add(New Rectangle(10, 10, 100, 100),
                               Sub() MessageBox.Show("Hello World"))

        'If the user clicks near the bottom, right corner, minimise the form.
        actionsByRectangle.Add(New Rectangle(ClientSize.Width - 110,
                                             ClientSize.Height - 110,
                                             100,
                                             100),
                               Sub() WindowState = FormWindowState.Minimized)
    End Sub

    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        For Each rectangle As Rectangle In actionsByRectangle.Keys
            If rectangle.Contains(e.Location) Then
                'We have found a rectangle containing the point that was clicked so execute the corresponding action.
                actionsByRectangle(rectangle).Invoke()

                'Don't look for any more matches.
                Exit For
            End If
        Next
    End Sub

    'Uncomment the code below to see the click targets drawn on the form.
    'Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    '    For Each rectangle As Rectangle In actionsByRectangle.Keys
    '        e.Graphics.DrawRectangle(Pens.Black, rectangle)
    '    Next
    'End Sub

End Class

Note that I have added code there that can draw the boxes on the form if you want to see them, but those are just representations of the areas, not the Rectangle values themselves.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46