?
Solved

How do I place a label behind a label

Posted on 2008-10-01
7
Medium Priority
?
221 Views
Last Modified: 2013-11-26
I want to place one label behind the other so that I will get a shadowed effect. In Access, I was able to make the background of a two labels transparent and I would place the blue text label in on top of a white text label to get a shadowed effect for a label. I am having difficulty getting the desired effect.

Can this be done on a form in VB.NET in Visual Studio 2008? Is there a way to simply get a shadowed effect to text in another way?

Thanks,
Lenny Gray
0
Comment
Question by:LennyGray
[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
  • 3
  • 3
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22618501
I think this requirement deserves a custom control, inherited from the regular Label control.
You have to override the OnPaint event.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22618751
You can do this in the Paint event of the label:
 

Private Sub Label1_Paint(...) Handles Label1.Paint
   e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Blue, 2, 2) ' you have to play with this values
End Sub

Open in new window

0
 
LVL 10

Author Comment

by:LennyGray
ID: 22620354
Can you give me sample code for the custom control?

I use this technique quite frequently in my Access applications and I'd like to start building a library of routines to call.

Your code would be both educational and functional.

Please assume nothing in your example because I am a VBA programmer with a lot of experience but I am very new to VB.NET. I will need a lot of "hand-holding" while I learn this language.

Thanks.


Lenny
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22620582
put the following code in a separated vb file, at your designer's toolbox a new custom control should appear, use it as a regular label
Imports System.Windows.Forms
 
Public Class ShadowLabel
    Inherits Label
 
    Private BackBrush As Brush
 
    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        BackBrush = New SolidBrush(Me.BackColor)
    End Sub
 
    Protected Overrides Sub OnBackColorChanged(ByVal e As System.EventArgs)
        MyBase.OnBackColorChanged(e)
        BackBrush.Dispose()
        BackBrush = New SolidBrush(Me.BackColor)
    End Sub
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim r As RectangleF = e.ClipRectangle
        Dim sf As New StringFormat
 
        Select Case Me.TextAlign
            Case ContentAlignment.BottomCenter
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Far
            Case ContentAlignment.BottomLeft
                sf.LineAlignment = StringAlignment.Near
                sf.Alignment = StringAlignment.Far
            Case ContentAlignment.BottomRight
                sf.LineAlignment = StringAlignment.Far
                sf.Alignment = StringAlignment.Far
            Case ContentAlignment.MiddleCenter
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Center
            Case ContentAlignment.MiddleLeft
                sf.LineAlignment = StringAlignment.Near
                sf.Alignment = StringAlignment.Center
            Case ContentAlignment.MiddleRight
                sf.LineAlignment = StringAlignment.Far
                sf.Alignment = StringAlignment.Center
            Case ContentAlignment.TopCenter
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Near
            Case ContentAlignment.TopLeft
                sf.LineAlignment = StringAlignment.Near
                sf.Alignment = StringAlignment.Near
            Case ContentAlignment.TopRight
                sf.LineAlignment = StringAlignment.Far
                sf.Alignment = StringAlignment.Near
        End Select
 
        e.Graphics.FillRectangle(Me.BackBrush, e.ClipRectangle)
        e.Graphics.DrawString(Me.Text, Me.Font, Brushes.White, r, sf)
        r.Offset(-1, -1)
        e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Blue, r, sf)
    End Sub
End Class

Open in new window

0
 
LVL 10

Author Comment

by:LennyGray
ID: 22627455
What do you mean by "put the following code in a separated vb file, at your designer's toolbox a new custom control should appear, use it as a regular label".

I put this into a class in my application but it did not show up in the toolbox, so I am doing something dumb.

Sorry for my lack of experience with this.

Lenny
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 22627624
save the snippet in a separated class file, lets say ShadowLabel.vb
Add this file to your project
If done well, when you are designing a form, the ShadowLabel control will appear inside the toolbox, at the Custom controls section
0
 
LVL 10

Author Closing Comment

by:LennyGray
ID: 31502138
Thanks...It worked!!

I did not know to add it to the project.

I have a long way to go.

Thanks again!

Lenny Gray
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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