DragDrop with RichTextBox under .net 1.1

R

Rolf Welskes

Hello,
I have tried to implement dragdrop for RichTextBox under .Net 1.1.
IMPORTANT: it must be .NET 1.1 without any service pack, because of the
distribution worldwide. 2.0 is not possible.

I have implemented a sample I found.
First there should be RichTextBox.DragEnter and RichTextBox.DragDrop events.
But it is not there. Referenece says only for internal use.
So I have implemented it bei override OnDragEnter, OnDragDrop in a derived
class from the RichTextBox-Class.

//AllowDrop is set to true.

protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter (e);

if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy ;
else
e.Effect = DragDropEffects.None ;
}

protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);

int i;
String s;

// Get start position to drop the text.
i = this.SelectionStart;
s = this.Text.Substring(i);
this.Text = this.Text.Substring(0,i);

// Drop the text on to the RichTextBox.
this.Text = this.Text +
e.Data.GetData(DataFormats.Text).ToString();
this.Text = this.Text + s;

}

this works a little, but is very indifferent.
First you start to drag and see only the non-cursor on source and on target.
Then you make the same and on the source a drag is done without to leave the
mouse button.
Then it works for a bit of text. Then again the same.
In my sample I have 2 overriden RichtTextBox-Objekts of this class. Each is
source AND target.

So this is not usable.
What is the right way.
Thank You.

Rolf
 
L

Linda Liu [MSFT]

Hi Rolf,

RichTextBox has DragEnter and DragDrop events in .net 1.0, .net 1.1 and
..net 2.0. Although you couldn't see these events in Intellisense,
RichTextBox does have these two events.

Suspose you have drag&drop RichTextBox onto a form named richTextBox1. You
could add the following two sentences in the form's constructor or Load
event handler to subscribe the two events of richTextBox1.

this.richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
this.richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);

Then you should add the corresponding handlers for these two events in the
form.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you.

I have tried the following sample from msdn (dot net 1.1 !).
private void TestPanel_Load(object sender, System.EventArgs e)
{
richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
}

private void richTextBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy ;
else
e.Effect = DragDropEffects.None ;

}

private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
int i;
String s;

// Get start position to drop the text.
i = richTextBox1.SelectionStart;
s = richTextBox1.Text.Substring(i);
richTextBox1.Text = richTextBox1.Text.Substring(0,i);

// Drop the text on to the RichTextBox.
richTextBox1.Text = richTextBox1.Text +
e.Data.GetData(DataFormats.Text).ToString();
richTextBox1.Text = richTextBox1.Text + s;

}

It's very simple, but does not work.
If you drag from another richtextbox on this with this implementation,
you see the DragDropEffects.non cursor and the text is not dropped.
On the otther hand in the richtextbox which is the dragdrop source
nonsence take place, the selection disappears, the caret goes to the start
of the line.

Seems, that in msdn are samples, which nobody ever has realy run.

Thank you.
Rolf
 
L

Linda Liu [MSFT]

Hi Rolf,

Do you mean that you could compile the sample project successfully and run
it? If yes, it's sure that there's no compilation error in the project.

Since the sample code only handles the DropEnter and DropDrop events of
richTextBox1, you should test this program by dragging some text from other
sources and drop it onto the richTextBox1. For example, you could use
WordPad as a drag&drop source.

To open Wordpad, go to Start menu and select Run. In the textbox of Run
window, type "wordpad" and press Enter. Thus, Workpad is opened. You may
type some text in the Wordpad, and drag&drop the text onto the richTextBox1
when the program is running.

Please try my suggestion and see if the drag&drop operation work when you
use Wordpad as the source.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
R

Rolf Welskes

Hello,
you are right,
from word pad to the implemented richtextbox I can drag a text. oK.

BUT
I need only the way to drag from an own richtextbox to another own
richtextbox.

Is this not possible.

Thank you.
Rolf
 
L

Linda Liu [MSFT]

Hi Rolf,

You could handle the RichTextBox's MouseMove event and call the DoDragDrop
method of the RichTextBox in this event handler.

Below is a sample.

private void richTextBox1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.richTextBox1.DoDragDrop(this.richTextBox1.Text,DragDropEffects.Copy);
}
}

Thus, you could drag the text in the richTextBox1 and drop it onto other
richTextBox whose DragEnter and DragDrop events have been handled.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hello Rolf,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If the problem is not resolved or you have anything unclear, please feel
free to post in the newsgroup and we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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