Append clipboard text to a listbox

Question

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?


Show source
| c#   | wpf   | .net   | listbox   | winforms   2017-01-01 23:01 5 Answers

Answers ( 5 )

  1. 2017-01-02 00:01

    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);
    
  2. 2017-01-02 00:01

    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);
        }
    
  3. 2017-01-02 11:01

    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.

  4. 2017-01-03 17:01

    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());
    
  5. 2017-01-03 17:01

    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;
    
◀ Go back