none
Draw border around text in textbox in vb.net RRS feed

  • Question

  • I would like to draw border around text in text box. I tray with borderstayle but this draw border around TextBox not around text is this possible?

    BR

    Friday, February 14, 2014 9:53 AM

Answers

  • Hi, I think the best way is create a custom control from textbox, see below:

    Public Class MyTextBox : Inherits TextBox
    
        Public Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
    
            Dim size = e.Graphics.MeasureString(Text, Font)
            Using pen As New Pen(SystemColors.ControlText)
                e.Graphics.DrawRectangle(pen, 0, (ClientSize.Height - size.Height) / 2, size.Width, size.Height)
            End Using
            Using brush As New SolidBrush(SystemColors.ControlText)
                e.Graphics.DrawString(Text, Font, brush, 0, (ClientSize.Height - size.Height) / 2)
            End Using
    
            MyBase.OnPaint(e)
        End Sub
    
    End Class

    • Proposed as answer by Shridhar J Joshi Friday, February 14, 2014 10:28 AM
    • Marked as answer by Opassi Friday, February 14, 2014 3:38 PM
    Friday, February 14, 2014 10:23 AM
  • if you create your own inherited control you can place the control yourself in code like

    Public Class Form1
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim mtb As New mytb
            mtb.Text = "Whatever"
            mtb.Left = 100
            mtb.Top = 100
            Me.Controls.Add(mtb)
    
        End Sub     
    
     
    End Class
    
    Public Class mytb
        Inherits TextBox
    
        Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Using p As Pen = New Pen(Brushes.Red)
                e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle)
                e.Graphics.DrawRectangle(p, 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
                e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle)
            End Using
        End Sub
    
    End Class


    • Edited by Rbie Friday, February 14, 2014 2:55 PM
    • Marked as answer by Opassi Friday, February 14, 2014 3:38 PM
    Friday, February 14, 2014 1:37 PM

All replies

  • Hi, I think the best way is create a custom control from textbox, see below:

    Public Class MyTextBox : Inherits TextBox
    
        Public Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
    
            Dim size = e.Graphics.MeasureString(Text, Font)
            Using pen As New Pen(SystemColors.ControlText)
                e.Graphics.DrawRectangle(pen, 0, (ClientSize.Height - size.Height) / 2, size.Width, size.Height)
            End Using
            Using brush As New SolidBrush(SystemColors.ControlText)
                e.Graphics.DrawString(Text, Font, brush, 0, (ClientSize.Height - size.Height) / 2)
            End Using
    
            MyBase.OnPaint(e)
        End Sub
    
    End Class

    • Proposed as answer by Shridhar J Joshi Friday, February 14, 2014 10:28 AM
    • Marked as answer by Opassi Friday, February 14, 2014 3:38 PM
    Friday, February 14, 2014 10:23 AM
  • This is greate but I still have problems I do that

    Public Class Form1

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = "Test"
        End Sub

    End Class

    Public Class MyTextBox : Inherits TextBox

        Public Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub

        Protected Overrides Sub OnPaint(e As PaintEventArgs)

            Dim size = e.Graphics.MeasureString(Text, Font)
            Using pen As New Pen(SystemColors.ControlText)
                e.Graphics.DrawRectangle(pen, 0, (ClientSize.Height - size.Height) / 2, size.Width, size.Height)
            End Using
            Using brush As New SolidBrush(SystemColors.ControlText)
                e.Graphics.DrawString(Text, Font, brush, 0, (ClientSize.Height - size.Height) / 2)
            End Using

            MyBase.OnPaint(e)
        End Sub

    End Class

    Friday, February 14, 2014 1:00 PM
  • Hi, I think the best way is create a custom control from textbox, see below:

    Public Class MyTextBox : Inherits TextBox
    
        Public Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
    
            Dim size = e.Graphics.MeasureString(Text, Font)
            Using pen As New Pen(SystemColors.ControlText)
                e.Graphics.DrawRectangle(pen, 0, (ClientSize.Height - size.Height) / 2, size.Width, size.Height)
            End Using
            Using brush As New SolidBrush(SystemColors.ControlText)
                e.Graphics.DrawString(Text, Font, brush, 0, (ClientSize.Height - size.Height) / 2)
            End Using
    
            MyBase.OnPaint(e)
        End Sub
    
    End Class


    Friday, February 14, 2014 1:01 PM
  • if you create your own inherited control you can place the control yourself in code like

    Public Class Form1
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim mtb As New mytb
            mtb.Text = "Whatever"
            mtb.Left = 100
            mtb.Top = 100
            Me.Controls.Add(mtb)
    
        End Sub     
    
     
    End Class
    
    Public Class mytb
        Inherits TextBox
    
        Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Using p As Pen = New Pen(Brushes.Red)
                e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle)
                e.Graphics.DrawRectangle(p, 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
                e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle)
            End Using
        End Sub
    
    End Class


    • Edited by Rbie Friday, February 14, 2014 2:55 PM
    • Marked as answer by Opassi Friday, February 14, 2014 3:38 PM
    Friday, February 14, 2014 1:37 PM
  • Hi Opassi,

    Please add a new class MyTextBox to your project, copy my code to the MyTextBox.vb, see below:

    Rebuild your project, and you will see MyTextBox control in the ToolBox, drag the control to your form, you will see it:

    • Proposed as answer by Rbie Friday, February 14, 2014 3:00 PM
    Friday, February 14, 2014 2:33 PM