VB.net关于Textbox文本控件全选的小技巧
#Region "搜索框的全选"
Dim counter As Integer = 0 '点击次数 Dim isSelectAll As Boolean = False '是否全选 Dim selectPoint As Integer = 0 '当前光标位置
Private Sub TextBox1_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.Click counter += 1
If counter = 1 Then '判断是否首次点击 selectPoint = TextBox1.SelectionStart TextBox1.SelectAll() isSelectAll = True ElseIf counter > 1 Then If TextBox1.SelectionLength = 0 Then '判断选择字符的长度是否为0 If TextBox1.SelectionStart = selectPoint Then '判断是否点击上次同一位置 If isSelectAll = True Then isSelectAll = False Else TextBox1.SelectAll() isSelectAll = True End If Else If TextBox1.SelectionStart = TextBox1.TextLength AndAlso isSelectAll = False Then '判断光标位置在最末 TextBox1.SelectAll() isSelectAll = True Else selectPoint = TextBox1.SelectionStart '不是上次同一位置,则记录当前位置 isSelectAll = False End If
End If End If End If End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.LostFocus counter = 0 End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.DoubleClick isSelectAll = True End Sub
#End Region (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |