HomeHomeDigitalRune Sof...DigitalRune Sof...Text Editor Con...Text Editor Con...Drag and DropDrag and Drop
Previous
 
Next
New Post
8/29/2010 2:06 AM
 
Hi Helmut,

First allow me to explain my setup:
I have a main form with a tab control on it. Each tab page contains a user control called ucTextArea. The tab pages are added dynamically whenever a user opens a new document. The ucTextArea user control contains the digital rune control. Doing it this way allow users to have multiple documents open at once on different tabs. This works great, but I want to enable my users to drag a text file from their "My Documents" folder onto the text area and it should open the file automatically. I'm having some problems with this.

Here's my code so far:
Firstly, I've set the Digital Rune control's AllowDrop property to True. Then I created two events, one called textEditorControl_DragDrop and the other is called textEditorControl_DragEnter, see the code for these events below:

private void textEditorControl_DragDrop(object sender, DragEventArgs e)
{
try
{
Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
if (a != null)
{
string s = a.GetValue(0).ToString();
//Not sure if the blow is correct
frmMain.ActiveForm.Activate();

Open(s);
}
}
catch (Exception ex)
{
MessageBox.Show("Error in DragDrop function: " + ex.Message);
}
}

private void textEditorControl_DragEnter(object sender, DragEventArgs e)
{
// If file is dragged, show cursor "Drop allowed"

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

I also have a function called Open(string sFile), which opens the file:
public void Open(string sFile)
{
try
{
StreamReader StreamReader1 = new StreamReader(sFile);
textEditorControl.Text = StreamReader1.ReadToEnd();
StreamReader1.Close();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error loading from file");
}

}

The problem is that this code just doesn't work. However if I implement the exact same code on the tab control, instead of the textEditorControl, then it works perfectly. But using it this way, the user MUST drag the file and drop on the tab, instead of the textEditor. I want them to be able to drag the file onto the Text Editor.

Do you maybe have any ideas on how to get this implemented properly? Any help would be appreciated.

Regards,
Francois
 
New Post
8/30/2010 9:59 AM
 
The TextEditor Control handles Drag and Drop itself but it only supports strings in the DragDrop data and not files. It seems that this code has precedence over your DragDrop code.

The relevant code is in the file DigitalRune.Windows.TextEditor/GUI/TextAreaDragDropHandler.cs. I think you will have to modify this file directly and add your DragDrop code to the existing code in this file.

By the way, the TextEditorControl has a LoadFile() method which could be used instead of your Open() method.
 
Previous
 
Next
HomeHomeDigitalRune Sof...DigitalRune Sof...Text Editor Con...Text Editor Con...Drag and DropDrag and Drop