Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code in custom control does not work if form is not a top-level window

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I wanted to change the behavior of a text box so that when entering the text box it would highlight the full string inside.
Code:
    Private Sub location_tb_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles location_tb.Enter
        Dim tb As TextBox = CType(sender, TextBox)

        If MouseButtons = Windows.Forms.MouseButtons.None Then
            tb.SelectAll()
            _HadFocus = True
        End If
    End Sub

    Private Sub location_tb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles location_tb.MouseUp
        Dim tb As TextBox = CType(sender, TextBox)

        If _HadFocus = False And tb.SelectionLength = 0 Then
            _HadFocus = True
            tb.SelectAll()
        End If
    End Sub

    Private Sub location_tb_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles location_tb.MouseDoubleClick
        Dim tb As TextBox = CType(sender, TextBox)

        If _HadFocus = True And tb.SelectionLength <> 0 Then
            tb.SelectionStart = 0
            tb.SelectionLength = 0

            _HadFocus = True
            tb.SelectAll()
        End If
    End Sub
    
    Private Sub location_tb_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles location_tb.Leave
        _HadFocus = False
    End Sub
The code works fine, but if I set the forms TopLevel to False so the form acts as a control or MdiChild then all it will do is highlight the text. You can no longer click to position the cursor. If I step through the code it acts normally(I see it flash as it repositions the cursor), but some how reselects the text without steping through the code that is causing it. Any ideas why this happens and how to stop it?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I still can't seem to find a solution to this problem.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top