Windows Forms: Drag and Drop text files into a RichTextBox in C#

This post shows you how to drag and drop text files into a RichTextBox in C# Windows Forms Application.

To create a simple demo in c# drag and drop text within a RichTextBox, you can drag the RichTextBox control from the visual studio toolbox to your winform as shown below.

drag files into richtextbox c#

Add the load event handler to your form to allow you to drag and drop files into RichTextBox control as the following c# code.

private void Form1_Load(object sender, EventArgs e)
{
    richTextBox1.AllowDrop = true;
    richTextBox1.DragDrop += RichTextBox1_DragDrop;
}

Add the DragDrop event handler to RichTextBox allowing you to read the file from your disk as shown below.

private void RichTextBox1_DragDrop(object sender, DragEventArgs e)
{
    var data = e.Data.GetData(DataFormats.FileDrop);
    if (data != null)
    {
        var fileNames = data as string[];
        if (fileNames.Length > 0)
            richTextBox1.LoadFile(fileNames[0]);
    }
}

Press F5 to run your application, then try to drag and drop the files into RichTextBox control.

VIDEO TUTORIAL