Click here to Skip to main content
15,888,527 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionSelect all of the text in a textbox Pin
Marcus J. Smith15-May-07 3:36
professionalMarcus J. Smith15-May-07 3:36 
AnswerRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 4:17
mveDave Kreskowiak15-May-07 4:17 
GeneralRe: Select all of the text in a textbox Pin
Marcus J. Smith15-May-07 5:45
professionalMarcus J. Smith15-May-07 5:45 
AnswerRe: Select all of the text in a textbox Pin
TwoFaced15-May-07 4:22
TwoFaced15-May-07 4:22 
GeneralRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 4:30
mveDave Kreskowiak15-May-07 4:30 
GeneralRe: Select all of the text in a textbox Pin
Marcus J. Smith15-May-07 5:44
professionalMarcus J. Smith15-May-07 5:44 
GeneralRe: Select all of the text in a textbox Pin
TwoFaced15-May-07 5:55
TwoFaced15-May-07 5:55 
GeneralRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 6:01
mveDave Kreskowiak15-May-07 6:01 
You create your own custom TextBox class, inheriting from TextBox. I do this all the time to get all kinds of functionality I want out of the stock controls. It amazes me that people will readily do this for other classes, but rarely do it using controls.
Public Class NumericTextBox
    Inherits TextBox
 
    Protect Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        ' For some reason Keys.Back doesn't work for backspace, even though it's constant is 8!
        If Char.IsNumeric(e.KeyChar) OrElse e.KeyChar.Equals(Keys.Delete) _
                OrElse e.KeyChar.Equals(Convert.ToChar(8)) Then
            ' It's a key we'll allow, so let the base TextBox handle it normally.
            MyBase.OnKeyPress(e)
        Else
            ' Drop the key by not passing it to the base class.  Pass Handled back up the chain.
            e.Handled = True
        End If
    End Sub
 
    ' GotFocus is for when the user tabs into the TextBox
    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
        ' Let the base TextBox do it's thing first.
        MyBase.OnGotFocus(e)
        ' Then we'll select all the text.
        MyBase.SelectAll()
    End Sub
 
    ' If the user clicks anywhere in the TextBox, select all the text in it.
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        MyBase.OnClick(e)
        MyBase.SelectAll()
    End Sub
End Class

You don't have to create a new project to do this, but I highly recommend doing it. This way, the control is reusable in any project with a simple reference to this project.


A guide to posting questions on CodeProject[^]

Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007


GeneralRe: Select all of the text in a textbox [modified] Pin
TwoFaced15-May-07 5:53
TwoFaced15-May-07 5:53 
GeneralRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 6:08
mveDave Kreskowiak15-May-07 6:08 
GeneralRe: Select all of the text in a textbox Pin
TwoFaced15-May-07 6:28
TwoFaced15-May-07 6:28 
GeneralRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 6:30
mveDave Kreskowiak15-May-07 6:30 
GeneralRe: Select all of the text in a textbox - Windows app Pin
Marcus J. Smith15-May-07 6:15
professionalMarcus J. Smith15-May-07 6:15 
GeneralRe: Select all of the text in a textbox [modified] Pin
Dave Kreskowiak15-May-07 6:20
mveDave Kreskowiak15-May-07 6:20 
GeneralRe: Select all of the text in a textbox Pin
TwoFaced15-May-07 6:38
TwoFaced15-May-07 6:38 
GeneralRe: Select all of the text in a textbox Pin
Dave Kreskowiak15-May-07 6:55
mveDave Kreskowiak15-May-07 6:55 
AnswerRe: Select all of the text in a textbox [modified] Pin
Xandip15-May-07 19:16
Xandip15-May-07 19:16 
QuestionDelegates are type-safe Function Pointers Pin
Sonia Gupta15-May-07 2:34
Sonia Gupta15-May-07 2:34 
AnswerRe: Delegates are type-safe Function Pointers Pin
Colin Angus Mackay15-May-07 2:37
Colin Angus Mackay15-May-07 2:37 
Questiondelegates Pin
Sonia Gupta15-May-07 2:28
Sonia Gupta15-May-07 2:28 
AnswerRe: delegates Pin
Colin Angus Mackay15-May-07 2:35
Colin Angus Mackay15-May-07 2:35 
QuestionRe: delegates Pin
Sonia Gupta15-May-07 2:48
Sonia Gupta15-May-07 2:48 
AnswerRe: delegates Pin
Guffa15-May-07 3:50
Guffa15-May-07 3:50 
AnswerRe: delegates Pin
Colin Angus Mackay15-May-07 3:51
Colin Angus Mackay15-May-07 3:51 
QuestionRaising and handling events in 2 independant forms. Pin
steve_rm15-May-07 1:51
steve_rm15-May-07 1:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.