Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Drawing an outline around a label

Posted on 2004-10-13
8
Medium Priority
?
271 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

705 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