views:

1184

answers:

3

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!

+2  A: 

Use SelectionStart and SelectionLength properties.
google

tster
hmm... I can get the cursor location with SelectionStart. What if they put the cursor in the middle of the word (which is more likely in my case). I'm not clear on how to actually select the word. (I come from a web development background...). Thanks in advance.
Chad
Use the .Text and find the spaces:for example:int startWord = box.SelectionStart;while (box.Text[startWord] != ' ') { startWord--; }
tster
+2  A: 

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;
Michael Prewecki
works great. thank you.
Chad
A: 

Hello,

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!

Sebastian
Thank you, trying it now.
Chad
Works great! Except, if the user leaves the cursor at the end of a word. It throws an ArgumentOutOfRangeException from the Substring when trying to find firstPos.
Chad
And, there are no other characters in the textbox...
Chad