实现控件的拖放
最近,在学习Windows界面的开发,顺便总结了一点小东西,就把它记到我的blog里了。
呵呵,看起来很简单,不过可能会很有用。
首先在窗体中添加两个文本框 textbox1 和textbox2, 分别在属性框中双击其各自对应的事件,在代码页面中添加如下代码:
//在form_load 中添加
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
this.textBox1.Cursor = Cursors.SizeAll;
this.textBox1.DoDragDrop(this.textBox1.Text, DragDropEffects.Move );
}
private void textBox2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
this.textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();
}
}
private void textBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move ;
}
这样运行后,拖动textbox1中输入的置textbox2中 就可以看到拖放的效果。以此类推,可以应用的其他的控件上。