How do I place a label behind a label

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
LVL 10
LennyGrayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
I think this requirement deserves a custom control, inherited from the regular Label control.
You have to override the OnPaint event.
0
Jorge PaulinoIT Pro/DeveloperCommented:
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
LennyGrayAuthor Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Jaime OlivaresSoftware ArchitectCommented:
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
LennyGrayAuthor Commented:
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
Jaime OlivaresSoftware ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LennyGrayAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.