defining on which line the cursor is in a textbox

  • Thread starter Thread starter DraguVaso
  • Start date Start date
* "DraguVaso said:
Thanks, but unfortunately it doesn't work, it gave me an error:
Run-time exception thrown :
System.Runtime.InteropServices.MarshalDirectiveException - PInvoke
restriction: can not return variants.

Sorry, typo. I forgot to mark the return value as 'Int32'.
 
Hi,
Yes, you are right there is no way to find char index from line. It looks
like it meant to be used only for reporting the caret position in the status
bar. So I give up the idea. But the second one... You said it is too slow.
And you said that it takes 10 second for the first 1500 records. Of cource I
have no idea what the structure of the file looks like, however, my idea is
to start seaching from the current position of the caret to the next line.
If the next line means the next record it doesn't have to look for all 1500
records to find the begining of the next line. Or may be one line has many
records... So how big one line can be? How many '0' one line can have? And
is it only the '0' that marks the begining of the line?

I agree that if the edit control is readonly caching the text is the best
solution. In this case you can optimize the searching verry well.

B\rgds
100
DraguVaso said:
Hi,

Your second idea takes to much time, hehe. There are too many 0's in the
files. After 10 seconds it has only looked into 1500 records.

Regarding your first solution: that works ideal to find the current line,
but unfortunately there doesn't seem to be a functionthat does the reverse:
when I found my 0-line: giving me the position so I can set the
SelectionStart to it. In case you would know a way to get aroudn this
problem, it woudl be nice if you could tell me.

Thanks a lot!

Pieter

100 said:
Hi DraguVaso,
I misundertood your question. Sorry about that.
I take that when you say cursor you mean the caret not the mouse cursor.
Right?
If it is the caret you can go again with RichTextBox.
The position of the carret you can get from SelectionStart property.
This
is
position is in terms of char index.
Then you can get the line where the caret currently is by using
GetLineFromCharIndex.
However, this will work only if the WordWrap is turned off.
GetLineFormCharIndex returns the physical line on the screen. If a line has
been splited by the WordWrap RichTextBox considers this line as two or more
physical lines.

My second idea:

You can consider using RichTextBox' Find method. It has whole bunch of
overloads.
You can look for '0' this is what your lines begin with. When you find it
you have to make sure it is at the begining of the line. This is not easy.
Unfortunately Find method cannot search for new-line characters.
Thus, you cannot do Find("\n0") it won't find anything. So the first
solution I came up with is to check whether the '0' character you have found
and the character before it are on the same physical line. If they are not
'0' is at the begining of the line.
If they are, though, the '0' may be or may be not what you are looking for.
This will work only of '0' is the very first character in the line; no
whitspaces or stuff.

You can test the following code and see of it works for you. Just create a
form with one button and one RichTextBox on it and paste the following
button's event handler.
BTW the example is in c#, but the translation should be a piece of cake.

private void button1_Click(object sender, System.EventArgs e)
{

int index = richTextBox1.Find(new char[]{'0'},
richTextBox1.SelectionStart + 1);
if(index >= 0)
{
int foundTextLine = richTextBox1.GetLineFromCharIndex(index);
if(foundTextLine > richTextBox1.GetLineFromCharIndex(index) -1)
{
file://Begining of the line found
richTextBox1.SelectionStart = index;
}

}
file://Just to show the caret.
richTextBox1.Focus();

}


BTW, is your TextBox readonly or it can be change by the user? If it is
readonly you can cache the text and do the searching in the copy. Reading
Lines property is slow operation because it envolves sending the
underlaying windows control messages and then split the text into lines.


100 said:
Hi DraguVaso,

If it is possible and if you don't like using P\Invole my suggestion
is
 
I thought your solution was: to look for a 0, and than look if the character
before that 0 is on another line.

The fact is: my records (which have mostly a lenght of 128 of 360
characters) contain a whole bunch of 0's, hehe. Not to say that the 0 is the
most used character, hahaha :-)

1 file contains +- 5000 records, and 1 until 10 records start with a 0.

But I have now a solution that doesn't take any time and works perfect (see
somewhere else in this thread). It wasn't really the most 'beautiful'
solution, but it had with a lot of advance the best result :-)

Thanks anyway,

Pieter

100 said:
Yes, you are right there is no way to find char index from line. It looks
like it meant to be used only for reporting the caret position in the status
bar. So I give up the idea. But the second one... You said it is too slow.
And you said that it takes 10 second for the first 1500 records. Of cource I
have no idea what the structure of the file looks like, however, my idea is
to start seaching from the current position of the caret to the next line.
If the next line means the next record it doesn't have to look for all 1500
records to find the begining of the next line. Or may be one line has many
records... So how big one line can be? How many '0' one line can have? And
is it only the '0' that marks the begining of the line?

I agree that if the edit control is readonly caching the text is the best
solution. In this case you can optimize the searching verry well.

B\rgds
100
DraguVaso said:
Hi,

Your second idea takes to much time, hehe. There are too many 0's in the
files. After 10 seconds it has only looked into 1500 records.

Regarding your first solution: that works ideal to find the current line,
but unfortunately there doesn't seem to be a functionthat does the reverse:
when I found my 0-line: giving me the position so I can set the
SelectionStart to it. In case you would know a way to get aroudn this
problem, it woudl be nice if you could tell me.

Thanks a lot!

Pieter

This line
has
create
a
form with one button and one RichTextBox on it and paste the following
button's event handler.
BTW the example is in c#, but the translation should be a piece of cake.

private void button1_Click(object sender, System.EventArgs e)
{

int index = richTextBox1.Find(new char[]{'0'},
richTextBox1.SelectionStart + 1);
if(index >= 0)
{
int foundTextLine = richTextBox1.GetLineFromCharIndex(index);
if(foundTextLine > richTextBox1.GetLineFromCharIndex(index) -1)
{
file://Begining of the line found
richTextBox1.SelectionStart = index;
}

}
file://Just to show the caret.
richTextBox1.Focus();

}


BTW, is your TextBox readonly or it can be change by the user? If it is
readonly you can cache the text and do the searching in the copy. Reading
Lines property is slow operation because it envolves sending the
underlaying windows control messages and then split the text into lines.


Hi DraguVaso,

If it is possible and if you don't like using P\Invole my suggestion
is
to
go with RichEditBox control.
RichEditBox supports methods like GetCharIndexFromPosition and
GetLineFromCharIndex. Combining these methods will give you what you need.
I believe changing EditBox with RichEditBox won't be a pain.

B\rgds
100


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 that difficult, but to find
the
next
line is more difficult. For exemple: if my cursor is on line 200,
it
has
to
start searching on line 201, and not on line 1.

Anybody has any ideas?

I guess that posotioning the cursor on the lien I foudn is also
something
I
will need, so if somebody knows how to do that... :-)

Thansk a lot in advance,

Pieter
 
Back
Top