• Jueves 30 de Mayo de 2024, 02:56

Autor Tema:  Problemas con mi Textbox Personalizado.... evento Keyup  (Leído 1579 veces)

tux2003

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Problemas con mi Textbox Personalizado.... evento Keyup
« en: Jueves 20 de Mayo de 2010, 17:53 »
0
Hola Comunidad:

Estoy haciendo un textbox personalizado simulando los textbox que podemos crear con CSS poniendolo un color de contorno a nuestro gusto y ancho del mismo. En fin ya logre hacerlo chambea bien pero ahora tengo un problema que siento no es muy complicado de hacer pero estoy iniciando en .Net y me falta muchisimo por aprender. Quiero que los eventos de mi textbox respondan adecuadamente para los eventos keyup, keydown o keypress ya que no responden cuando los inserto en un form.

Les dejo mi codigo pruebenlo y ojala puedan asesorarme saludos.

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Namespace MiDSINCTextbox
Public Class clsDSINCTextbox
Inherits UserControl

#Region "Variables"
Dim colBorde As Color = Color.SteelBlue
Private Components As IContainer
Private intBorderSize As Integer = 1
Private intLeftpad As Integer = 2
Private intToppad As Integer = 2
Private WithEvents picBoth As New PictureBox()
Private WithEvents picLeft As New PictureBox()
Private WithEvents picRight As New PictureBox()
Private WithEvents picTop As New PictureBox()
Private WithEvents txtTextbox As New TextBox()
#End Region

#Region "Constructor"
Public Sub New()
MyBase.New()
IniciaComponentes()
colBorde = Color.DeepSkyBlue
End Sub
#End Region

