首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 拖放技術的典型應用

     編輯:關於C語言

WinForms 拖放基礎實現:TextBox 間文字拖放完整步驟

開發步驟

  1. 新增表單,放置兩個 Label、兩個 TextBox (txtDataTarget、txtSource)
  2. 設定 txtDataTarget.AllowDrop = true
  3. 實現三個核心事件

核心代碼


private void txtDataTarget_DragDrop(object sender, DragEventArgs e) {
    txtDataTarget.Text = e.Data.GetData(DataFormats.Text).ToString();
}

private void txtDataTarget_DragEnter(object sender, DragEventArgs e) {
    e.Effect = DragDropEffects.Copy; // 顯示複製圖標
}

private void txtSource_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";
        Cursor MyNoDropCursor = new Cursor(reportPath);
        DragDropEffects dropEffect = this.txtSource.DoDragDrop(this.txtSource.Text, 
            DragDropEffects.Copy | DragDropEffects.Link);
    }
}

關鍵點:

  • AllowDrop 啟用放置目標
  • DragEnter 設定 e.Effect 回饋視覺提示
  • DoDragDrop 發起拖放,指定資料與允許效果
  • 自訂游標載入路徑處理
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved