C# - How To Drag And Drop Text In C#
Source Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Drag_Drop_Text : Form
{
public Drag_Drop_Text()
{
InitializeComponent();
}
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
TextBox tb = (TextBox)sender;
tb.SelectAll();
tb.DoDragDrop(tb.Text, DragDropEffects.Copy);
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Text = (string)e.Data.GetData(DataFormats.Text);
}
private void Drag_Drop_Text_Load(object sender, EventArgs e)
{
textBox2.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox2.MouseDown += new MouseEventHandler(textBox1_MouseDown);
textBox2.DragDrop += new DragEventHandler(textBox1_DragDrop);
textBox3.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox3.MouseDown += new MouseEventHandler(textBox1_MouseDown);
textBox3.DragDrop += new DragEventHandler(textBox1_DragDrop);
}
}
}
=> OutPut :
c# drag and drop text |