473,378 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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?
Jun 2 '08 #1
3 1318
Plater
7,872 Expert 4TB
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 GetCharIndexFromPosition() which gets a charindex from a point (which would be your X,Y from double clicking)
And then use GetLineFromCharIndex() to get the line index.
Then you can probably use .Lines[lineIndex] to get the text?
Jun 2 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1. private void StatusTextBox_DoubleClick(object sender, EventArgs e)
  2.       {
  3.          int StartOfLine;
  4.          int EndOfLine;
  5.  
  6.          /////
  7.          // Find where the line starts by backing up a character at a time until
  8.          // there are no more characters or a newline character is found.
  9.          /////
  10.          for(StartOfLine = StatusTextBox.SelectionStart; StartOfLine >= 0; StartOfLine--)
  11.             {
  12.             if(StatusTextBox.Text[StartOfLine] == '\n')
  13.                {
  14.                StartOfLine++; // advance 1 char so newline is not part of selected line.
  15.                break;
  16.                }
  17.             }
  18.          if(-1 == StartOfLine)   // if no newline found start selection with 1st char in TextBox
  19.             {
  20.             StartOfLine = 0;
  21.             }
  22.  
  23.          /////
  24.          // Find where the line ends by going forward a character at a time until
  25.          // there are no more characters or a newline character is found.
  26.          /////
  27.          for(EndOfLine = StatusTextBox.SelectionStart; EndOfLine < StatusTextBox.Text.Length; EndOfLine++)
  28.             {
  29.             if(StatusTextBox.Text[EndOfLine] == '\n')
  30.                {
  31.                EndOfLine--; // go back 1 char so newline is not part of selected line.
  32.                break;
  33.                }
  34.             }
  35.          if(StatusTextBox.Text.Length == EndOfLine)   // if no newline found start selection with 1st char in TextBox
  36.             {
  37.             EndOfLine = StatusTextBox.Text.Length; // selection ends with last character
  38.             }
  39.  
  40.          StatusTextBox.SelectionStart = StartOfLine;
  41.          StatusTextBox.SelectionLength = EndOfLine - StartOfLine;
  42.  
  43.          GenericCommandTextBox.Text = StatusTextBox.SelectedText;
  44.          Update();
  45.       } // End of StatusTextBox_DoubleClick()
  46.  
Jun 2 '08 #3
Thanks for pointing me to the 2 functions. They look like they can be used for a more elegant solution than mine.

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 GetCharIndexFromPosition() which gets a charindex from a point (which would be your X,Y from double clicking)
And then use GetLineFromCharIndex() to get the line index.
Then you can probably use .Lines[lineIndex] to get the text?
Jun 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Nurchi BECHED | last post by:
Hello, All! I have created an application with a multiline textbox on the form. When I press a button, it has to show something and then break the line and show something else. I tried "\n",...
2
by: Enzo Marinelli | last post by:
Hello everyone I have a MultiLine TextBox, as follows <asp:TextBo Id="StreetAddress Rows="4 Runat="Server TextMode="MultiLine Width="100%"/
2
by: Harry Hudini | last post by:
I have a multiline textbox and want to be able to allow the user to position the cursor in a line of the textbox, then click on a Remove button to remove the line which the cursor is on. Thing is,...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
1
by: mosscliffe | last post by:
I have a Gridview created by dragging a table onto a web form page and I have enabled the edit key. One of my field is a multi-line 'text' field. How do I change the standard textbox created...
1
by: Jeff | last post by:
hey asp.net 2.0 In my website I've got a textbox with TextMode=MultiLine. The user can enter text that contain "new line"... But to displays this I'm also using a textbox with...
2
by: garyusenet | last post by:
I have a multi line text box, i'd like to store the contents of this textbox to a text file. However when I write the value of ' textbox.text ' to the file it is written verbatim spanning many...
2
by: BillE | last post by:
I need to create a multi-line label dynamically. Since labels aren't multi-line, I'm creating a textbox and making it look like a label with the multiline property set to true. I want to...
2
by: sherifffruitfly | last post by:
Hi all, I'm using a multiline+scrollbar textbox to log output as my program runs. Basically, I pass a reference to the class instance that's currently doing stuff, and the instance can tack on...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.