< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

实现一个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   一点飞鸿  阅读(469)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示