#Region "Funciones"
Private Sub IniciaComponentes()
Me.SuspendLayout()
'Establecemos los parametros del textbox del control
With Me.txtTextbox
'Indicamos el tipo de borde
.BorderStyle = Windows.Forms.BorderStyle.None
'El tipo de fuente y estilo
.Font = New Font("Tahoma", 8.25!, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
'Establecemos una ubicacion
.Location = New Point(8, 5)
'La longitud máxima
.MaxLength = 0
'El nombre
.Name = "TextBox1"
'Un tamaño
.Size = New Size(176, 14)
'Texto por default
.Text = "TextBox"
End With
'Establecemos los parametros de la linea que dibuaremos del lado izquierdo
With Me.picLeft
'Color de fondo
.BackColor = colBorde ' Color.DeepSkyBlue
.Dock = DockStyle.Left
'establecemos una ubicacion
.Location = New Point(0, 1)
'el nombre
.Name = "picLeft"
'un tamaño
.Size = New Size(1, 22)
.TabStop = False
End With
'Establecemos los parametros de la linea que dibuaremos del lado derecho
With Me.picRight
'Establecemos el color de fondo
.BackColor = colBorde 'Color.DeepSkyBlue
.Dock = DockStyle.Right
'una ubicacion
.Location = New Point(191, 1)
'el nombre
.Name = "picRight"
'un tamaño
.Size = New Size(1, 22)
.TabStop = False
End With
'Establecemos los parametros de la linea que dibuaremos en la parte superior
With Me.picTop
'establecemos el color de fondo
.BackColor = colBorde 'Color.DodgerBlue
.Dock = DockStyle.Top
'el nombre
.Name = "picTop"
'un tamaño
.Size = New Size(192, 1)
.TabStop = False
End With
'Establecemos los parametros de la linea que dibuaremos en la parte inferior
With Me.picBoth
'establecemos el color de fondo
.BackColor = colBorde 'Color.DodgerBlue
.Dock = DockStyle.Bottom
'una ubicacion
.Location = New Point(0, 23)
'un nombre
.Name = "picBoth"
'un tamaño
.Size = New Size(192, 1)
.TabStop = False
End With
With Me
'establecemos el color de fondo
.BackColor = Color.White
.Controls.AddRange(New Control() {Me.txtTextbox, Me.picRight, Me.picLeft, Me.picBoth, Me.picTop})
'el nombre del control
.Name = "TextBox"
'un tamaño
.Size = New Size(192, 24)
.ResumeLayout(False)
End With
End Sub
#End Region

#Region "Métodos"
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (Components Is Nothing) Then Components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub

Private Sub TextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
picTop.Width = Me.Width
picLeft.Height = Me.Height
picRight.Height = Me.Height
picBoth.Width = Me.Width

txtTextbox.Location = New Point(intLeftpad, intToppad)
txtTextbox.Width = Me.Width - (intBorderSize * 2) - intLeftpad
txtTextbox.Height = Me.Height - (intBorderSize * 2) - intToppad
End Sub

Private Sub txtTextbox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTextbox.KeyUp
If e.KeyData = Keys.Enter Then MsgBox("Chingon")
End Sub
#End Region

#Region "Propiedades"
<Category("CustomProperties"), Description("Establece el color de fondo del control")>
Public Property BackGroudColor() As Color
Get
Return Me.BackColor
End Get
Set(ByVal value As Color)
Me.BackColor = value
txtTextbox.BackColor = value
End Set
End Property

<Category("CustomProperties"), Description("Establece el color de borde del control")>
Public Property BorderColor() As Color
Get
Return colBorde
End Get
Set(ByVal value As Color)
colBorde = value
picBoth.BackColor = value
picLeft.BackColor = value
picRight.BackColor = value
picTop.BackColor = value
End Set
End Property

<Category("CustomProperties"), Description("Establece el tamaño del borde del control")>
Public Property BorderSize() As Integer
Get
Return intBorderSize
End Get
Set(ByVal value As Integer)
intBorderSize = value
Me.Refresh()
End Set
End Property

'JE Propiedad bastante logica no XD
Public NotOverridable Overrides Property Font() As Font
Get
Return txtTextbox.Font
End Get
Set(ByVal Value As Font)
txtTextbox.Font = Value
Me.Refresh()
End Set
End Property

<Category("CustomProperties"), Description("Establece el color de la fuente del control")>
Public Overrides Property ForeColor() As Color
Get
Return txtTextbox.ForeColor
End Get
Set(ByVal value As Color)
txtTextbox.ForeColor = value
End Set
End Property

<Category("CustomProperties"), Description("Indica si el control sera multilinea")>
Public Property Multiline() As Boolean
Get
Return txtTextbox.Multiline
End Get
Set(ByVal value As Boolean)
txtTextbox.Multiline = value
Me.Refresh()
End Set
End Property

Public Property PaddingLeft() As Integer
Get
Return intLeftpad
End Get

Set(ByVal Value As Integer)
intLeftpad = Value
Me.Refresh()
End Set
End Property

Public Property PaddingTop() As Integer
Get
Return intToppad
End Get

Set(ByVal Value As Integer)
intToppad = Value
End Set
End Property

<Category("CustomProperties"), Description("Establece el caracter a utilizar para encriptar un password")>
Public Property PasswordChar() As String
Get
Return txtTextbox.PasswordChar
End Get
Set(ByVal value As String)
txtTextbox.PasswordChar = value
Me.Refresh()
End Set
End Property

<Category("CustomProperties"), Description("Indica si el control tendra scrolls y que tipo sera")>
Public Property ScrollBars() As ScrollBars
Get
Return txtTextbox.ScrollBars
End Get
Set(ByVal value As ScrollBars)
txtTextbox.ScrollBars = value
End Set
End Property

<Category("CustomProperties"), Description("Establece la alineacion del texto del control")>
Public Property TextAlign() As HorizontalAlignment
Get
Return txtTextbox.TextAlign
End Get
Set(ByVal value As HorizontalAlignment)
txtTextbox.TextAlign = value
Me.Refresh()
End Set
End Property

<Category("CustomProperties"), Description("Establece el texto que sera mostrado en el control")>
Public Property TextControl As String
Get
Return txtTextbox.Text
End Get
Set(ByVal Value As String)
txtTextbox.Text = Value
End Set
End Property

Public Property WordWrap() As Boolean
Get
Return txtTextbox.WordWrap
End Get
Set(ByVal value As Boolean)
txtTextbox.WordWrap = value
Me.Refresh()
End Set
End Property
#End Region
End Class
End Namespace

tux2003

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Re: Problemas con mi Textbox Personalizado.... evento Keyup
« Respuesta #1 en: Lunes 24 de Mayo de 2010, 16:42 »
0
Yo se que muchos de los programadores que empiezan tienen dudas al igual que yo y todos vamos aprendiendo y adquirimos experiencia con el paso del tiempo. No me gusta ser egoista ni nada asi que quiero compartir la solucion con ustedes solo faltaba anexar lo siguiente:

        Private Sub txtTextbox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTextbox.KeyDown
            MyBase.OnKeyDown(e)
        End Sub

        Private Sub txtTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTextbox.KeyPress
            MyBase.OnKeyPress(e)
        End Sub

        Private Sub txtTextbox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTextbox.KeyUp
            MyBase.OnKeyUp(e)
        End Sub

explico.... como estoy heredando directamente de UserControl y ocupo un textbox en mi control los eventos y/ó acciones se ejecutaban ahi de modo que teniamos que pasar los eventos y/o acciones a la clase base en este caso UserControl.

El codigo del control quedó así

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Namespace MiDSINCTextbox
    Public Class clsDSINCTextbox
        Inherits UserControl

#Region "Variables"
        Dim colBorde As Color = Color.SteelBlue
        Private Components As IContainer
        Private intBorderSize As Integer = 1
        Private intLeftpad As Integer = 2
        Private intToppad As Integer = 2
        Private WithEvents picBoth As New PictureBox()
        Private WithEvents picLeft As New PictureBox()
        Private WithEvents picRight As New PictureBox()
        Private WithEvents picTop As New PictureBox()
        Private WithEvents txtTextbox As New TextBox()
#End Region

#Region "Constructor"
        Public Sub New()
            MyBase.New()
            IniciaComponentes()
            colBorde = Color.DeepSkyBlue
        End Sub
#End Region

#Region "Funciones"
        Private Sub IniciaComponentes()
            Me.SuspendLayout()
            'Establecemos los parametros del textbox del control
            With Me.txtTextbox
                'Indicamos el tipo de borde
                .BorderStyle = Windows.Forms.BorderStyle.None
                'El tipo de fuente y estilo
                .Font = New Font("Tahoma", 8.25!, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
                'Establecemos una ubicacion
                .Location = New Point(8, 5)
                'La longitud máxima
                .MaxLength = 0
                'El nombre
                .Name = "TextBox1"
                'Un tamaño
                .Size = New Size(176, 14)
                'Texto por default
                .Text = "TextBox"
            End With
            'Establecemos los parametros de la linea que dibuaremos del lado izquierdo
            With Me.picLeft
                'Color de fondo
                .BackColor = colBorde ' Color.DeepSkyBlue
                .Dock = DockStyle.Left
                'establecemos una ubicacion
                .Location = New Point(0, 1)
                'el nombre
                .Name = "picLeft"
                'un tamaño
                .Size = New Size(1, 22)
                .TabStop = False
            End With
            'Establecemos los parametros de la linea que dibuaremos del lado izquierdo
            With Me.picRight
                'Establecemos el color de fondo
                .BackColor = colBorde 'Color.DeepSkyBlue
                .Dock = DockStyle.Right
                'una ubicacion
                .Location = New Point(191, 1)
                'el nombre
                .Name = "picRight"
                'un tamaño
                .Size = New Size(1, 22)
                .TabStop = False
            End With
            'Establecemos los parametros de la linea que dibuaremos en la parte superior
            With Me.picTop
                'establecemos el color de fondo
                .BackColor = colBorde 'Color.DodgerBlue
                .Dock = DockStyle.Top
                'el nombre
                .Name = "picTop"
                'un tamaño
                .Size = New Size(192, 1)
                .TabStop = False
            End With
            'Establecemos los parametros de la linea que dibuaremos en la parte inferior
            With Me.picBoth
                'establecemos el color de fondo
                .BackColor = colBorde 'Color.DodgerBlue
                .Dock = DockStyle.Bottom
                'una ubicacion
                .Location = New Point(0, 23)
                'un nombre
                .Name = "picBoth"
                'un tamaño
                .Size = New Size(192, 1)
                .TabStop = False
            End With
            With Me
                'establecemos el color de fondo
                .BackColor = Color.White
                .Controls.AddRange(New Control() {Me.txtTextbox, Me.picRight, Me.picLeft, Me.picBoth, Me.picTop})
                'el nombre del control
                .Name = "TextBox"
                'un tamaño
                .Size = New Size(192, 24)
                .ResumeLayout(False)
            End With
        End Sub
#End Region

#Region "Métodos"
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (Components Is Nothing) Then Components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub TextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            picTop.Width = Me.Width
            picLeft.Height = Me.Height
            picRight.Height = Me.Height
            picBoth.Width = Me.Width

            txtTextbox.Location = New Point(intLeftpad, intToppad)
            txtTextbox.Width = Me.Width - (intBorderSize * 2) - intLeftpad
            txtTextbox.Height = Me.Height - (intBorderSize * 2) - intToppad
        End Sub

        Private Sub txtTextbox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTextbox.KeyDown
            MyBase.OnKeyDown(e)
        End Sub

        Private Sub txtTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTextbox.KeyPress
            MyBase.OnKeyPress(e)
        End Sub

        Private Sub txtTextbox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTextbox.KeyUp
            MyBase.OnKeyUp(e)
        End Sub
#End Region

#Region "Propiedades"
        <Category("CustomProperties"), Description("Establece el color de fondo del control")>
        Public Property BackGroudColor() As Color
            Get
                Return Me.BackColor
            End Get
            Set(ByVal value As Color)
                Me.BackColor = value
                txtTextbox.BackColor = value
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece el color de borde del control")>
        Public Property BorderColor() As Color
            Get
                Return colBorde
            End Get
            Set(ByVal value As Color)
                colBorde = value
                picBoth.BackColor = value
                picLeft.BackColor = value
                picRight.BackColor = value
                picTop.BackColor = value
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece el tamaño del borde del control")>
        Public Property BorderSize() As Integer
            Get
                Return intBorderSize
            End Get
            Set(ByVal value As Integer)
                intBorderSize = value
                Me.Refresh()
            End Set
        End Property

        'JE Propiedad bastante logica no XD
        Public NotOverridable Overrides Property Font() As Font
            Get
                Return txtTextbox.Font
            End Get
            Set(ByVal Value As Font)
                txtTextbox.Font = Value
                Me.Refresh()
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece el color de la fuente del control")>
        Public Overrides Property ForeColor() As Color
            Get
                Return txtTextbox.ForeColor
            End Get
            Set(ByVal value As Color)
                txtTextbox.ForeColor = value
            End Set
        End Property

        <Category("CustomProperties"), Description("Indica si el control sera multilinea")>
        Public Property Multiline() As Boolean
            Get
                Return txtTextbox.Multiline
            End Get
            Set(ByVal value As Boolean)
                txtTextbox.Multiline = value
                Me.Refresh()
            End Set
        End Property

        Public Property PaddingLeft() As Integer
            Get
                Return intLeftpad
            End Get

            Set(ByVal Value As Integer)
                intLeftpad = Value
                Me.Refresh()
            End Set
        End Property

        Public Property PaddingTop() As Integer
            Get
                Return intToppad
            End Get

            Set(ByVal Value As Integer)
                intToppad = Value
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece el caracter a utilizar para encriptar un password")>
        Public Property PasswordChar() As String
            Get
                Return txtTextbox.PasswordChar
            End Get
            Set(ByVal value As String)
                txtTextbox.PasswordChar = value
                Me.Refresh()
            End Set
        End Property

        <Category("CustomProperties"), Description("Indica si el control tendra scrolls y que tipo sera")>
        Public Property ScrollBars() As ScrollBars
            Get
                Return txtTextbox.ScrollBars
            End Get
            Set(ByVal value As ScrollBars)
                txtTextbox.ScrollBars = value
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece la alineacion del texto del control")>
        Public Property TextAlign() As HorizontalAlignment
            Get
                Return txtTextbox.TextAlign
            End Get
            Set(ByVal value As HorizontalAlignment)
                txtTextbox.TextAlign = value
                Me.Refresh()
            End Set
        End Property

        <Category("CustomProperties"), Description("Establece el texto que sera mostrado en el control")>
        Public Property TextControl As String
            Get
                Return txtTextbox.Text
            End Get
            Set(ByVal Value As String)
                txtTextbox.Text = Value
            End Set
        End Property

        Public Property WordWrap() As Boolean
            Get
                Return txtTextbox.WordWrap
            End Get
            Set(ByVal value As Boolean)
                txtTextbox.WordWrap = value
                Me.Refresh()
            End Set
        End Property
#End Region
    End Class
End Namespace

El código esta programado en VS .Net 2010 y si quieren algo más pueden anexarlo sin problema alguno solo agradecería no me roben el crédito :D saludos y suerte.