将项目从列表框复制/粘贴到任何文档(Excel、Word、.txt)——VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6674391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:36:23  来源:igfitidea点击:

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

vb.net

提问by User124726

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

我无法将列表框中的项目复制/粘贴到任何文档(Excel、Word、.txt)。我需要在列表框中选择多个项目。我搜索了它,但那里似乎有多个模糊的答案。任何人都可以指导我吗?

Thanks!

谢谢!

回答by Rajeev

All you need to do is allow SelectionModeto MultiSimpleor MultiExtendedthen you can use SelectedItems collection to copy to clipboard in KeyDown event of listbox

所有你需要做的是让SelectionModeMultiSimpleMultiExtended那么你可以使用SelectedItems集合复制到剪贴板中的列表框的KeyDown事件

Simply put

简单的说

ListBox1.SelectionMode = SelectionMode.MultiSimplein form.loadEvent

ListBox1.SelectionMode = SelectionMode.MultiSimpleform.load事件

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

并使用此代码(注意:列表框被命名为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

回答by atari400

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