Problem with GetCharIndexFromPosition()


M

MySelf

Hello,

I am working on an application including a drag and drop feature
between a TreeView and a RTF Text box. But I have a problem : when
there is already a string in the RTF Text Box ("ABCDEF" for example),
and if I drop the new string (coming from TreeView) at the end of the
existing string (ie just after the "F"), the new string is inserted
just before it (ie between the "E" and the "F"), because the
GetCharIndexFromPosition() function does not return 6 but 5, and so the
insertin is made one char before.

Is this a known bug, or is there a bug in my code ? Thank you for your
help.


Here is the code I use to insert the string dragged from the treeview
on the RTF Text Box :

void richTextBoxMessageContent_DragDrop(object sender, DragEventArgs
e)
{
string newText = (string) e.Data.GetData(typeof
(System.String));
Point pc = richTextBoxMessageContent.PointToClient(new
Point(e.X,e.Y));


var index =
richTextBoxMessageContent.GetCharIndexFromPosition(pc);
var l = richTextBoxMessageContent.TextLength;

richTextBoxMessageContent.Text =
richTextBoxMessageContent.Text.Insert(index, newText);

richTextBoxMessageContent.Focus();

}
 
Ad

Advertisements

M

Marcel Overweel

MySelf said:
Hello,

I am working on an application including a drag and drop feature between a
TreeView and a RTF Text box. But I have a problem : when there is already
a string in the RTF Text Box ("ABCDEF" for example), and if I drop the new
string (coming from TreeView) at the end of the existing string (ie just
after the "F"), the new string is inserted just before it (ie between the
"E" and the "F"), because the GetCharIndexFromPosition() function does not
return 6 but 5, and so the insertin is made one char before.

Is this a known bug, or is there a bug in my code ? Thank you for your
help.
Hiya!

It's not a bug. The index returned from GetCharIndexFromPosition()
is zero based, so if you are inserting at the index of the last character,
you are inserting it -before- the last character.

It's like placing the caret at position X meaning that it appears in front
of the character at position X, not after.

From the help file:

String.Insert Method
Inserts a specified instance of String at a specified index position in this
instance.

Remarks
If startIndex is equal to the length of this instance, value is appended to
the end of this instance.

So just add one to the index obtained from GetCharIndexFromPosition.

Regards,
Marcel
 
M

MySelf

Marcel Overweel a présenté l'énoncé suivant :
Hiya!

It's not a bug. The index returned from GetCharIndexFromPosition()
is zero based, so if you are inserting at the index of the last character,
you are inserting it -before- the last character.
So just add one to the index obtained from GetCharIndexFromPosition.

Thank you for your answer. By adding 1 to the index, I can insert the
string at the end of the existing text, but now, I have a problem when
I drop the new string in the middle of the existing string, as the
string is inserted one char aftter the selected place.

For example, if the existing text is "ABCDEF" and if I drop the string
between "B" and "C", the string is inserted between "C" and "D". :-(
 
M

Marcel Overweel

MySelf said:
Marcel Overweel a présenté l'énoncé suivant :



Thank you for your answer. By adding 1 to the index, I can insert the
string at the end of the existing text, but now, I have a problem when I
drop the new string in the middle of the existing string, as the string is
inserted one char aftter the selected place.

For example, if the existing text is "ABCDEF" and if I drop the string
between "B" and "C", the string is inserted between "C" and "D". :-(
Hmm, good one.
Maybe a reverse check can solve this.

GetPositionFromCharIndex(): retrieves the location within the
control at the specified character index.

You know the location of the mouse and which character
(index) the rtfbox thinks you are pointing at.

For any characters except the last one, forget about the +1.
That seems to be working without any modifications.

For the last character, by querying the position of that char and
by measuring (or educated guessing) the width of it, it should be
possible to calculate how far the mouse is away from the left or
right edge of that char.
Then you can decide to add one to the index or not.

Looking at MS Word for example, it seems that the centre of a
character is used to determine if the dragged text goes to the
left or to the right, so, in that case, it is required to measure the
real width of the char.

I don't know how GetCharIndexFromPosition() handles this,
so maybe you can use the same logic to improve the default
behaviour a little.

Here's some info about how to string/char measurements:
http://stackoverflow.com/questions/4219798/determine-how-wide-a-rendered-character-is-in-net


Regards,
Marcel
 
M

MySelf

Peter Duniho a émis l'idée suivante :
For better or worse, the GetCharIndexFromPosition() method is working exactly
as it's supposed to.
Thank you Marcel and Pete for taking the time to answer my question.

Obviously, I'll have to find a way to manage this problem. But with
your help and remarks, things are better understandable now. :)
 
Ad

Advertisements

Joined
Jul 31, 2015
Messages
1
Reaction score
0
I know this thread is very old, but this solves the problem (in VB):

Private Sub FilenameTextBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FilenameTextBox.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
With FilenameTextBox
.Focus()
Dim TextSize As Size = TextRenderer.MeasureText(CType(sender, TextBox).Text, CType(sender, TextBox).Font)
If TextSize.Width < e.Location.X Then
.SelectionStart = .GetCharIndexFromPosition(e.Location) + 1
Else
.SelectionStart = .GetCharIndexFromPosition(e.Location)
End If
.SelectionLength = 0
End With
End If
End Sub
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top