C#中拖放操作 C#高手请进

[复制链接]
查看11 | 回复3 | 2010-10-26 15:44:05 | 显示全部楼层 |阅读模式
有两个TextBox控件TextBox1和TextBox2,要从TextBox1把文字拖放到TextBox2我的代码怎么实现不了,请高人解答
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 拖放操作
{

public partial class Form1 : Form

{

const byte CtrlMask = 8;

public Form1()

{

InitializeComponent();

}

private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)

{

textBox1.SelectAll();

string s = textBox1.SelectedText;

DoDragDrop(s, DragDropEffects.Move | DragDropEffects.Copy);

}

private void textBox2_DragEnter(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.Text))

{

if ((e.KeyState & CtrlMask) == CtrlMask)

{

e.Effect = DragDropEffects.Copy;

}

else

{

e.Effect = DragDropEffects.Move;

}

}

else

{

e.Effect = DragDropEffects.None;

}

}

private void textBox2_DragDrop(object sender, DragEventArgs e)

{

//textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();

}

}
}

回复

使用道具 举报

千问 | 2010-10-26 15:44:05 | 显示全部楼层
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WindowsApplication1{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

回复

使用道具 举报

千问 | 2010-10-26 15:44:05 | 显示全部楼层
你可以在类初始化时 声明一个字符串 str 来 临时存储 要转移的数据。当双击 tbox1 时把里面的字符 赋值给 str,当执行 tbox2 的 拖放动作后 把 tbox2的 text 赋值成 str。 逻辑不清晰,不了解事件机制,copy代码是很难进步的
回复

使用道具 举报

千问 | 2010-10-26 15:44:05 | 显示全部楼层
你写的东西出什么BUG了?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行