Drag drop file Text vào RichTextBox trên C# winform.[C#] Hướng dẫn Drag and Drop File vào RichTextBox
Dưới đây là hình ảnh demo mình kéo file text vàoRichTextBox.Source code C#:
namespace Drag_Drop_RichTextBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.AllowDrop = true;
richTextBox1.DragDrop += RichTextBox1_DragDrop;
}
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) {
string myText = File.ReadAllText(fileNames[0], Encoding.UTF8);
richTextBox1.Text = myText;
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://laptrinhvb.net");
}
}
}
Các bạn chỉ cần Enable thuộc tính
AllowDrop = true.và viết sự kiện
drapdrop trên richtextbox như source code ở trên.Chúc các bạn thành công với thủ thuật trên.
Theo LapTrinhVB.Net
![[CSHARP] Drag drop file to RichTextBox [CSHARP] Drag drop file to RichTextBox](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikBuRmyW56tbI5IRTHm6bCy0DL5spLoNpQ2wPvn6jj7Uhxw-WKrfKfCRc0ca5payOZ4ufgm5Kt2JQkvx7hbZqvxn3lmcrerh-uRMeMu329UuR_hPaKYrNv9-l1Z3CIwcOJaGJGdh_s5-hZhQJ94Yec5A9uJdemuIkFCpucMqG0Ii_zXlAiCbyhqF5u4jU/s16000/%5BCSHARP%5D%20Drag%20drop%20file%20to%20RichTextBox.png)


Comments