how to select just a word by a double click in a textbox?

  • Thread starter Thread starter Pascal
  • Start date Start date
P

Pascal

hi from a newbee
As said in the title : how to select just a word by a double click in a
textbox and show it in a label. I tried a lot of thing without success. Can
somebody help me showing me good links or else?
thanks

--
 
The short form of the answer is: in the MouseDoubleClick event handler for
your textbox (TTT), set text property of your label (LLL) to the selected
text in TTT, eg by

LLL.Text = Trim(TTT.SelectedText)

The purpose of Trim() is that the default function of double click is to
select the word under the cursor and all trailing spaces. I checked that
this works for a rich text box, so I assume it works for a text box.

I understand you said newbee, so LLL in your case would probably be
something like Label1 or Label2, and similarly TTT is probably TextBox1 or
similar. You need to add a double click handler for your text box and insert
one line of code. Get it?
 
I tried this from an example in c# but it takes care only of a space as
separator of words
How can i do to take care of any separator like : {" "c, ","c, "."c, ":"c,
";"c, "?"c, "!"c, "'"c, "`"c, "-"c} etc. ?
thanks

Dim cursorPosition As Integer = TextBox1.SelectionStart
Dim nextSpace As Integer = TextBox1.Text.IndexOf(" "c,
cursorPosition)
Dim selectionStart As Integer = 0
Dim trimmedString As String = String.Empty
' Strip everything after the next space...
If nextSpace <> -1 Then
trimmedString = TextBox1.Text.Substring(0, nextSpace)
Else
trimmedString = TextBox1.Text
End If


If trimmedString.LastIndexOf(" "c) <> -1 Then
selectionStart = 1 + trimmedString.LastIndexOf(" "c)
trimmedString = trimmedString.Substring(1 +
trimmedString.LastIndexOf(" "c))
End If

TextBox1.SelectionStart = selectionStart
TextBox1.SelectionLength = trimmedString.Length
 
I tried this from an example in c# but it takes care only of a space as
separator of words
How can i do to take care of any separator like : {" "c, ","c, "."c,
":"c, ";"c, "?"c, "!"c, "'"c, "`"c, "-"c} etc. ?
thanks

add something like:

Dim chars As Char() = " ,.:;?!-".ToCharArray

Dim cursorPosition As Integer = TextBox1.SelectionStart
Dim nextSpace As Integer = TextBox1.Text.IndexOf(" "c, cursorPosition)

Change the above to:

Dim nextSpace As Integer = TextBox1.Text.IndexOfAny _
(chars, cursorPosition)
Dim selectionStart As Integer = 0
Dim trimmedString As String = String.Empty
' Strip everything after the next space...
If nextSpace <> -1 Then
trimmedString = TextBox1.Text.Substring(0, nextSpace)
Else
trimmedString = TextBox1.Text
End If


If trimmedString.LastIndexOf(" "c) <> -1 Then
selectionStart = 1 + trimmedString.LastIndexOf(" "c)
trimmedString = trimmedString.Substring(1 + trimmedString.LastIndexOf("
"c))

and change the above to:

trimmedString = trimmedString.Substring _
(1 + trimmedString.LastIndexOfAny(chars))
End If

TextBox1.SelectionStart = selectionStart
TextBox1.SelectionLength = trimmedString.Length

Hopefully this does what you want.
 
Woah you did it, many thanks!

Is there an elegant maner to do the same with this code :

words = TextBox1.Text.Split(New String() {" "c, ","c, "."c, ":"c,
";"c, "?"c, "!"c, "'"c, "`"c, "-"c},StringSplitOptions.RemoveEmptyEntries)

I tried this with the idea i could use the array chars already done:
words = TextBox1.Text.Split(New String() {chars},
StringSplitOptions.RemoveEmptyEntries)
but it doesn't work!

The aim of the code is to store all the words in an array, then for few
words with special characteristics, to inclose them with a span tag in order
to build special html page like theese here :

http://www.scalpa.info/fr_gram_vb1_clicmot.php

It's a long way for a non-programmer to design their own tools (something
like hotpotatoes...). But it's very exciting.

thanks for you help
pascal
 
Woah you did it, many thanks!

Is there an elegant maner to do the same with this code :

words = TextBox1.Text.Split(New String() {" "c, ","c, "."c, ":"c, ";"c,
"?"c, "!"c, "'"c, "`"c, "-"c},StringSplitOptions.RemoveEmptyEntries)

I tried this with the idea i could use the array chars already done:
words = TextBox1.Text.Split(New String() {chars},
StringSplitOptions.RemoveEmptyEntries)
but it doesn't work!

The aim of the code is to store all the words in an array, then for few
words with special characteristics, to inclose them with a span tag in
order to build special html page like theese here :

http://www.scalpa.info/fr_gram_vb1_clicmot.php

It's a long way for a non-programmer to design their own tools
(something like hotpotatoes...). But it's very exciting.

thanks for you help
pascal

I just answered on your other post to look at Regular Expressions (class
Regex). It is more powerful for this type of app.
 
Back
Top