Selecting entire textbox in VB.NET 
Author Message
 Selecting entire textbox in VB.NET

Hi,

I have a form with many textboxes.  I want to highlight the entire
text on focus of the cell.  In VB, I could use control arrays combined
with the GotFocus event, and do something like:

Private Sub txtInputBox_GotFocus(Index As Integer)
   With txtInputBox(Index)
      .SelStart = 0
      .SelLength = Len(.Text)
   End With
End Sub

However, without the control arrays in VB.NET, how can I do something
similar without having to write 100 different Subs to handle each
control element?  Ideally, I'd like to have one function that does it
all for all similar controls.

Now what I have to do is:
Private Sub txtInputBox1_GotFocus Handles txtInputBox1.Enter
   With txtInputBox1
      .SelectionStart = 0
      .SelectionLength = Len(.Text)
   End With
End Sub

Private Sub txtInputBox2_GotFocus Handles txtInputBox2.Enter
   With txtInputBox2
      .SelectionStart = 0
      .SelectionLength = Len(.Text)
   End With
End Sub

Private Sub txtInputBox3_GotFocus Handles txtInputBox3.Enter
   With txtInputBox3
      .SelectionStart = 0
      .SelectionLength = Len(.Text)
   End With
End Sub

etc.

Obviously, this is NOT good.

ed



Sun, 23 Oct 2005 03:08:24 GMT  
 Selecting entire textbox in VB.NET
Hi,

        In the form load event add a handler to a common got focus
procedure.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim ctrl As System.Windows.Forms.Control

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is System.Windows.Forms.TextBox Then

        AddHandler ctrl.GotFocus, AddressOf Me.Textbox_GotFocus

End If

Next

End Sub

Private Sub Textbox_GotFocus(ByVal sender As Object, ByVal e As EventArgs)

Dim txtbox As System.Windows.Forms.TextBox = sender

With txtbox

    .SelectionStart = 0

    .SelectionLength = txtbox.Text.Length

End With

End Sub

Ken

---------------


Quote:
> Hi,

> I have a form with many textboxes.  I want to highlight the entire
> text on focus of the cell.  In VB, I could use control arrays combined
> with the GotFocus event, and do something like:

> Private Sub txtInputBox_GotFocus(Index As Integer)
>    With txtInputBox(Index)
>       .SelStart = 0
>       .SelLength = Len(.Text)
>    End With
> End Sub

> However, without the control arrays in VB.NET, how can I do something
> similar without having to write 100 different Subs to handle each
> control element?  Ideally, I'd like to have one function that does it
> all for all similar controls.

> Now what I have to do is:
> Private Sub txtInputBox1_GotFocus Handles txtInputBox1.Enter
>    With txtInputBox1
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> Private Sub txtInputBox2_GotFocus Handles txtInputBox2.Enter
>    With txtInputBox2
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> Private Sub txtInputBox3_GotFocus Handles txtInputBox3.Enter
>    With txtInputBox3
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> etc.

> Obviously, this is NOT good.

> ed



Sun, 23 Oct 2005 03:42:12 GMT  
 Selecting entire textbox in VB.NET
Why not write your own control that inherits from Textbox, in which you
overridethe gotfocused and lostfocused events?


Quote:
> Hi,

> I have a form with many textboxes.  I want to highlight the entire
> text on focus of the cell.  In VB, I could use control arrays combined
> with the GotFocus event, and do something like:

> Private Sub txtInputBox_GotFocus(Index As Integer)
>    With txtInputBox(Index)
>       .SelStart = 0
>       .SelLength = Len(.Text)
>    End With
> End Sub

> However, without the control arrays in VB.NET, how can I do something
> similar without having to write 100 different Subs to handle each
> control element?  Ideally, I'd like to have one function that does it
> all for all similar controls.

> Now what I have to do is:
> Private Sub txtInputBox1_GotFocus Handles txtInputBox1.Enter
>    With txtInputBox1
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> Private Sub txtInputBox2_GotFocus Handles txtInputBox2.Enter
>    With txtInputBox2
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> Private Sub txtInputBox3_GotFocus Handles txtInputBox3.Enter
>    With txtInputBox3
>       .SelectionStart = 0
>       .SelectionLength = Len(.Text)
>    End With
> End Sub

> etc.

> Obviously, this is NOT good.

> ed



Sun, 23 Oct 2005 20:18:45 GMT  
 Selecting entire textbox in VB.NET

Or check out the "Handles" keyword. You could create one event, then
append the handles of your other textboxes to the end of it. I think
this would be simpler than creating a custom control.

This example has three generic textboxes, all handled by one event.
Note textbox names after the "Handles" keyword.
==================================================
    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter,
TextBox3.Enter

        Dim ThisBox As TextBox = sender
        ThisBox.Text = "This is " & ThisBox.Name
==================================================
    End Sub

'ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolclassgotfocustopic.htm
'The GotFocus and LostFocus events are low-level focus events that
 'are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages.
'Typically, the GotFocus and LostFocus events are only used when
 'updating UICues. The Enter and Leave events should be used for all
 'controls except the Form class, which uses the Activated and
 'Deactivate events.



Quote:
>Why not write your own control that inherits from Textbox, in which you
>overridethe gotfocused and lostfocused events?



>> Hi,

>> I have a form with many textboxes.  I want to highlight the entire
>> text on focus of the cell.  In VB, I could use control arrays combined
>> with the GotFocus event, and do something like:

>> Private Sub txtInputBox_GotFocus(Index As Integer)
>>    With txtInputBox(Index)
>>       .SelStart = 0
>>       .SelLength = Len(.Text)
>>    End With
>> End Sub

>> However, without the control arrays in VB.NET, how can I do something
>> similar without having to write 100 different Subs to handle each
>> control element?  Ideally, I'd like to have one function that does it
>> all for all similar controls.

>> Now what I have to do is:
>> Private Sub txtInputBox1_GotFocus Handles txtInputBox1.Enter
>>    With txtInputBox1
>>       .SelectionStart = 0
>>       .SelectionLength = Len(.Text)
>>    End With
>> End Sub

>> Private Sub txtInputBox2_GotFocus Handles txtInputBox2.Enter
>>    With txtInputBox2
>>       .SelectionStart = 0
>>       .SelectionLength = Len(.Text)
>>    End With
>> End Sub

>> Private Sub txtInputBox3_GotFocus Handles txtInputBox3.Enter
>>    With txtInputBox3
>>       .SelectionStart = 0
>>       .SelectionLength = Len(.Text)
>>    End With
>> End Sub

>> etc.

>> Obviously, this is NOT good.

>> ed

----------------------------

We contemplate eternity
Beneath the vast indifference of heaven

- Warren Zevon



Sun, 23 Oct 2005 23:38:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Highlighting entire text in textbox after incorrect entry by user

2. CODE: How to Select Entire String (Sentence) and Delete

3. newbie: how to select entire row

4. How to select entire row in ListView ?

5. Select entire row in ListView

6. How to select entire row in Listview

7. How to select entire row in ListView ? - FullRowSelect

8. How to select entire row in ListView ?

9. Selecting Entire contents of text box for Overtyping

10. VBA Select contents of entire file

11. Selecting entire row in Listview?

12. Listview - Selecting an entire row?

 

 
Powered by phpBB® Forum Software