-1

I have a label with text "UserName" in foreground color as white and the form is on a sky-blue color. I would like to add a black border NOT to the label itself, but to the text inside of it.

Is this possible?

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnLogin.Enabled = False
        centrarVentana(Me)
        lblNombreUsuario.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
End Sub

The code obviously is just for show,since it does not do what I want.

Edit FINAL : Thanks so much everybody. It finally worked! I am leaving the code here so everybody can re use. Once you understand it its actually really easy.

Imports System.Drawing.Drawing2D

Public Class BorderLabel
    Inherits Label
    Public outline_color As Color = Color.Black
    Public border_thickness As Integer = 5
    Public Sub New()
        MyBase.New

    End Sub


    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        e.Graphics.FillRectangle(New SolidBrush(BackColor), ClientRectangle)
        Dim gp As GraphicsPath = New GraphicsPath
        Dim outline As Pen = New Pen(Me.outline_color, Me.border_thickness)
        Dim sf As StringFormat = New StringFormat
        Dim foreBrush As Brush = New SolidBrush(ForeColor)
        gp.AddString(Text, Font.FontFamily, CType(Font.Style, Integer), Font.Size, ClientRectangle, sf)
        e.Graphics.ScaleTransform(1.3!, 1.35!)
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality
        e.Graphics.DrawPath(outline, gp)
        e.Graphics.FillPath(foreBrush, gp)
    End Sub
End Class

enter image description here

NOTE : This question is not an exact duplicate of Setting a Font with outline Color in C# since I am on Visual Basic and had to do code changes for this to work correctly.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
  • https://stackoverflow.com/questions/19842722/setting-a-font-with-outline-color-in-c-sharp – Hans Passant Apr 05 '18 at 16:30
  • @HansPassant thanks Pasant. Your link is what I need. I am following the steps to do it. And I can see the customlabel in the toolbox but when I click on it to add it to the form, it fails saying that "it failed to add the class and hence it is going to be removed from the toolbox" . – Matias Barrios Apr 05 '18 at 16:58
  • Did you try building your solution first (`CTRL + SHIFT + B`), then adding it? Also did you add it by using a C# DLL or by converting the code to VB.NET? – Visual Vincent Apr 05 '18 at 17:13
  • @VisualVincent I modified the code to suit VB syntaxis and imports. Ill try to build it now. Also Ill add the code I modified. – Matias Barrios Apr 05 '18 at 17:23
  • While it is unrelated to your current issue, the properties in that code are not bound to anything, so they won't work. Remove everything from `Get` up to (and including) `End Property`, thus just keeping the `Public Property ...` lines. – Visual Vincent Apr 05 '18 at 17:29
  • Thanks so much buddy! It finally worked! @VisualVincent – Matias Barrios Apr 05 '18 at 17:46
  • 1
    Glad I could help! You should post your solution as an answer rather than in your question, or accept the duplicate so that the question gets closed. – Visual Vincent Apr 05 '18 at 19:52
  • 1
    Possible duplicate of [Setting a Font with outline Color in C#](https://stackoverflow.com/questions/19842722/setting-a-font-with-outline-color-in-c-sharp) – Visual Vincent Apr 05 '18 at 19:52

1 Answers1

0

Take a look at KeithS answer

also you can create your own label:

    Public Class CustomLabel
    Inherits Label

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid)
    End Sub
End Class

And here's how you implement it

Dim newForm As Form = New Form
Dim newLabel As CustomLabel = New CustomLabel
newForm.Controls.Add(newLabel)
newLabel.BackColor = Color.Black
newLabel.Font = New System.Drawing.Font("Microsoft Arial", 18!, FontStyle.Regular, GraphicsUnit.Point, CType(0,Byte))
newLabel.ForeColor = Color.Crimson
newLabel.Text = "Some text on a topmost transparent form window"
newForm.Show
newForm.TopMost = true
newLabel.AutoSize = true
newLabel.Location = New Point(230, 375)
  • I can see he is suggesting an idea of putting one label above the other. Seems really nasty but it might work for what I want. regarding the code you commented here, this will only add a rectangular border around the label, not a border in the text itself. Am I right? – Matias Barrios Apr 05 '18 at 16:32
  • You get the idea with the label over the label. I think I have achieved what you are looking for. Tell me if i'm right: [here](https://prnt.sc/j1deb1) – Mauricio Narvaez Apr 05 '18 at 17:55
  • Yeah, it will work, but the above solutions are better since they are not so ugly and unreliable. What happens if I resize the window? Would the relative positions remain the same as to produce the same effect? Take a look to the solution I ended up using. Its way better, thanks anyway buddy! – Matias Barrios Apr 05 '18 at 19:49
  • 1
    Yeah, I realize that you pick another clean and faster solution when I was looking for the one that I show in the picture. But anyways, im glad you found it. Keep it coding! – Mauricio Narvaez Apr 06 '18 at 15:36