テキストボックスから他のテキストボックスへのデータのドラッグ
&ドロップは文字の選択状態の判断と、DataObjectの使用がポイント
です。
サンプルでは対照文字を選択しきらないうちにイベントが発生して
しまうのを回避するため、右クリック時でのドラッグ&ドロップ時に
処理を行っています。
(前提)テキストボックス2つ(TextBox1,TextBox2)
'○ユーザフォーム初期化イベント
Private Sub UserForm_Initialize()
TextBox1.Value = "Excel Access PPT Word Outlook"
End Sub
'○テキストボックス1のマウス移動時(ドラッグ開始)
Private Sub TextBox1_MouseMove _
(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
'マウス右クリック時、テキストが選択されている状態で処理
If Button = 2 And TextBox1.SelText <> "" Then
'DataObjectを使用してドラッグ開始
Dim D As New DataObject
D.SetText TextBox1.SelText
D.StartDrag
End If
End Sub
'○テキストボックス1のドロップ時処理
Private Sub TextBox1_BeforeDropOrPaste _
(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Action As MSForms.fmAction, _
ByVal Data As MSForms.DataObject, _
ByVal X As Single, ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, _
ByVal Shift As Integer)
'同一テキストボックス内でのドロップ回避
Cancel = True
End Sub
|