Tipp 0007 Drag & Drop zwischen Textboxen
Autor/Einsender:
Datum:
  Michael Werner
28.01.2003
Entwicklungsumgebung:   VB.Net 2002
Framework:   1.0
Dieser Tipp zeigt, wie mit Hilfe der Textbox-Eigenschaft AllowDrop und der Textbox-Methode DoDragDrop, sowie den Events MouseDown, DragDrop und DragEnter, und der DragDropEffects-Enumeration (DragDropEffects.Copy und DragDropEffects.Move), ein Text zwischen Textboxen hin- und hergeschoben werden kann.
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Const CtrlMask As Byte = 8
  Const Text1 As String = "Hallo Leut's!" & vbNewLine & _
                          "Mich freut's."
  Const Text2 As String = "Pipapo," & vbNewLine & _
                          "lach nicht so."

  Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.AllowDrop = True
    TextBox2.AllowDrop = True
    TextBox1.Text = Text1
    TextBox2.Text = Text2
  End Sub

  Private Sub TextBox1_MouseDown(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles TextBox1.MouseDown
    If e.Button = MouseButtons.Left Then
      TextBox1.SelectAll()
      TextBox1.DoDragDrop(TextBox1.SelectedText, _
            DragDropEffects.Move Or DragDropEffects.Copy)
    End If
  End Sub

  Private Sub TextBox2_DragDrop(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TextBox2.DragDrop
    TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString
    If (e.KeyState And CtrlMask) <> CtrlMask Then
      TextBox1.Text = ""
    End If
  End Sub

  Private Sub TextBox2_DragEnter(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TextBox2.DragEnter
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
      If (e.KeyState And CtrlMask) = CtrlMask Then
        e.Effect = DragDropEffects.Copy
      Else
        e.Effect = DragDropEffects.Move
      End If
    Else
      e.Effect = DragDropEffects.None
    End If
  End Sub

  Private Sub TextBox2_MouseDown(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles TextBox2.MouseDown
    If e.Button = MouseButtons.Left Then
      TextBox2.SelectAll()
      TextBox2.DoDragDrop(TextBox2.SelectedText, _
            DragDropEffects.Move Or DragDropEffects.Copy)
    End If
  End Sub

  Private Sub TextBox1_DragDrop(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TextBox1.DragDrop
    TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString

    If (e.KeyState And CtrlMask) <> CtrlMask Then
      TextBox2.Text = ""
    End If
  End Sub

  Private Sub TextBox1_DragEnter(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) Handles _
        TextBox1.DragEnter
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
      If (e.KeyState And CtrlMask) = CtrlMask Then
        e.Effect = DragDropEffects.Copy
      Else
        e.Effect = DragDropEffects.Move
      End If
    Else
      e.Effect = DragDropEffects.None
    End If
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = Text1
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    TextBox2.Text = Text2
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button3.Click
    Me.Close()
  End Sub
End Class
 
Weitere Links zum Thema
Datei-Drag & Drop in eine ListBox

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,9 kB) Downloads bisher: [ 1396 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 22. Januar 2012