Reputation: 13356
In a Windows Form, using C#, how do I select (as in, actually highlight the text, making it accessible to the .SelectedText property) a word based on the cursor location?
Here's what I'm trying to do. I have a textbox that users can currently select a word by highlighting it. They can then perform various actions to the word, but I want to make it simpler.
I wish to make it so they can simple put the cursor inside the word and the app will select the word the cursor is inside of.
Thanks in advance!
Upvotes: 0
Views: 7469
Reputation: 2189
//this is our article
string article = " " + richTextBox1.Text.ToLower() + " ";
//we search the word from textbox1
int middle = article.IndexOf(textBox1.Text);
int headOfWord = article.LastIndexOf(" ", middle);
int tailOfWord = article.IndexOf(" ", middle);
//we have found the head and tail of the word
textBox2.Text = article.Substring(headOfWord, tailOfWord - headOfWord);
richTextBox1.Focus();
richTextBox1.Select(headOfWord, tailOfWord - headOfWord - 1);
Upvotes: 1
Reputation: 2102
You can use SelectionStart
and SelectionLength
but you probably need to find the next space from the cursor position, then reverse the contents of the textbox and find the next "space" from the "altered cursor" position, then use the two methods above.
This will also work
int cursorPosition = textBox1.SelectionStart;
int nextSpace = textBox1.Text.IndexOf(' ', cursorPosition);
int selectionStart = 0;
string trimmedString = string.Empty;
// Strip everything after the next space...
if (nextSpace != -1)
{
trimmedString = textBox1.Text.Substring(0, nextSpace);
}
else
{
trimmedString = textBox1.Text;
}
if (trimmedString.LastIndexOf(' ') != -1)
{
selectionStart = 1 + trimmedString.LastIndexOf(' ');
trimmedString = trimmedString.Substring(1 + trimmedString.LastIndexOf(' '));
}
textBox1.SelectionStart = selectionStart;
textBox1.SelectionLength = trimmedString.Length;
Upvotes: 5
Reputation: 5961
actually i got a simpler approach here
Dim intCursor As Integer = txtInput.SelectionStart
Dim intStart As Int32 = CInt(IIf(intCursor - 1 < 0, 0, intCursor - 1))
Dim intStop As Int32 = intCursor
intStop = txtInput.Text.IndexOf(" ", intCursor)
intStart = txtInput.Text.LastIndexOf(" ", intCursor)
If intStop < 0 Then
intStop = txtInput.Text.Length
End If
If intStart < 0 Then
intStart = 0
End If
debug.print( txtInput.Text.Substring(intStart, intStop - intStart).Trim)
Upvotes: 0
Reputation: 1521
Hope this helps:
if (string.IsNullOrEmpty(textBox1.Text))
{
return;
}
int cursorPos = textBox1.SelectionStart;
int firstPos = 0;
int lastPost = 0;
// If the current cursor is at the end of the string, try to go backwards
string currentChar = cursorPos == textBox1.Text.Length ? textBox1.Text.Substring(cursorPos - 1, 1) : textBox1.Text.Substring(cursorPos, 1);
if (currentChar == " ")
{
cursorPos--;
}
// Iterate to the first position where a space is
for (int i = cursorPos; i > 0; i--)
{
// Get the current character
currentChar = i == textBox1.Text.Length ? textBox1.Text.Substring(i - 1, 1) : textBox1.Text.Substring(i, 1);
if (currentChar == " ")
{
firstPos = i+1;
break;
}
}
for (int i = cursorPos; i <= textBox1.Text.Length; i++)
{
if (i == textBox1.Text.Length)
{
lastPost = i;
}
else
{
// Get the current character
currentChar = textBox1.Text.Substring(i, 1);
if (currentChar == " ")
{
lastPost = i;
break;
}
}
}
textBox1.SelectionStart = firstPos;
textBox1.SelectionLength = lastPost - firstPos;
textBox1.Focus();
For this example, you need a text box, textBox1 and a button where this code goes. Let me know if you need any help.
EDIT: Changed a bit the code and tested all the scenarios. Hope it helps!
Upvotes: 0