2012-03-15 19 views
1

C#winformsアプリケーションからテキストをドラッグドロップする際に、少し問題があります。Cの特定の場所にあるテキストボックスから別の場所に単語をドラッグアンドドロップする方法#

"TextBoxA"からコンテンツをドラッグし、 "TextBoxB"内の特定の場所にドロップします。

(マウスボタンを離した場所に依存して)「ハロービッグ・ワールド」:

TextBoxA.Text = "Big " 
TextBoxB.Text = "Hello World" 

TextBoxAから「ビッグ」をドラッグし、TextBoxBから「Hello World」の間にそれを落とし、TextBoxBは次のように終わるでしょう。

+0

:こんにちはReniuz/ – Reniuz

+0

、ジャスト言葉遣い更新 - 申し訳ありません – user532104

答えて

0

このexampleがお役に立てば幸いです。

+0

おかげアントンを私は後の午前何そのかなりありません。私は、マウスの位置を見つけ出し、文字列内の各文字の幅を計算し、適切な場所に配置しました。 – user532104

0

私はこれが非常に古い質問であると認識していますが、まだ答える価値があると思いました。

下記のコードのコメントは、わかりやすいものです。質問がある場合はお知らせください。あなたはその後、テキストボックスのテキストについては、ファイルをドラッグについて話質問の冒頭で

public Form1() 
    { 
     InitializeComponent(); 

     // Allow TextBoxB to accept a drop. 
     TextBoxB.AllowDrop = true; 

     // Add event handlers to manage the drag drop action. 
     TextBoxB.DragEnter += TextBoxB_DragEnter; 
     TextBoxB.DragDrop += TextBoxB_DragDrop; 
    } 

    void TextBoxB_DragEnter(object sender, DragEventArgs e) 
    { 
     // Update cursor to inform user of drag drop action. 
     if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy; 
    } 

    void TextBoxB_DragDrop(object sender, DragEventArgs e) 
    { 
     // Get the current cursor postion within the control. 
     var point =TextBoxB.PointToClient(Cursor.Position); 

     // Find the character index based on cursor position. 
     var pos = TextBoxB.GetCharIndexFromPosition(point); 

     // Insert the required text into the control. 
     TextBoxB.Text = TextBoxB.Text.Insert(pos, TextBoxA.Text); 
    } 
関連する問題