示例说明
将ListBox一行数据拖到TextBox中指定光标位置。涉及事件有:
- ListBox的MouseDown,用于鼠标单击选中数据
- TextBox的DragEnter,用鼠标将数据拖到TextBox时发生,复制数据
- TextBox的DragDrop,鼠标拖放完成时发生,粘贴插入数据
示例代码
private void ListBox_MouseDown(object sender, MouseEventArgs e)
{
var index = lb.IndexFromPoint(e.X, e.Y);
var item = lb.Items[index].ToString();
txt.DoDragDrop(item, DragDropEffects.Copy);
}
private void TextBox_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void TextBox_DragDrop(object sender, DragEventArgs e)
{
var point = tx.PointToClient(new Point(e.X, e.Y));
var data = e.Data.GetData(typeof(string)) as string;
txt.Text = txt.Text.Insert(txt.SelectionStart, data);
}