שתף באמצעות


Cut and Paste to the Clipboad from a Listbox?

Question

Tuesday, November 6, 2018 9:22 PM

The following code will copy and paste from a listbox, but the issue is it copies every item from the listbox. I would like to either copy only what items are selected or copy the number of items selected from a textbox. An example would be if the textbox has the number 4 in it it would only copy the first four items in the text box.

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim S As String
        For X = 0 To ListBox1.Items.Count - 1
            S = S & ListBox1.Items(X) & vbCrLf
        Next X
        Clipboard.SetText(S)

    End Sub

All replies (4)

Wednesday, November 7, 2018 8:46 AM ✅Answered

Hi,

 if the textbox has the number 4 in it it would only copy the first four items in the text box.

 Dim S As String
        For X = 0 To TextBox1.Text - 1
            S = S & ListBox1.Items(X) & vbCrLf
        Next X
        Clipboard.SetText(S)

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Tuesday, November 6, 2018 10:34 PM

Hi

Try this for Copy Selected:

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim buff As New Text.StringBuilder
    For Each obj As Object In ListBox1.SelectedItems
      buff.AppendLine(obj.ToString)
    Next
    If buff.Length > 0 Then
      Clipboard.SetText(buff.ToString)
    End If
  End Sub

Regards Les, Livingston, Scotland


Tuesday, November 6, 2018 10:53 PM

Hi

Try this for Copy from item 0 to TextBox value of items (needs above Button2 code as well):

  Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    ListBox1.SelectedIndex = -1
    Dim c As Integer = 0
    Integer.TryParse(TextBox13.Text, c)
    If c > -1 AndAlso c < ListBox1.Items.Count - 1 Then
      Clipboard.Clear()
      For i As Integer = 0 To c - 1
        ListBox1.SelectedIndex = i
      Next
      Button2.PerformClick()
    End If
  End Sub

Regards Les, Livingston, Scotland


Wednesday, November 7, 2018 7:03 PM

Thank you all for your help