You want to dragging text file to RichTextBox Control,this topic is for you
"How to drop a text file into a RichTextBox Control"
Drag and drop is a common feature in many applications that allows users to move or copy data from one location to another using a mouse or a touch screen. In this article, you will learn how to implement drag and drop functionality for text files in a RichTextBox Control, which is a Windows Forms control that provides rich text editing and formatting capabilities. You will also learn how to handle different scenarios, such as invalid file types, multiple files, or large files, and how to display the imported text in the RichTextBox Control. By following the steps in this article, you will be able to create a user-friendly and interactive application that can load text files by dragging them to a RichTextBox Control.
Explore My Other Channel for More Cool and Valuable Insights
π Youtube Learn Tech Tipsπ Tiktok
π Facebook:- If you choose EnableAutoDragDrop == true, you'll see the icon when you drag any file to RichTextBox
- So, I choose False for EnableAutoDragDrop for don't see icon
Description
- We using DrapDrop Event in RichTextBox and using AllowDrop properties = true
- list[0] is filename path
- RichTextBoxStreamType.PlainText is file text format
- e.Data.GetData("FileDrop"): the file which you drag into RichTextBox Control
Source Code
using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace demo
{
public partial class frmMain : From
{
public frmMain()
{
InitializeComponent();
rtbReward.DragDrop += new DragEventHandler(rtbReward_DrapDrop);
rtbReward.AllowDrop = true;
}
private void rtbReward_DrapDrop(object sender, DragEventArgs e)
{
try
{
object filename = e.Data.GetData("FileDrop");
if (filename != null)
{
var list = filename as string[];
if (list != null && !string.IsNullOrEmpty(list[0]))
{
rtbReward.Clear();
rtbReward.LoadFile(list[0],
RichTextBoxStreamType.PlainText);
}
}
}
catch
{
}
}
}
}