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 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
has
to something