C#内TextBox的Drog&Drop拖放操作
其实解决的重点是如何控制即选中文本又可以拖放。
可以这么解决:
1.在TextBox的MouseUp时将选中的文本保存的TextBox.Tag属性中.
2.在MouseDown开始拖放时,然后还原Selection.
我们定义一个类用于记录MouseUp时的状态:
public class DropData
{
public string _text;
public int _start;
public int _len;
public DropData(string text, int start, int len)
{
_text = text;
_start = start;
_len = len;
}
}
{
public string _text;
public int _start;
public int _len;
public DropData(string text, int start, int len)
{
_text = text;
_start = start;
_len = len;
}
}
在TextBox的MouseUp时将选中的文本保存的TextBox.Tag属性中.
private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
if (textBox1.SelectedText != "")
{
DropData d = new DropData(textBox1.SelectedText, textBox1.SelectionStart, textBox1.SelectionLength);
textBox1.Tag = d;
}
}
{
if (textBox1.SelectedText != "")
{
DropData d = new DropData(textBox1.SelectedText, textBox1.SelectionStart, textBox1.SelectionLength);
textBox1.Tag = d;
}
}
在MouseDown开始拖放时,然后还原Selection.现在可能将选中的文本拖放到任一支持AllowDrop的控件中了。
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
if (textBox1.Tag == null) return;
if (textBox1.Tag is DropData)
{
DropData d = textBox1.Tag as DropData;
textBox1.SelectionStart = d._start;
textBox1.SelectionLength = d._len;
textBox1.DoDragDrop(d._text, DragDropEffects.Copy);
}
textBox1.Tag = null; //注意,这里要还原变量,否则无休止托放
}
{
if (textBox1.Tag == null) return;
if (textBox1.Tag is DropData)
{
DropData d = textBox1.Tag as DropData;
textBox1.SelectionStart = d._start;
textBox1.SelectionLength = d._len;
textBox1.DoDragDrop(d._text, DragDropEffects.Copy);
}
textBox1.Tag = null; //注意,这里要还原变量,否则无休止托放
}

参考文档:
C#文件拖放操作(DragDrop File)
C#拖放技术(Drop&Drag)相关方法和事件
C#.NET TreeView拖拽功能(Drag,Drop)
C#拖动鼠标移动Form窗体,移动Panel面板通用类(FormDragDrop)|C/S开发框架
DevExpress SkinItem皮肤选择器(SkinBarSubItem、SkinDropDownButtonItem、SkinPaletteDropDownButtonItem)3个组件使用
C# 拖动鼠标移动窗体背景图片窗体通用类FormDragDrop
C#ResultMonitor类,在TextBox组件显示操作接口
使用FastReport打印RichTextBox RTF 格式的报表
.NET RichTextBox控件使用详解
C# RichTextbox中插入图片
(C#)RichTextBox控件查找文本演示(功能全)
[转帖]FreeTextBox添加自定义按钮
FreeTextBox HTML即时输入组件太好太强大!
MDI程序子窗体的拖放功能(Drag,Drop)
[转帖]C#如何实现DataGridView到DataGridView的拖拽
详解DevExpress.LookUpEdit控件实现自动搜索定位功能|C/S开发框架
C#文件拖放操作(DragDrop File)
C#拖放技术(Drop&Drag)相关方法和事件
C#.NET TreeView拖拽功能(Drag,Drop)
C#拖动鼠标移动Form窗体,移动Panel面板通用类(FormDragDrop)|C/S开发框架
DevExpress SkinItem皮肤选择器(SkinBarSubItem、SkinDropDownButtonItem、SkinPaletteDropDownButtonItem)3个组件使用
C# 拖动鼠标移动窗体背景图片窗体通用类FormDragDrop
C#ResultMonitor类,在TextBox组件显示操作接口
使用FastReport打印RichTextBox RTF 格式的报表
.NET RichTextBox控件使用详解
C# RichTextbox中插入图片
(C#)RichTextBox控件查找文本演示(功能全)
[转帖]FreeTextBox添加自定义按钮
FreeTextBox HTML即时输入组件太好太强大!
MDI程序子窗体的拖放功能(Drag,Drop)
[转帖]C#如何实现DataGridView到DataGridView的拖拽
详解DevExpress.LookUpEdit控件实现自动搜索定位功能|C/S开发框架
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
在线客服
