Solved

Drawing an outline around a label

Posted on 2004-10-13
8
264 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

11 Experts available now in Live!

Get 1:1 Help Now