自定义控件

我现在正在研究自定义控件和组件的问题。包括全新设计,扩展,组合式控件。我将陆续写出来和大家共同进步.
如下:是对TextBox控件的扩展的代码.
Imports System.ComponentModel

Public Class TextBoxExtended
    
Inherits TextBox

    
'Private variables
    Private blnAllowCharacters As Boolean = True
    
Private blnAllowNumbers As Boolean = True
    
Private blnAllowSpecialCharacters As Boolean = True
    
Private blnSpaceEntered As Boolean = False
    
Private blnPeriodEntered As Boolean = False

    
Private intMinLength As Integer = 0
    
Private intCharacterCasing As Integer = 0

    
Private chrSpecialCharacterList() As Char
    
Private chrCharacter As Char

    
Private TextBoxToolTip As New ToolTip

    
<Category("Behavior"), _
    Description(
"Indicates whether or not the control accepts character data."), _
    DefaultValue(
True)> _
    
Public Property AllowCharacters() As Boolean
        
Get
            
Return blnAllowCharacters
        
End Get
        
Set(ByVal value As Boolean)
            blnAllowCharacters 
= value
        
End Set
    
End Property


    
<Category("Behavior"), _
    Description(
"Indicates whether or not the control accepts numeric data."), _
    DefaultValue(
True)> _
    
Public Property AllowNumbers() As Boolean
        
Get
            
Return blnAllowNumbers
        
End Get
        
Set(ByVal value As Boolean)
            blnAllowNumbers 
= value
        
End Set
    
End Property


    
<Category("Behavior"), _
    Description(
"Indicates whether or not the control accepts special " & _
    
"characters. If False, the SpecialCharacterList property must specify " & _
    
"which special characters to exclude."), _
    DefaultValue(
True)> _
    
Public Property AllowSpecialCharacters() As Boolean
        
Get
            
Return blnAllowSpecialCharacters
        
End Get
        
Set(ByVal value As Boolean)
            blnAllowSpecialCharacters 
= value
        
End Set
    
End Property


    
<Category("Behavior"), _
    Description(
"A list of special characters to exclude when the " & _
    
"AllowSpecialCharacter property is set to False."), _
    DefaultValue(
"")> _
    
Public Property SpecialCharacterList() As Char()
        
Get
            
Return chrSpecialCharacterList
        
End Get
        
Set(ByVal value As Char())
            chrSpecialCharacterList 
= value
        
End Set
    
End Property


    
<Category("Behavior"), _
    Description(
"Specifies the minimum number of characters that should " & _
    
"be entered into the edit control."), _
    DefaultValue(
0)> _
    
Public Property MinLength() As Integer
        
Get
            
Return intMinLength
        
End Get
        
Set(ByVal value As Integer)
            intMinLength 
= value
        
End Set
    
End Property


    
Public Enum OverloadsCharacterCasing
        Normal 
= 0
        Upper 
= 1
        Lower 
= 2
        Proper 
= 3
        Sentence 
= 4
    
End Enum


    
<Category("Behavior"), _
    Description(
"Indicates if all characters should left alone or " & _
    
"converted to another case."), _
    DefaultValue(OverloadsCharacterCasing.Normal)
> _
    
Public Overloads Property CharacterCasing() As OverloadsCharacterCasing
        
Get
            
Return intCharacterCasing
        
End Get
        
Set(ByVal value As OverloadsCharacterCasing)
            
Select Case value
                
Case OverloadsCharacterCasing.Normal
                    
MyBase.CharacterCasing = Windows.Forms.CharacterCasing.Normal
                
Case OverloadsCharacterCasing.Upper
                    
MyBase.CharacterCasing = Windows.Forms.CharacterCasing.Upper
                
Case OverloadsCharacterCasing.Lower
                    
MyBase.CharacterCasing = Windows.Forms.CharacterCasing.Lower
            
End Select
            intCharacterCasing 
= value
        
End Set
    
End Property


    
<Category("Misc"), _
    Description(
"Determines the ToolTip shown when the mouse " & _
    
"hovers over the control."), _
    DefaultValue(
"")> _
    
Public Property ToolTip() As String
        
Get
            
Return TextBoxToolTip.GetToolTip(Me)
        
End Get
        
Set(ByVal value As String)
            TextBoxToolTip.SetToolTip(
Me, value)
        
End Set
    
End Property


    
<Category("Misc"), _
    Description(
"Determines the icon that is shown on the ToolTip."), _
    DefaultValue(ToolTipIcon.None)
> _
    
Public Property ToolTipIcon() As ToolTipIcon
        
Get
            
Return TextBoxToolTip.ToolTipIcon
        
End Get
        
