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
Comments