Solved

Drawing an outline around a label

Posted on 2004-10-13
8
267 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
  • 5
  • 3
8 Comments
 
LVL 85

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 85

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 85

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 85

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 85

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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