Set(ByVal value As ToolTipIcon)
            TextBoxToolTip.ToolTipIcon 
= value
        
End Set
    
End Property


    
<Category("Misc"), _
    Description(
"Indicates whether the ToolTip will take on a balloon form."), _
    DefaultValue(
False)> _
    
Public Property ToolTipIsBalloon() As Boolean
        
Get
            
Return TextBoxToolTip.IsBalloon
        
End Get
        
Set(ByVal value As Boolean)
            TextBoxToolTip.IsBalloon 
= value
        
End Set
    
End Property


    
<Category("Misc"), _
    Description(
"Determines the title of the ToolTip."), _
    DefaultValue(
"")> _
    
Public Property ToolTipTitle() As String
        
Get
            
Return TextBoxToolTip.ToolTipTitle
        
End Get
        
Set(ByVal value As String)
            TextBoxToolTip.ToolTipTitle 
= value
        
End Set
    
End Property


    
Private Sub TextBoxExtended_KeyPress(ByVal sender As Object, _
        
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        
'If not allowing characters
        If Not blnAllowCharacters Then
            
'Check the key character entered
            If Char.IsLetter(e.KeyChar) Then
                
'Stop the character from being entered
                e.Handled = True
            
End If
        
End If

        
'If not allowing numbers
        If Not blnAllowNumbers Then
            
'Check the key character entered
            If Char.IsNumber(e.KeyChar) Or Char.IsDigit(e.KeyChar) Then
                
'Stop the character from being entered
                e.Handled = True
            
End If
        
End If

        
'If not allowing special characters
        If Not blnAllowSpecialCharacters Then
            
'Loop through the special character list
            For Each chrCharacter In chrSpecialCharacterList
                
If chrCharacter = e.KeyChar Then
                    
'Stop the character from being entered
                    e.Handled = True
                    
'Exit the loop as a match was found
                    Exit For
                
End If
            
Next
        
End If

        
'Set the case of the character
        Select Case intCharacterCasing
            
Case OverloadsCharacterCasing.Normal
                
'Do nothing
            Case OverloadsCharacterCasing.Upper
                
'Do nothing - let the base class handle it
            Case OverloadsCharacterCasing.Lower
                
'Do nothing - let the base class handle it
            Case OverloadsCharacterCasing.Proper
                
If blnSpaceEntered Or Me.Text.Length = 0 Then
                    
'Add the character to the Text property
                    Me.Text &= Char.ToUpper(e.KeyChar)
                    
'Set the Handled property to True to 
                    'prevent the operating system from processing it
                    e.Handled = True
                    
'Reposition the cursor back to the end of the text
                    Me.SelectionStart = Me.Text.Length
                
End If
            
Case OverloadsCharacterCasing.Sentence
                
'If this is the first character
                If Me.Text.Length = 0 Or _
                    (blnPeriodEntered 
And blnSpaceEntered) Then
                    
'Add the character to the Text property
                    Me.Text &= Char.ToUpper(e.KeyChar)
                    
'Set the Handled property to True to 
                    'prevent the operating system from processing it                    
                    e.Handled = True
                    
'Reposition the cursor back to the end of the text
                    Me.SelectionStart = Me.Text.Length
                    
'Turn off the period flag
                    blnPeriodEntered = False
                
End If
        
End Select

        
'Track the space key
        If Char.IsWhiteSpace(e.KeyChar) Then
            blnSpaceEntered 
= True
        
Else
            blnSpaceEntered 
= False
        
End If

        
'Track the period key
        If e.KeyChar.CompareTo("."c) = 0 Then
            blnPeriodEntered 
= True
        
End If
    
End Sub


    
Private Sub TextBoxExtended_KeyUp(ByVal sender As Object, _
        
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

        
'Fix CTRL+A shortcut key to select all text
        If e.Control And e.KeyCode = Keys.A Then
            
Me.SelectionStart = 0
            
Me.SelectionLength = Me.Text.Length
        
End If
    
End Sub


    
Private Sub TextBoxExtended_Validating(ByVal sender As Object, _
        
ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating

        
'Validate the minimum length
        If intMinLength > 0 Then
            
If Me.Text.Length < intMinLength Then
                e.Cancel 
= True
                MessageBox.Show(
"This TextBox has a required minimum length " & _
                    
"of " & intMinLength.ToString & " characters.", _
                    My.Application.Info.Title, MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)
            
End If
        
End If
    
End Sub


    
Private Sub InitializeComponent()
        
Me.SuspendLayout()
        
Me.ResumeLayout(False)

    
End Sub

End Class

posted @ 2007-07-10 16:44  山娃  阅读(258)  评论(0)    收藏  举报