将listview拖放到文本框

时间:2015-04-20 14:14:57

标签: vb.net

我目前正在创建一个从数据库表填充列表框的应用程序。我想要做的是能够将项目从列表框拖到文本框。当我从列表框中拖动一个项目并将其放在文本框中时,文本框中的内容是System.Data.DataRowView。我不确定为什么会这样。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    sqlselectpart = "Select fpartno from inmast"

    Try
        If connection.State = 0 Then
            connection.Open()
        End If
        DS.Clear()

        adapter = New SqlDataAdapter(sqlselectpart, connection)
        adapter.Fill(DS, "fpartno")
        ListBox1.DataSource = DS.Tables("fpartno")
        ListBox1.DisplayMember = "fpartno"

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try


End Sub

Private Sub ListBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

    If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.ListBox1.SelectedItem IsNot Nothing Then
        Me.ListBox1.DoDragDrop(Me.ListBox1.SelectedItem.ToString(), DragDropEffects.Copy)
    End If

End Sub

Private Sub TextBox1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter

    If (e.Data.GetDataPresent(DataFormats.Text)) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If

End Sub

Private Sub TextBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop

    TextBox1.Text = e.Data.GetData(DataFormats.Text)

End Sub

0 个答案:

没有答案