实现一个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);
 }
}

posted on 2017-11-04 22:56  一点飞鸿  阅读(464)  评论(0编辑  收藏  举报