Insert string on RTF TextBox after Drag and Drop

M

MySelf

Hello,


I have another question on my Drag and Drop from Treeview to RTF
textbox.

For the moment, I can only insert the dropped string at the end of the
current text alredy in RTF Textbox.


What I'd like to do is insert the dropped text at the exact point where
it was dropped (in the text that's already in RTF Textbox, of course).

I have been looking at the Drag and drop events, but with no success.
It seems that when dropping the string on the RTF TextBox, I only get
the graphical position of the drop (a Point with X and Y values) but
not the text character where it was dropped and should be inserted.

Can you help me ? Thank you.
 
M

MySelf

Peter Duniho a émis l'idée suivante :
[...]
What I'd like to do is insert the dropped text at the exact point where
it was dropped (in the text that's already in RTF Textbox, of course).

I have been looking at the Drag and drop events, but with no success. It
seems that when dropping the string on the RTF TextBox, I only get the
graphical position of the drop (a Point with X and Y values) but not the
text character where it was dropped and should be inserted.

Can you help me ? Thank you.

The drag-and-drop API works with a wide variety of object types, most of
which are nothing like a text box. It would make no sense for the
drag-and-drop API itself to have anything that would help map cursor
coordinates to text insertion points.

The TextBoxBase class itself, on the other hand, has what you're looking for.
See, for example, TextBoxBase.GetCharIndexFromPosition().

Pete

Thank you for your answer. I would like to insert the dropped string
just at the point of the RTFTextBox I point with the mouse when
dropping the string.

Below is the code I use for inserting the dropped string on the text of
my RTFTextBox, but I find that the index value is always 0, after using
GetCharIndexFromPosition(). :

void richTextBoxMessageContent_DragDrop(object sender, DragEventArgs
e)
{
string newText = (string) e.Data.GetData(typeof
(System.String));

var index =
richTextBoxMessageContent.GetCharIndexFromPosition(new Point(e.X,
e.Y));
richTextBoxMessageContent.Text =
richTextBoxMessageContent.Text.Insert(index, newText);
richTextBoxMessageContent.Focus();
}

Can you find a reason why ? Thank you.
 
M

MySelf

Après mûre réflexion, Peter Duniho a écrit :
[...]
Below is the code I use for inserting the dropped string on the text of
my RTFTextBox, but I find that the index value is always 0, after using
GetCharIndexFromPosition(). :

void richTextBoxMessageContent_DragDrop(object sender, DragEventArgs e)
{
string newText = (string) e.Data.GetData(typeof (System.String));

var index = richTextBoxMessageContent.GetCharIndexFromPosition(new
Point(e.X, e.Y));
richTextBoxMessageContent.Text =
richTextBoxMessageContent.Text.Insert(index, newText);
richTextBoxMessageContent.Focus();
}

Can you find a reason why ? Thank you.

The X and Y coordinates in the DragEventArgs class are in screen coordinates.
But the GetCharIndexFromPosition() method requires the coordinates to be
passed as client coordinates. You need to convert the values before using
them.

See
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient.aspx

Pete

Thank you, it works much better now. Except that sometimes,
GetCharIndexFromPosition() gives me a value superior by 3 of that
expected value : If I drop my string value on 5th character of
RTFtextbox, it gives me the value of 8. Is there a reason for this ?
 
M

MySelf

Peter Duniho a exprimé avec précision :
I'm sure there is. Without a concise-but-complete code example that reliably
reproduces the problem, I'm sure I don't know what that reason actually might
be.

:)
Here is the exact function used for that :

void richTextBoxMessageContent_DragDrop(object sender,
DragEventArgs e)
{
string newText = (string)
e.Data.GetData(typeof(System.String));

Point pc = PointToClient(new Point(e.X,e.Y));

var index =
richTextBoxMessageContent.GetCharIndexFromPosition(pc);

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

}
 
M

MySelf

Peter Duniho a formulé ce jeudi :
That said, based on the code you posted, it appears to me that you are using
the wrong Control instance for your call to PointToClient(). The client
coordinate system you need for the call to GetCharIndexFromPosition() is the
one for the RichTextBox instance of interest, not the Form instance that
contains it.

When I'm trying to understand why a piece of code is not working
properly, I divide it in "elementary" lines of code so I can check how
each line is working. When I get it working, I can make it more
concise. :)

Thank you very much for your answer, everything works perfectly now !
:)
 

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