mapping a picturebox

MattFalz

New member
Joined
Jul 30, 2006
Messages
3
Programming Experience
Beginner
Hi All,

I am trying to map a picturebox . When the mouse is on a certain location in the picture box i require to display a label while if the move is not in the desired location the label should be hidden. The problem is that the label is not working properly as it only display in the first area whilst the rest are ignored.

To further explain my self I am posting a sample of my code

( in the black columns the label should be triggered while it hides in the white column.) If instead I replace the label with a message box it works fine !!!

Copy the code in a new windows app

VB.NET:
Public Class Form1
    Private pb As PictureBox
    Private lb As Label

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(400, 400)
        pb = New PictureBox
        pb.Location = New Point(10, 10)
        pb.Size = New Size(240, 240)
        pb.BackColor = Color.White
        AddHandler pb.Paint, AddressOf Pb_paint
        AddHandler pb.MouseMove, AddressOf Pb_Move
        Me.Controls.Add(pb)

        lb = New Label
        lb.Text = "test"
        lb.Size = New Size(200, 20)
        lb.BackColor = Color.Red
        lb.Hide()
        lb.Location = New Point(10, 250)
        Me.Controls.Add(lb)
    End Sub
    Private Sub Pb_paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        With e.Graphics
            .FillRectangle(Brushes.Black, 0, 0, 30, pb.Height)
            .FillRectangle(Brushes.Black, 60, 0, 30, pb.Height)
            .FillRectangle(Brushes.Black, 120, 0, 30, pb.Height)
            .FillRectangle(Brushes.Black, 180, 0, 30, pb.Height)
        End With

    End Sub

    Private Sub pb_Move(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim MouseCo As Point = pb.PointToClient(Windows.Forms.Cursor.Position)
        Dim pos As Integer = 0
        For i As Integer = 0 To 4
            If (MouseCo.X >= pos And MouseCo.X <= pos + 30) And (MouseCo.Y >= 0 And MouseCo.Y <= pb.Height) Then
                'MessageBox.Show("Test")
                lb.Show()
                Exit For
            Else
                lb.Hide()
            End If
            pos += 60
        Next
    End Sub
End Class

Thanks in advance,

Matt
 
Move "lb.Hide()" at end of handler (after For loop) and change "Exit For" to "Return". No "Else" clause.

You can also make code much simpler by adding the rectangles to an array of Rectangles, draw them with Graphics.FillRectangles method, for hit-testing iterate the array and check if theRectangle.Contains(e.Location).
 
Back
Top