none
draw a rectangle around a textbox RRS feed

Answers

  • Start a new Windows Forms Application and add a TextBox to Form1.
    Replace the Form1 code with:

    Public
     Class Form1
      
    Private Sub Form1_Load(ByVal sender As Object, _
                             
    ByVal e As EventArgs) Handles MyBase.Load
        TextBox1.Location = 
    New Point((Me.ClientSize.Width - TextBox1.Width) \ 2, _
                                      (
    Me.ClientSize.Height - TextBox1.Height) \ 2)
        
    AddHandler TextBox1.Parent.Paint, AddressOf DrawRedRectangleAroundTextBox
      
    End Sub
      Sub DrawRedRectangleAroundTextBox(ByVal sender As ObjectByVal e As PaintEventArgs)
        e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle)
      
    End Sub
    End
     Class

    • Marked as answer by DerStauner Thursday, May 19, 2011 7:28 AM
    Tuesday, May 17, 2011 6:29 PM

All replies

  • Hi,

    is this possible to draw a red rectangle about a text box? How?

      Private Sub Form1_Paint(ByVal sender As Object,
                  ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim r As Rectangle = Me.TextBox1.Bounds
        r.X -= 2
        r.Y -= 2
        r.Width += 3
        r.Height += 3
        e.Graphics.DrawRectangle(Pens.Red, r)
      End Sub
    
    


    Stephen J Whiteley
    Tuesday, May 17, 2011 6:11 PM
    Moderator
  • just update the tip (not the website)

    http://www.vb-tips.com/ControlBorder.aspx


    Success
    Cor
    Tuesday, May 17, 2011 6:15 PM
  • Hi

    If you need a thicker edged box then

      Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim b As Rectangle = Me.TextBox1.Bounds
        Dim x, y, w, h As Single
        For sp As Single = 4 To 8
          x = b.X - sp
          y = b.Y - sp
          w = sp + b.Width + sp
          h = sp + b.Height + sp
          Dim r As Rectangle = New Rectangle(x, y, w, h)
          e.Graphics.DrawRectangle(Pens.Red, r)
        Next
      End Sub
    
    


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Tuesday, May 17, 2011 6:27 PM
  • Start a new Windows Forms Application and add a TextBox to Form1.
    Replace the Form1 code with:

    Public
     Class Form1
      
    Private Sub Form1_Load(ByVal sender As Object, _
                             
    ByVal e As EventArgs) Handles MyBase.Load
        TextBox1.Location = 
    New Point((Me.ClientSize.Width - TextBox1.Width) \ 2, _
                                      (
    Me.ClientSize.Height - TextBox1.Height) \ 2)
        
    AddHandler TextBox1.Parent.Paint, AddressOf DrawRedRectangleAroundTextBox
      
    End Sub
      Sub DrawRedRectangleAroundTextBox(ByVal sender As ObjectByVal e As PaintEventArgs)
        e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle)
      
    End Sub
    End
     Class

    • Marked as answer by DerStauner Thursday, May 19, 2011 7:28 AM
    Tuesday, May 17, 2011 6:29 PM