Free VB.NET Resources From MicrosoftEnlarge Combobox Dropdown Area

Select Text Upon Entry

June 15th, 2007

Selling you on VB.NETOn my VB6 Notebook Archive Site I have several code examples that were part of my standard library of routines that I used in many places. I thought it would be an interesting exercise to revisit these functions to see how they would be implemented in VB.NET. The first one we’ll take a look at is the Select Text Upon Entry routine. In VB6 we used the SelStart and SelLength properties of the textbox in the GotFocus event to select all the text when the user enters it. How would you do this in VB.NET?

First of all, let’s take a look at how you would do this in VB6:

Private Sub Text1_GotFocus()
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
End Sub

Now, let’s see what our VB.NET/Framework 2.0 level options are.

SelStart and SelLength are still there although they are completely spelled out as SelectionStart and SelectionLength. Otherwise they function the same as their VB6 counterparts. However, there are two new .NET methods that select text, Select and SelectAll. Select allows you to select a range indicated by a start value and length while SelectAll selects all of the text in the textbox at once. So, it looks like our natural choice should be SelectAll.

The GotFocus event is still there too but there is also the new Enter event. Which should we use? Well according to MSDN the event sequence when the user, or code, enters a control is Enter then GotFocus. Microsoft recommends that programmers use the Enter/Leave events instead of the legacy GotFocus/LostFocus events. Therefore, to update the routine we should put our code in the Enter event.

So, our updated routine would look like this:

Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
   TextBox1.SelectAll()
End Sub

Now we can test it and see what happens. We type in some text into TextBox1 and tab to another control and tab back to TextBox1 and our text is automatically selected. But, when we mouse into the textbox it isn’t selected. What gives?!? OK, let’s move the code back to our old reliable GotFocus event. The same thing happens!

As it turns out, this is caused by the way the event structure in .NET works. When the control is selected with the mouse, the TextBox’s Enter event is fired before the MouseDown event. The MouseDown event then sets the SelectionStart property to the location where the mouse was clicked. To fix this problem, you have to have code in both the Enter and MouseDown events.

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    TextBox1.SelectAll()
End Sub

This means we have to have the same code in two different events due to this new behavior in .NET. One option would be to subclass the textbox control and override the behavior in code there, thus moving the same code in two places from your main application to within the control code. This might be a good option to consider if this is a feature you need throughout your application.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB6 To VB.NET


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (1 votes, average: 1 out of 5)
Loading ... Loading ...

1 Comment Add your own

  • 1. James Moss  |  May 25th, 2008 at 9:57 pm

    This posting helped me, thanks!

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives