How do I tell which line has been doubleclicked in a multiline textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffsteed
    New Member
    • Jun 2008
    • 3

    #1

    How do I tell which line has been doubleclicked in a multiline textbox

    I am using VisualStudio 2005 to create an application in C#.

    I have a scrollable, multiline textbox where I want the user to be able to doubleclick any given line to put that line in another textbox. How do I tell which line has been doubleclicked? The event argument contains only x,y information and I don't want to have to do the math to figure out which of the visible lines contains that point.

    Any ideas?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmm, double clicking text normally ( I think?) has the habbit of selecting an entire word/section of words. Perhaps the selectedtext property can help?

    Another idea might be to switch to a ListBox (if possible) and then you can get the index of the entry they clicked and copy it over?

    EDIT:
    Textboxes appear to have this function GetCharIndexFro mPosition() which gets a charindex from a point (which would be your X,Y from double clicking)
    And then use GetLineFromChar Index() to get the line index.
    Then you can probably use .Lines[lineIndex] to get the text?

    Comment

    • jeffsteed
      New Member
      • Jun 2008
      • 3

      #3
      I actually figured out a way to do it that involves using the SelectionStart property. I tried to allow for cases where the user clicks on the first line, clicks in the very first character possition, the last line or after the last line. I have not tried it much but this seems to work as I intended:

      Code:
      private void StatusTextBox_DoubleClick(object sender, EventArgs e)
            {
               int StartOfLine;
               int EndOfLine;
      
               /////
               // Find where the line starts by backing up a character at a time until
               // there are no more characters or a newline character is found.
               /////
               for(StartOfLine = StatusTextBox.SelectionStart; StartOfLine >= 0; StartOfLine--)
                  {
                  if(StatusTextBox.Text[StartOfLine] == '\n')
                     {
                     StartOfLine++; // advance 1 char so newline is not part of selected line.
                     break;
                     }
                  }
               if(-1 == StartOfLine)   // if no newline found start selection with 1st char in TextBox
                  {
                  StartOfLine = 0;
                  }
      
               /////
               // Find where the line ends by going forward a character at a time until
               // there are no more characters or a newline character is found.
               /////
               for(EndOfLine = StatusTextBox.SelectionStart; EndOfLine < StatusTextBox.Text.Length; EndOfLine++)
                  {
                  if(StatusTextBox.Text[EndOfLine] == '\n')
                     {
                     EndOfLine--; // go back 1 char so newline is not part of selected line.
                     break;
                     }
                  }
               if(StatusTextBox.Text.Length == EndOfLine)   // if no newline found start selection with 1st char in TextBox
                  {
                  EndOfLine = StatusTextBox.Text.Length; // selection ends with last character
                  }
      
               StatusTextBox.SelectionStart = StartOfLine;
               StatusTextBox.SelectionLength = EndOfLine - StartOfLine;
      
               GenericCommandTextBox.Text = StatusTextBox.SelectedText;
               Update();
            } // End of StatusTextBox_DoubleClick()

      Comment

      • jeffsteed
        New Member
        • Jun 2008
        • 3

        #4
        Thanks for pointing me to the 2 functions. They look like they can be used for a more elegant solution than mine.

        Originally posted by Plater
        Hmm, double clicking text normally ( I think?) has the habbit of selecting an entire word/section of words. Perhaps the selectedtext property can help?

        Another idea might be to switch to a ListBox (if possible) and then you can get the index of the entry they clicked and copy it over?

        EDIT:
        Textboxes appear to have this function GetCharIndexFro mPosition() which gets a charindex from a point (which would be your X,Y from double clicking)
        And then use GetLineFromChar Index() to get the line index.
        Then you can probably use .Lines[lineIndex] to get the text?

        Comment

        Working...