CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2000
    Location
    Holland
    Posts
    14

    Highliting label

    Hi,
    When we want cross over a label with caption, we want to highlight or color the caption. If we leave the label we want to change the color back to the normal color.

    Please help us
    Thank you



  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Highliting label

    Try this:

    private Sub Form_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
    If Label1.ForeColor <> vbBlack then
    Label1.ForeColor = vbBlack
    End If
    End Sub

    private Sub Label1_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
    If Label1.ForeColor <> vbRed then
    Label1.ForeColor = vbRed
    End If
    End Sub




    When the mouse is over the label, the text in the label will turn red. When the mouse is over the form it turns the text back to black. The if statments keep the code from re-assigning the color every time the mouse moves, to improve performance.

    Hope this helps,
    John


    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Dec 1999
    Location
    Texas
    Posts
    96

    Re: Highliting label

    That will work, but if there are any other controls on the form besides the one label, then it probably won't work well. I don't know of a goot way to perform this action using a label, but if you use a picturebox (with AutoRedraw set to True) you can use the SetCapture and ReleaseCapture API calls. Try this:

    option Explicit
    '
    'API Declarations
    private Declare Function SetCapture Lib "user32" _
    (byval hWnd as Long) as Long
    private Declare Function ReleaseCapture _
    Lib "user32" () as Long
    '
    private Sub Form_Load()
    'Make sure the picturebox's AutoRedraw property
    'is true.
    Picture1.AutoRedraw = true
    'Place the caption for the "make-shift" label
    'in the Tag property so we don't have to
    'remember it.
    Picture1.Tag = "My Label"
    'print the caption
    Picture1.print Picture1.Tag
    End Sub
    '
    private Sub Picture1_MouseMove (Button as Integer, Shift as Integer, X as Single, Y as Single)
    'Check to see if the mouse is outside of the
    'picturebox's borders
    If (X < 0 Or X > Picture1.Width) Or (Y < 0 Or Y > Picture1.Height) then
    'Mouse is not over the picturebox.
    ReleaseCapture
    'set the forecolor to black
    Picture1.ForeColor = vbBlack
    else
    'Mouse is within the borders
    SetCapture Picture1.hWnd
    'set the forecolor to the highlight color
    Picture1.ForeColor = vbBlue
    End If
    '
    'Clear the picturebox
    Picture1.Cls
    'Redraw the caption
    Picture1.print Picture1.Tag
    End Sub




    It's a little wierd, but it works. There may be a better (easier way) to do this, but I don't know what it is.

    Hope this helps,
    Rippin


  4. #4
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Highliting label

    I know this isn't the best code to do this, but i don't see why more controls on the form would make a difference in the functionality of it. I can see having to do it for 20 or 30 labels and all of them have to have this "highlight" function - that would be bad because the code would have to do 20 or 30 if checks to find the one label that needs changing, if the labels weren't in a control array. If they were, then setting the color would still be just the one liner above, but setting it back would still go through the array. Where is the OnMouseOut function that's in VBScript when you need it!?


    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  5. #5
    Join Date
    Dec 1999
    Location
    Texas
    Posts
    96

    Re: Highliting label

    The reason I say that it won't work well with multiple controls is because the Form_MouseMove might not fire if you move over, say, a commandbutton. Especially if the controls are positioned closely together. In order for it to work, you would have to add the code to every control's MouseMove event. Also you would need to loop though all other controls to make sure their color is not stuck in the highlight color. Like I said, it will work, I'm not disputing that, I'm just offering another possible solution. Please don't think I was trying to discredit your code.

    Rippin


  6. #6
    Join Date
    Nov 1999
    Posts
    14

    Re: Highliting label

    There is a great example of this here: http://www.mvps.org/vb/

    Go to the samples section and check out HyperLabel.Zip.
    It will do exactly what you want and probably more.



  7. #7
    Join Date
    Dec 1999
    Location
    MD, USA
    Posts
    34

    Re: Highliting label

    Nice ! tried it out......works smooth.......


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured