Search code examples
.netvb.netcopy-pastepaste

text From the clipbord is pasted twice in a ListBox in VB.Net


I want to paste data from the clipbord in a vb.net using this code:

 Private Sub MenuItemPaste_Click(sender As Object, e As EventArgs) Handles MenuItemPaste.Click

    Dim items() As String = Clipboard.GetText.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)  ' 
    ListBox1.Items.AddRange(items)

Why Does the selected text is pasted 2 times?


Solution

  • Your code seems correct and there is no reason to paste the text twice! I guess you need to clear the ListBox1 first before adding text to it in the MenuItemPaste_Click event. So you can try using the following code :

    ListBox1.Items.Clear()
    Dim items() As String = Clipboard.GetText.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)  ' 
    ListBox1.Items.AddRange(items)