PavelTorgashov / FastColoredTextBox

Fast Colored TextBox for Syntax Highlighting. The text editor component for .NET.
Other
1.21k stars 463 forks source link

Drag Drop events don't work on FastColoredTextBoxes #250

Open voidZiAD opened 2 years ago

voidZiAD commented 2 years ago

When using FastColoredTextbox, I'm not able to make the DragDrop or DragEnter or DragOver events work. Basically, when I put in some code inside the Events nothing changes, I've looked all over the internet to try making drag and drop work however it still does not.

Any solutions or any way this can be fixed for FastColoredTextbox?

psychosan commented 1 year ago

Greetings,

Hope this helps someone out.

  1. Make sure you are Not running your IDE as Admin (assuming Visual Studio here).
  2. Below is a working example. The FastColoredTextBox is named SqlCmdWindow.

private void SqlCmdWindow_DragOver(object sender, DragEventArgs e) { // Check if the format of the data can be accepted // Only accept file drops from Explorer, etc. var ccx = StringComparison.InvariantCultureIgnoreCase; string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false); var filePath = files.FirstOrDefault();

if (filePath != null && (filePath.EndsWith(".sql", ccx) || filePath.EndsWith(".txt", ccx)))
    e.Effect = DragDropEffects.Link;
else
    e.Effect = DragDropEffects.None;

}

private void SqlCmdWindow_DragDrop(object sender, DragEventArgs e) { // Extract the data from the DataObject-Container into a string list var ccx = StringComparison.InvariantCultureIgnoreCase; string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false); var filePath = files.FirstOrDefault();

if (filePath != null && (filePath.EndsWith(".sql", ccx) || filePath.EndsWith(".txt", ccx)))
{
    TitleMsg.Text = filePath;                
    var fileText = File.ReadAllText(filePath);

    // Add file text into FastColoredTextBox control:
    SqlCmdWindow.Text = fileText;
}

}



Hope this helps : )