Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to copy selected item from listview and pasting it in another text box using context menu.

What I have tried:

C#
private void mnuPaste_click(object sender, EventArgs e)
{
    IDataObject idata = Clipboard.GetDataObject();
    if(idata.GetDataPresent(DataFormats.Text))
    {
        txtdemo.text=(String)idata.GetData(DataFormats.Text);
    }
}


I tried for the code to paste the text. And I am able to paste the text in another text box. I was not able to copy the item from listview.

I followed https://www.codeproject.com/Articles/5952/Context-Menu-for-ListView-Column-Manipulation for referance. Its nice information but could not find information to get value of selected item.
Posted
Updated 28-Feb-18 5:37am
v2
Comments
phil.o 28-Feb-18 10:01am    
Please show the code which assigns the text value to the clipboard.
webmail123 28-Feb-18 10:18am    
I tried to access Listview1.SelectedItem property but I am using Windows SDK 6.0 for mobile development, I am not able to access that property.
phil.o 28-Feb-18 10:56am    
That is not my question :)
You have shown the part where you are pasting from the clipboard. We would like to have the part where you are copying to the clipboard.
webmail123 28-Feb-18 10:55am    
I tried this one (which was just experimental)
Clipboard.SetDataObject(listView1.SelectedIndices[0].Tostring());

But obviously it returns the index value. Not the text. :-(
phil.o 28-Feb-18 10:58am    
Then you should put a breakpoint on this line, lauch a debug session (F5), and see which one of the properties of the listview actually holds the value you want.

1 solution

I am using .Net compact framework so I was unable to perform to copy the string of the selected item and put it into clipboard. But Now I have done it.

This is what I did:

C#
private void mnuCopy_click(object sender, EventArgs e)
{
    string ind1 = listView1.SelectedIndices[0].ToString();
    int ind2 = Convert.ToInt16(ind1);
    string item = listView1.Items[ind2].Text; //here I got the string of selected item
    Clipboard.SetDataObject(item);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900