Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello There I want to make when i drag a .txt file into FastColoredTextBox the file.txt txt will be in the FastColoredTextBox Please Help

What I have tried:

I have tried doing it but instead of giving me the text inside the file it gives me the file path i dont want that i want the txt inside the file here is the code
C#
private void fastColoredTextBox1_DragOver(object sender, DragEventArgs e)
{
   files = (string[])e.Data.GetData(DataFormats.FileDrop);
   if (Path.GetExtension(files[0]) == ".txt" && files.Count()==1)
   {
      e.Effect = DragDropEffects.All;
   }
}

private void fastColoredTextBox1_DragDrop(object sender, DragEventArgs e)
{
   string textFg = File.ReadAllText(files[0]);
   string text = File.ReadAllText(files[0]);
   fastColoredTextBox1.Text = text;
}
Posted
Updated 9-Mar-20 9:51am
v2
Comments
RamiroX 9-Mar-20 4:33am    
Do you set the property "AllowDrop" of the cdontrol to true ?
Please visit https://stackoverflow.com/questions/9464001/drag-and-drop-file-into-textbox/21978057
ZurdoDev 9-Mar-20 8:53am    
fastColoredTextBox looks like a textbox control. So, it does not likely support dragging files directly and it reading the contents, you'll likely have to write the code yourself, which you have shown, so what is your question?

1 solution

There are several cases which you haven't considered where you code will currently crash. Start by fixing those:
C#
private void fastColoredTextBox1_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.None;
    
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files.Length == 1 && string.Equals(Path.GetExtension(files[0]), ".txt", StringComparison.OrdinalIgnoreCase))
        {
            e.Effect = DragDropEffects.All;
        }
    }
}

private void fastColoredTextBox1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files.Length == 1 && string.Equals(Path.GetExtension(files[0]), ".txt", StringComparison.OrdinalIgnoreCase))
        {
            fastColoredTextBox1.Text = File.ReadAllText(files[0]);
        }
    }
}
If it still doesn't work, then you'll need to log a bug on the GitHub project:
GitHub - PavelTorgashov/FastColoredTextBox: Fast Colored TextBox for Syntax Highlighting. The text editor component for .NET.[^]

Or post a question in the forum at the bottom of the CodeProject article:
Fast Colored TextBox for Syntax Highlighting[^]
 
Share this answer
 
Comments
Maciej Los 10-Mar-20 2:59am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900