|
-
Jun 15th, 2009, 08:57 AM
#1
Thread Starter
New Member
Getting the current/selected line num in a multiline textbox
I'm trying to get the selected line num (or anything which helps me to identify the line) in a multi line texbox.
The meaning for current or selected line is for the line that the user's characteristic (I found that word on the dictionary) is placed on.
thanks for helpers =]
-
Jun 15th, 2009, 09:23 AM
#2
Re: Getting the current/selected line num in a multiline textbox
You need to understand what a "line" is in a mutiline textbox. A line is defined by the invisible CRLF character (carriage return/line feed) in the string that tells whatever control is displaying the string that there should be a new line there.
However, in a multiline textbox, when you type, and the text you type is too long for 1 line, and it goes to the next line, this is NOT a new line as far as the string is concerned. No CRLF character is inserted into the string here. You have to actually hit the enter key to go to the next line for a CRLF to be there.
That being said, a multiline textbox has a .lines property, which is an array of strings, each array element is each line in the string, but again the lines are determined by CRLF, so it likely will not match exactly to what line you would THINK it is based on looking at the actual textbox control.
-
Jun 15th, 2009, 09:23 AM
#3
Re: Getting the current/selected line num in a multiline textbox
try this:
vb Code:
TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 15th, 2009, 09:38 AM
#4
Thread Starter
New Member
Re: Getting the current/selected line num in a multiline textbox
 Originally Posted by kleinma
You need to understand what a "line" is in a mutiline textbox. A line is defined by the invisible CRLF character (carriage return/line feed) in the string that tells whatever control is displaying the string that there should be a new line there.
However, in a multiline textbox, when you type, and the text you type is too long for 1 line, and it goes to the next line, this is NOT a new line as far as the string is concerned. No CRLF character is inserted into the string here. You have to actually hit the enter key to go to the next line for a CRLF to be there.
That being said, a multiline textbox has a .lines property, which is an array of strings, each array element is each line in the string, but again the lines are determined by CRLF, so it likely will not match exactly to what line you would THINK it is based on looking at the actual textbox control.
Is there any way to choose a line by the way it's showed on the textbox?
Or maybe there's a way to make the textbox to add a CRLF character after finishing a line?
thanks for helping
-
Jun 15th, 2009, 09:54 AM
#5
Thread Starter
New Member
Re: Getting the current/selected line num in a multiline textbox
 Originally Posted by .paul.
try this:
vb Code:
TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart)
Mmm I don't think it works, maybe you can give me an example for removing the current or selected line?
-
Jun 15th, 2009, 09:59 AM
#6
Re: Getting the current/selected line num in a multiline textbox
it gets the line that has the caret in.
you havent explained exactly what you're trying to do yet?
for instance:
vb Code:
msgbox(TextBox1.GetLineFromCharIndex(TextBox1.text.indexof("Hello")))
would return the line number of the first line that contains "Hello"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 15th, 2009, 10:03 AM
#7
Re: Getting the current/selected line num in a multiline textbox
to remove a line
vb Code:
Dim lines As New List(Of String)(TextBox1.Lines)
lines.RemoveAt(lineNumber)
TextBox1.Lines = lines.ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 15th, 2009, 10:22 AM
#8
Thread Starter
New Member
Re: Getting the current/selected line num in a multiline textbox
Ok.
First of all I want to find the line which is selected in the textbox (means the characteristic is on it).
Then, after it was found I can remove, edit or anything else..
.paul. has posted this way:
Form1.TextBox1.Lines().GetValue(Form1.TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart))
which gets the current selected line, but if the line got no text it may be a problem.
I'm not sure which way is the best, but if there isn't any other way, I will use .paul.'s way.
thanks for all :P
-
Jun 15th, 2009, 10:27 AM
#9
Re: Getting the current/selected line num in a multiline textbox
this will get the currently selected line:
vb Code:
dim str as string = TextBox1.Lines(TextBox1.GetLineFromCharIndex(TextBox1.SelectionStart))
to check its not nothing:
vb Code:
if str.trim <> "" then
'not an empty line
end if
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 15th, 2009, 01:52 PM
#10
Re: Getting the current/selected line num in a multiline textbox
How do you define what is "selected" in the textbox? Does the user just highlight text with the mouse? Or is it just where the blinking cursor happens to be is what you consider to be selected? Normally in terms of something being selected in a textbox, that means it is text that is highlighted.
The word "characteristic" that you keep using I think you are using incorrectly... I haven't heard that as any sort of programming term.
-
Jun 15th, 2009, 02:54 PM
#11
Thread Starter
New Member
Re: Getting the current/selected line num in a multiline textbox
 Originally Posted by kleinma
How do you define what is "selected" in the textbox? Does the user just highlight text with the mouse? Or is it just where the blinking cursor happens to be is what you consider to be selected? Normally in terms of something being selected in a textbox, that means it is text that is highlighted.
The word "characteristic" that you keep using I think you are using incorrectly... I haven't heard that as any sort of programming term.
Hhh I know, the smart dictionary told me that xD
And yes, for the selected line I mean for the line which the blinking cursor is placed in it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|