How to drag and drop?
This code helps to copy or Move user's text from one text box to another text box. In this resource user can get help to drag and drop text. In this resource user can copy text by pressing CTRL key and then drag that text.
This code helps to drag and drop functionality in .net
Through this code user can drag and drop text from one TextBox To another TextBox. Here We take two textboxes named txtFrom and txtTo.
You have to allowdrop Property true in both textboxes. And here We take a constant variable like
const byte Ctrlbyte = 8;
This variable helps to detect CTRL key is pressed or not if CTRL is pressed then drag and drop operation will perform COPY otherwise perform MOVE.
Then first create an MouseDown event of the txtFrom like this
private void txtFrom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
And Then On txtFrom_MouseDown event write this code
if (e.Button == MouseButtons.Left)
{
txtFrom.SelectAll();
txtFrom.DoDragDrop(txtFrom.SelectedText, DragDropEffects.Copy | DragDropEffects.Move);
}
In Above code first we check which mouse button is pressed. If left Mouse Button is pressed then it will go inside the code. Then
txtFrom.SelectAll();
This code means select all text which is in txtFrom. And then invoke operation of drag and drop
txtFrom.DoDragDrop(txtFrom.SelectedText, DragDropEffects.Copy | DragDropEffects.Move);
Then after create an DragEnter event of txtTo like
private void txtTo_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
}
And then write this code
if (e.Data.GetDataPresent(DataFormats.Text))
{
if ((e.KeyState & Ctrlbyte) == Ctrlbyte)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
Here first we checked the type of the content which is draged. If it is corret type for this control then it will be perform COPY or MOVE. Otherwise it
will be rejected. Here we checked while doing drag and drop operation CTRL key is pressed or not if CTRL is pressed then it will perform
COPY operation otherwise it will perform MOVE Operation.
And then create DragDrop event of txtTo Like this
private void txtTo_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
}
And then write this code
txtTo.Text = e.Data.GetData(DataFormats.Text).ToString();
if ((e.KeyState & Ctrlbyte) != Ctrlbyte)
{
txtFrom.Text = "";
}
in above code we paste the data which is dragged and drop in txtTo textbox and then checked is CTRL pressed or not. If pressed then it will be operate copy
other wise operate move. The full code of above functionality is as underFull Code
const byte Ctrlbyte = 8;
private void txtFrom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
txtFrom.SelectAll();
txtFrom.DoDragDrop(txtFrom.SelectedText, DragDropEffects.Copy | DragDropEffects.Move);
}
}
private void txtTo_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
if ((e.KeyState & Ctrlbyte) == Ctrlbyte)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.Move;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void txtTo_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
txtTo.Text = e.Data.GetData(DataFormats.Text).ToString();
if ((e.KeyState & Ctrlbyte) != Ctrlbyte)
{
txtFrom.Text = "";
}
}