开发者

Copy/Paste Items from listbox to any doc (Excel, Word, .txt) -- VB.NET

I'm unable to copy/paste items from my listbox to any document (Excel, Word, .txt). I would need to select multiple 开发者_StackOverflow中文版items in the listbox. I searched for it but there seem to be multiple vague answers around there. Can anyone guide me?

Thanks!


All you need to do is allow SelectionMode to MultiSimple or MultiExtended then you can use SelectedItems collection to copy to clipboard in KeyDown event of listbox

Simply put

ListBox1.SelectionMode = SelectionMode.MultiSimple in form.load Event

and use this code (note: listbox is named as ListBox1)

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown

    If e.Control AndAlso e.KeyCode = Keys.C Then
        Dim copy_buffer As New System.Text.StringBuilder
        For Each item As Object In ListBox1.SelectedItems
            copy_buffer.AppendLine(item.ToString)
        Next
        If copy_buffer.Length > 0 Then
            Clipboard.SetText(copy_buffer.ToString)
        End If
    End If
End Sub


Insert a richtextbox and add this:

For x As Integer = 0 To ListBox1.Items.Count - 1
    RichTextBox1.AppendText(ListBox1.Items(x).ToString & Environment.NewLine)
Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