Control.DragDrop イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public: event DragEventHandler^ DragDrop { void add (DragEventHandler^ value); void remove (DragEventHandler^ value); }

DragEventArgs の X プロパティおよび Y プロパティは、クライアント座標ではなく、画面座標の値です。次の Visual C# のコードでは、これらのプロパティをクライアントの Point に変換します。
![]() |
---|
Microsoft .NET Framework version 2.0 より前のバージョンでは、DragEnter イベントと DragDrop イベントを備えた UserControl を Windows フォーム上に配置し、デザイン時にその UserControl に対して、なんらかのデータをドラッグ アンド ドロップした場合、DropDrop イベントと DropEnter イベントが発生します。ただし、ソリューションを一度閉じてから再び開くと、DragEnter イベントと DragDrop イベントは発生しなくなります。 |

2 つの ListBox コントロールの間でドラッグ アンド ドロップ操作を実行するコード例を次に示します。この例では、ドラッグ アクションが開始したときに DoDragDrop メソッドが呼び出されます。ドラッグ操作は、MouseDown イベント実行中のマウス位置から SystemInformation.DragSize を超えてマウスが移動したときに開始されます。IndexFromPoint メソッドは、MouseDown イベントで、ドラッグする項目のインデックスを判別するために使用します。
この例では、ドラッグ アンド ドロップ操作でカスタム カーソルを使用する方法についても示します。この例では、2 つのカーソル ファイル (3dwarro.cur と 3dwno.cur) がアプリケーション ディレクトリ内に存在している必要があります。なお、それぞれのファイルはドラッグ用のカスタム カーソルとドロップなしのカスタム カーソルを表します。カスタム カーソルは、UseCustomCursorsCheckCheckBox がオンになっている場合に使用されます。カスタム カーソルは、GiveFeedback イベント ハンドラで設定されます。
キーボードの状態は、右側の ListBox の DragOver イベント ハンドラで評価されます。ドラッグ操作の内容は、Shift キー、Ctrl キー、Alt キー、または Ctrl + Alt キーの状態によって決まります。ドロップが発生する ListBox 内の位置は、DragOver イベント時にも判定されます。ドロップするデータが String でない場合は、DragEventArgs.Effect の値が DragDropEffects で None に設定されます。最後に、ドロップのステータスが DropLocationLabelLabel に表示されます。
右側の ListBox にドロップするデータは、DragDrop イベント ハンドラで判定されます。また、String 値が ListBox の該当する場所に追加されます。ドラッグ操作がフォームの範囲を超えて移動した場合、ドラッグ アンド ドロップ操作は QueryContinueDrag イベント ハンドラでキャンセルされます。
DragDrop イベントの使用方法を次のコード例に示します。コード例全体については、DoDragDrop メソッドのトピックを参照してください。
Private Sub ListDragTarget_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragDrop ' Ensures that the list item index is contained in the data. If (e.Data.GetDataPresent(GetType(System.String))) Then Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object) ' Perform drag-and-drop, depending upon the effect. If (e.Effect = DragDropEffects.Copy Or _ e.Effect = DragDropEffects.Move) Then ' Insert the item. If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item) Else ListDragTarget.Items.Add(item) End If End If ' Reset the label text. DropLocationLabel.Text = "None" End If End Sub
private void ListDragTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { // Ensure that the list item index is contained in the data. if (e.Data.GetDataPresent(typeof(System.String))) { Object item = (object)e.Data.GetData(typeof(System.String)); // Perform drag-and-drop, depending upon the effect. if (e.Effect == DragDropEffects.Copy || e.Effect == DragDropEffects.Move) { // Insert the item. if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item); else ListDragTarget.Items.Add(item); } } // Reset the label text. DropLocationLabel.Text = "None"; }
void ListDragTarget_DragDrop( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ e ) { // Ensure that the list item index is contained in the data. if ( e->Data->GetDataPresent( System::String::typeid ) ) { Object^ item = dynamic_cast<Object^>(e->Data->GetData( System::String::typeid )); // Perform drag-and-drop, depending upon the effect. if ( e->Effect == DragDropEffects::Copy || e->Effect == DragDropEffects::Move ) { // Insert the item. if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches ) ListDragTarget->Items->Insert( indexOfItemUnderMouseToDrop, item ); else ListDragTarget->Items->Add( item ); } } // Reset the label text. DropLocationLabel->Text = "None"; }
private void listDragTarget_DragDrop(Object sender, System.Windows.Forms.DragEventArgs e) { // Ensure that the list item index is contained in the data. if (e.get_Data().GetDataPresent(String.class.ToType())) { Object item = (Object)(e.get_Data().GetData( String.class.ToType())); // Perform drag-and-drop, depending upon the effect. if (e.get_Effect().Equals(DragDropEffects.Copy) || e.get_Effect().Equals(DragDropEffects.Move)) { // Insert the item. if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) { listDragTarget.get_Items().Insert( indexOfItemUnderMouseToDrop, item); } else { listDragTarget.get_Items().Add(item); } } } // Reset the label text. dropLocationLabel.set_Text("None"); } //listDragTarget_DragDrop

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からControl.DragDrop イベントを検索する場合は、下記のリンクをクリックしてください。

- Control.DragDrop イベントのページへのリンク