在應用程式中,使用者常需將資料從一個控制項拖曳至另一個控制項,此時需使用拖放技術。
txtDataTart(目標)與 txtScoure(來源)。txtDataTart 的 AllowDrop 屬性設為 true。private void txtDataTart_DragDrop(object sender, DragEventArgs e)
{
txtDataTart.Text = e.Data.GetData(DataFormats.Text).ToString();
}
private void txtDataTart_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void txtScoure_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
string reportPath = Application.StartupPath.Substring(0,
Application.StartupPath.Substring(0,
Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\sl3293dwarro.cur";
MyNoDropCursor = new Cursor(reportPath);
DragDropEffects dropEffect = this.txtScoure.DoDragDrop(
this.txtScoure.Text, DragDropEffects.Copy | DragDropEffects.Link);
}
}
AllowDrop = true:啟用控制項接受拖放資料的能力。DragEnter 事件:設定 e.Effect 指示允許的拖放效果(如 Copy、Move、Link)。DragDrop 事件:實際執行資料傳遞,從 e.Data 取得資料。DoDragDrop 方法:啟動拖放作業,傳入要拖曳的資料與允許的效果。MouseMove 中載入 .cur 檔案作為拖曳時的游標。| 效果 | 說明 |
|---|---|
Copy | 複製資料 |
Move | 移動資料 |
Link | 建立連結 |
None | 不允許拖放 |
All | 所有效果 |