Solved

Drawing an outline around a label

Posted on 2004-10-13
8
269 Views
Last Modified: 2006-11-17
I would like some assistance with comeing up with some code.  I want to use the code below to paint a solid rectangle on a form, on the rectangle I will have a label with text in it, I want to draw a colored rectangle around the label as if it was the label's border when the mouse enters (hovers) over the label.  This should simulate the way Microsoft works with their menu's when the mouse hovers over the text or picture in the label it will paint the outline around the control.  It's the best I could think up to use without paying $300.00 for a control that will do all the work.  My way is free so to speak.


Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
         Dim g As Graphics = e.Graphics  
        Dim Brush1 As SolidBrush = New SolidBrush(Color.InactiveCaptionText)
        'first num is margin left side, second is margin top, third is leingth of box, fourth is height of box.  
        g.FillRectangle(Brush1, 3, 3, 50, 50)
End Sub
0
Comment
Question by:teamdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12305300
Hi teamdad,

You could use either of the techniques shown below or even combine them together.  Just add two labels to a form and this code:

    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        Label1.BackColor = Color.FromKnownColor(KnownColor.InactiveCaptionText)
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.BackColor = Color.FromKnownColor(KnownColor.Control)
    End Sub

    Private Sub Label2_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.MouseEnter
        Label2.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub Label2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.MouseLeave
        Label2.BorderStyle = BorderStyle.None
    End Sub
0
 

Author Comment

by:teamdad
ID: 12307880
I had hoped to be able to use the light blue BackColor with the darker blue BorderStyle color like Microsoft uses with their menu's like in MS Office but I guess I would have to purchase one of the $300.00 controls to get that good of an effect?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12308185
You can pick any predefined colors you want or create your own.  I'm not familiar with theses menus in office but here is an example of predefined colors and custom colors:

    ' Predefined Color
    Label1.BackColor = Color.DeepSkyBlue

    ' Custom Color
    Dim c As Color = Color.FromArgb(175, 0, 175)
    Label1.BackColor = c

If you want the Border to be a different color then I think you'll have to either create a UserControl based on the Label or create a Class that inherits from Label and then override the Paint method so you can paint the border yourself in the color of your choice.  I'll post an example in a bit.

~IM
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12312577
Here is a way to get the border of the label a different color without creating a user control or a new class that inherits from Label:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private inLabel1 As Boolean

    Private Sub Label1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseEnter
        Label1.BackColor = Color.DeepSkyBlue
        inLabel1 = True
    End Sub

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        If inLabel1 Then
            Dim r As Rectangle = New Rectangle(New Point(0, 0), Label1.Size)
            r.Width = r.Width - 1
            r.Height = r.Height - 1
            e.Graphics.DrawRectangle(Pens.Blue, r)
        End If
    End Sub

    Private Sub Label1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.MouseLeave
        Label1.BackColor = Color.FromKnownColor(KnownColor.Control)
        inLabel1 = False
    End Sub

End Class
0
 

Author Comment

by:teamdad
ID: 12312794
It gives the blue wavey line under Label1 and gives the error Handles clause requires a WithEvents variable
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12312814
Add a Label to your form.  =)

~IM
0
 

Author Comment

by:teamdad
ID: 12312826
Ok... now I know I must get some rest at night instead of trying to come up with code.   I had copied your full code and had double instances of

Public Class Form1
    Inherits System.Windows.Forms.Form

Works great.... lotta code for just a simple task but it does the trick I wanted.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12312881
If you don't want to duplicate that code for EVERY single label on your form then you can create a UserControl that derives from the base Label and place that code in it.

Then you can add that new Label control to your toolbox and drag/drop it onto your forms just like other controls.

Let me know if you want full instructions on how to do this.  (I haven't done it myself in awhile...)

~IM
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question