A nasty little TextBox

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

Hi there,

I'm trying to do an easy thing... but no luck so far...

I have a form with a textbox, and I want the following behaviour on mouse
click event:
-if no text is selected, select all
-if some (or all) text is selected, unselect all

This looks good... but doesn't work! :-(

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Click
If TextBox1.SelectionLength > 0 Then
TextBox1.Select(0, 0)
Else
TextBox1.SelectAll()
End If
End Sub

I set a breakpoint on "TextBox1.Select(0,0)", never stops there; and I dont
see why?
How can I get the behaviour I want?

Please help me... to master this nasty TextBox!

Regards,
Mario
 
Hi Mario,

Can it be that by clicking on the textbox you deselect everything?
:-)
You can make a seperate button to try or maybe use the mouse.down, I never
tried that (just a guess).

Cor
 
No, sorry, using a button is not acceptable, not it is sendkeys, or other
bogus workarrounds.

Thanks any way.

Regards,
Mario
 
Cor is right. Clicking the textbox deselects the text. Therefore, it will
always be zero. Try this it out. It works.

Dim hold As Integer

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Click
If hold > 0 Then
TextBox1.Select(0, 0)
Else
TextBox1.SelectAll()
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.Select(0, 5)
hold = TextBox1.SelectionLength
End Sub
 
Hi Brian,

Thank's. Not exactly like you said, but you gave me a good idea, to cache
the selection length.


Static hold As Integer
If hold > 0 Then
TextBox1.Select(0, 0)
hold = 0
Else
TextBox1.SelectAll()
hold = TextBox1.SelectionLength
End If


Regards,
Mario
 
Back
Top