1. 程式人生 > >C# 拖拽事件

C# 拖拽事件

typeof eof 代碼 txt odr agen rgs present ext

實現一個textBox像另一個TextBox拖拽數據。

一個控件想要被拖入數據的前提是AllowDrop屬性為true。

所以,首先需要將被拖入數據的textBox的AllowDrop屬性設置為True;

txt1為原textBox名稱,txt2為要拖入數據的TextBox名稱。

代碼如下:

private void txt_DragDrop(object sender, DragEventArgs e)
{
string hit = (string)e.Data.GetData(typeof(string));
txt.Text = hit;
}

private void txt_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}


private void txt1_MouseDown(object sender, MouseEventArgs e)
{
if (txt1.Text.Trim() != "")
{
string s = txt1.Text;
txt1.DoDragDrop(s, DragDropEffects.Copy);
}
}

C# 拖拽事件