FredMunro

Hello,

I have a problem with a number of textboxes in a current project (also noticed in previous projects) where the text within the textbox is highlighted at form load (noticed on sub forms sometimes. However the non-higlighting textboxes are created exactly the same to the higlighting ones).

The textboxes are usually within GroupBoxes and are multiline enabled, with the cursor set to "Arrow", the font is the default. But the text is always higlighted, is there someway to prevent this

Thanks in advance

Fred Munro



Re: Visual Basic Language Textbox problem (VB, VS05)

kleinma

Fred, I have seen this behavior too.

I am not sure if there is a simple solution since it does seem like some form of bug (since it happens on some, but not all)

However you can loop all the controls on the form (recursively) and make sure none of the textbox's text is highlighted...

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

DeselectAllTextboxes(Me)

End Sub

Private Sub DeselectAllTextboxes(ByVal c As Control)

For Each ctrl As Control In c.Controls

If ctrl.Controls.Count > 0 Then

DeselectAllTextboxes(ctrl)

End If

If TypeOf c Is TextBox Then

With DirectCast(c, TextBox)

.SelectionStart = 0

.SelectionLength = 0

End With

End If

Next

End Sub






Re: Visual Basic Language Textbox problem (VB, VS05)

Daniel fred Munro

Thanks for the response,

however that doesn't seem to resolve the problem... I have tried placing the supplied code as the very first entry, and midway also towards the end, all with the same result the textbox text is still highlighted.

Any Ideas

Fred Munro





Re: Visual Basic Language Textbox problem (VB, VS05)

decyclone

This is the code I created to test the scenario you described :

Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = New String("A"c, 500)
TextBox2.Text = New String("B"c, 500)
TextBox3.Text = New String("C"c, 500)
TextBox4.Text = New String("D"c, 500)

TextBox1.SelectionStart = 0 ' When I comment out the following lines, all the text in the textboxes gets selected when I give them focus without clicking on them
TextBox1.SelectionLength = 0

TextBox2.SelectionStart = 0
TextBox2.SelectionLength = 0

TextBox3.SelectionStart = 0
TextBox3.SelectionLength = 0

TextBox4.SelectionStart = 0
TextBox4.SelectionLength = 0
End Sub

If the solution is not the one you wanted, then provide some code to reproduce the problem you are facing.
End Class





Re: Visual Basic Language Textbox problem (VB, VS05)

Daniel fred Munro

Thanks for the response,

This seems to be a half answer, after adjusting the code to fit the textbox name in the project, I ran it.. great no longer highlighted! but instead of my text it has "A" (Capitol A's) representing every character.

As far as I know there is no code related to the textbox itself, it's a normal text box, multilined, "ActiveGradientCaption" background color, text is centered, docked to fill the group box it is in.

All options that are set from the properties, no code. I haven't set any actions from it, just to sit there and display some text.

Fred Munro





Re: Visual Basic Language Textbox problem (VB, VS05)

decyclone

the following line

new String("A"c,5)

returns a string : "AAAAA", so the line

new String("A"c,500)

in the code I posted returns 500 A's, and I set them as textbox's text so the textbox is filled with A's.

It's String class's constructor I am using. I hope this solves your problem.





Re: Visual Basic Language Textbox problem (VB, VS05)

Daniel fred Munro

Thanks for that info,

I've been toying with it for a few hours I found that the following does the job perfectly:

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

TextBox1.SelectionStart = 0

TextBox1.SelectionLength = 0

End Sub

Thanks for you help

Fred Munro