1

So I created a variable to hold my clipboard text and I have no idea on how to append it to a listbox.

This is as far as I got..

private void clipboardBtn_Click(object sender, EventArgs e)
{
    string items = Clipboard.GetText();
    List<string> _items = new List<string>();
    _items.AddRange(items);
}

but that throws me this error..

Argument 1: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable'

What's causing this and how do I fix it? Is this even the correct way of appending text to the listbox?

-UPDATE-

I got this now but everytime I click the button it overwrites the old one instead of appending a new item to the listbox

    string items = Clipboard.GetText();
    List<string> _items = new List<string>();
    _items.Add(items);
    listBox1.DataSource =_items;

How do i append a new item?

John V Dole
  • 321
  • 1
  • 3
  • 11

5 Answers5

1

Clipboard.GetText has the signature

public static string GetText()

but List<T>.AddRange has the signature

public void AddRange( IEnumerable<T> collection )

So essentially you're trying to add a string as an IEnumerable<T> which gives you the above error.

Better use List<T>.Add for that purpose like that:

_items.Add(items);
zx485
  • 28,498
  • 28
  • 50
  • 59
0

your question is about List object and not about ListBox control.
the AddRange() method requires a collection, you can transform your string to a collection (Array) by using Split.

private void clipboardBtn_Click(object sender, EventArgs e)
{
            string YourGetClipBoardTextString = "aaa;bbb;ccc;ddd";
            List<string> _items = new List<string>();
            _items.AddRange(YourGetClipBoardTextString.Split(';').ToArray()); // you can split the string by any char seperator ";" " ", "," etc...
}

if you dont need to split the string just use the Add() method:

_items.Add(YourGetClipBoardTextString);

After your update, you can append new items to a listbox in that manner:

    foreach (string itm in _items)
    {
        listBox1.Items.Add(itm);
    }
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
0

since you're creating new "_items" on every click, you cannot see the old items. try like this,

 List<string> _items = new List<string>();
    private void clipboardBtn_Click(object sender, EventArgs e)
    {
        string items = Clipboard.GetText();
        _items.Add(items);
        listBox1.DataSource =_items;
    }

_items declared outside of method scope.

WPFUser
  • 1,145
  • 7
  • 24
0

First you need to split the clipboard contents into strings for each line, then you need to add them to the list box:

string[] items = Clipboard.GetText().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
listBox1.Items.AddRange(items);

If you don't want to have a separate listBox item for each line, just do this:

listBox1.Items.Add(Clipboard.GetText());
MrGoodbytes
  • 294
  • 2
  • 13
0

Your problem is that you are initializing a new list each time:

string items = Clipboard.GetText();
List<string> _items = new List<string>();//<New list here results in removal of existing item
_items.Add(items);
listBox1.DataSource =_items;

Try something like this:

 string items = Clipboard.GetText();
List<string> _items = listBox1.DataSource as List<string>;// You may have type casting issues here - 
_items.Add(items);
listBox1.DataSource =_items;
Theo
  • 885
  • 6
  • 16