Solved

Drawing an outline around a label

Posted on 2004-10-13
8
265 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now