Advertisement
Guest User

Control TextBox con "autoexplicacion"

a guest
Oct 23rd, 2010
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.00 KB | None | 0 0
  1. Public Class OllydbgTxtBOX
  2.  
  3.     Inherits TextBox
  4.  
  5.     Private oldFont As Font = Nothing
  6.     Private bTextoEnVacio As Boolean = False
  7.  
  8. #Region "Nuevo Atributos"
  9.     Private _TextoEnVacioColor As Color = Drawing.Color.Gray
  10.     <System.ComponentModel.Description("Color del texto cuando el 'TextBox' está vacio")> _
  11.     Public Property TextVacioColor() As Color
  12.         Get
  13.             Return _TextoEnVacioColor
  14.         End Get
  15.         Set(ByVal value As Color)
  16.             _TextoEnVacioColor = value
  17.             Me.Invalidate()
  18.         End Set
  19.     End Property
  20.  
  21.     Private _TextTextoVacio As String = "<Sin descripción>"
  22.  
  23.     <System.ComponentModel.Description("Texto o cuando el 'TextoBox' está vacio")> _
  24.     Public Property TextVacio() As String
  25.         Get
  26.             Return _TextTextoVacio
  27.         End Get
  28.         Set(ByVal value As String)
  29.             _TextTextoVacio = value
  30.             Me.Invalidate()
  31.         End Set
  32.     End Property
  33.  
  34.     Private _TextEnCursiva As Boolean = False
  35.     <System.ComponentModel.Description("El 'TextVacio' se escribirá en cursiva o no")> _
  36.     Public Property TextVacioEnCursiva() As Boolean
  37.         Get
  38.             Return _TextEnCursiva
  39.         End Get
  40.         Set(ByVal value As Boolean)
  41.             _TextEnCursiva = value
  42.             Me.Invalidate()
  43.         End Set
  44.     End Property
  45. #End Region
  46.  
  47.     ' Default constructor
  48.     Public Sub New()
  49.         JoinEvents(True)
  50.     End Sub
  51.  
  52.     ' Override OnCreateControl
  53.     Protected Overrides Sub OnCreateControl()
  54.         MyBase.OnCreateControl()
  55.         TextoVacio_SeleccionEv(Nothing, Nothing)
  56.     End Sub
  57.  
  58.     Private Sub JoinEvents(ByVal join As Boolean)
  59.         If join Then
  60.             AddHandler (TextChanged), AddressOf TextoVacio_SeleccionEv
  61.             AddHandler (LostFocus), AddressOf TextoVacio_SeleccionEv
  62.             AddHandler (FontChanged), AddressOf TextoVacio_FuenteEv
  63.  
  64.             'Los eventos de arriba no se incian inmediatamente
  65.             'Los llamamos cuando el control esta creado
  66.  
  67.             'Otra solucion alternativa: Usar un Timer para mirar las propiedades
  68.         End If
  69.     End Sub
  70.  
  71.     Private Sub TextoVacio_SeleccionEv(ByVal sender As Object, ByVal args As EventArgs)
  72.         If Me.Text.Length <= 0 Then
  73.             ActivarTextoVacio()
  74.         Else
  75.             DesactivarTextoVacio()
  76.         End If
  77.     End Sub
  78.  
  79.     Private Sub TextoVacio_FuenteEv(ByVal sender As Object, ByVal args As EventArgs)
  80.         If bTextoEnVacio Then
  81.             oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
  82.             Refresh()
  83.         End If
  84.     End Sub
  85.  
  86.     Private Sub ActivarTextoVacio()
  87.         'Fuente actual:
  88.         oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
  89.  
  90.         'Activar el OnPaint:
  91.         Me.SetStyle(ControlStyles.UserPaint, True)
  92.         Me.bTextoEnVacio = True
  93.  
  94.         'Pintar inmediatamente
  95.         Refresh()
  96.     End Sub
  97.  
  98.     Private Sub DesactivarTextoVacio()
  99.         'Desactivar el OnPaint
  100.         Me.bTextoEnVacio = False
  101.         Me.SetStyle(ControlStyles.UserPaint, False)
  102.  
  103.         If Not oldFont Is Nothing Then
  104.             Me.Font = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
  105.         End If
  106.     End Sub
  107.  
  108.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  109.  
  110.         Dim drawFont As Font
  111.  
  112.         If bTextoEnVacio = True Then
  113.             If _TextEnCursiva = False Then
  114.                 drawFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
  115.             Else
  116.                 drawFont = New Font(Font.FontFamily, Font.Size, FontStyle.Italic, Font.Unit)
  117.             End If
  118.         Else
  119.             drawFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
  120.         End If
  121.  
  122.  
  123.         Dim drawBrush As SolidBrush = New SolidBrush(Me.TextVacioColor)
  124.         e.Graphics.DrawString(IIf(bTextoEnVacio, TextVacio, Text).ToString(), drawFont, drawBrush, New Point(0, 0))
  125.  
  126.         MyBase.OnPaint(e)
  127.     End Sub
  128. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement