Advertisement
Guest User

DrawBorder extension - by Elektro

a guest
Jun 25th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.46 KB | None | 0 0
  1. Public NotInheritable Class Form1 : Inherits Form
  2.  
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Me.Button1.DrawBorder(Color.Red, 1, ButtonBorderStyle.Solid)
  5.     End Sub
  6.  
  7. End Class
  8.  
  9. Public Module ControlExtensions
  10.  
  11.     ''' <summary>
  12.     ''' Draws a solid border of the specified color with 1 pixel width around the bounds of the source <see cref="Control"/>.
  13.     ''' </summary>
  14.     <Extension>
  15.     Public Sub DrawBorder(ByVal sender As Control, ByVal color As Color)
  16.  
  17.         ControlExtensions.DrawBorder(sender, color, 1, ButtonBorderStyle.Solid, sender.ClientRectangle)
  18.  
  19.     End Sub
  20.  
  21.     ''' <summary>
  22.     ''' Draws a solid border of the specified color and with around the bounds of the source <see cref="Control"/>.
  23.     ''' </summary>
  24.     <Extension>
  25.     Public Sub DrawBorder(ByVal sender As Control, ByVal color As Color, ByVal width As Integer)
  26.  
  27.         ControlExtensions.DrawBorder(sender, color, width, ButtonBorderStyle.Solid, sender.ClientRectangle)
  28.  
  29.     End Sub
  30.  
  31.     ''' <summary>
  32.     ''' Draws a border of the specified color, with and style around the bounds of the source <see cref="Control"/>.
  33.     ''' </summary>
  34.     <Extension>
  35.     Public Sub DrawBorder(ByVal sender As Control, ByVal color As Color, ByVal width As Integer, ByVal style As ButtonBorderStyle)
  36.  
  37.         ControlExtensions.DrawBorder(sender, color, width, style, sender.ClientRectangle)
  38.  
  39.     End Sub
  40.  
  41.     ''' <summary>
  42.     ''' Draws a border of the specified color, with and style around the specified bounds of the source <see cref="Control"/>.
  43.     ''' </summary>
  44.     <Extension>
  45.     Public Sub DrawBorder(ByVal sender As Control, ByVal color As Color, ByVal width As Integer, ByVal style As ButtonBorderStyle, ByVal bounds As Rectangle)
  46.  
  47.         Select Case sender.GetType()
  48.  
  49.             ' You can add here a custom case for selective painting...
  50.             ' Case GetType(TextBox), GetType(RichTextBox) ' etc.
  51.  
  52.             Case Else
  53.                 Using g As Graphics = sender.CreateGraphics() ' or also: Graphics.FromHwnd(sender.Handle)
  54.  
  55.                     ControlPaint.DrawBorder(g, bounds,
  56.                                     color, width, style, ' left
  57.                                     color, width, style, ' top
  58.                                     color, width, style, ' right
  59.                                     color, width, style) ' bottom
  60.  
  61.                 End Using
  62.  
  63.         End Select
  64.  
  65.     End Sub
  66.  
  67. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement