Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy a listbox contents to clipboard

Status
Not open for further replies.

KarveR

MIS
Joined
Dec 14, 1999
Messages
2,065
Location
GB
Hi all, thanks for reading.

Could anyone enlighten me on how to copy a listbox contents to clipboard please?

Porbably blindingly basic but for someone on their first app, its a tad puzzling.

Thanks in advance,

Karv.

PS I've rummaged aroun msdn, doc, csharpcorner and other places, and have gotten as far as

Clipboard.SetDataObject(**but what goes in here omg Im such a noob**);

Is there anywhere I can read on some more of this.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Fire up the Visual Studio.Net Documentation and search for "Clipboard" - there's plenty to read. Here's a snippet:

Remarks
If the copy parameter is false, the data will be deleted from system Clipboard when the application exits.

Example
The following method is run in an application. It places a persistent copy of the selected text data in the text box on the system Clipboard. This code assumes button1, textBox1, and textBox2 have been instantiated and placed on a form.
Code:
[C#] 
private void button1_Click(object sender, System.EventArgs e) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText, true);
    else
       textBox2.Text = "No text selected in textBox1";
 }

In a different application, the following method retrieves the text from the system Clipboard, and pastes the text into textBox2. This code assumes button2 and textBox2 have been instantiated and placed on a form.
Code:
[Visual Basic]
Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()
    
    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
    Else
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub 'button2_Click

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Sorry. my bad.
I should have been a little more specific.
Clipboard.SetDataObject(obj.Text, true); gives me the last selected line.

Clipboard.SetDataObject(obj.Items[x],true); gives me a specific line regardless if its selected, but my question should have been, how do I copy *all* lines to the clipboard.
Is there some mysterious voodoo for SelectAll on ListBoxes, or will I have to go Loopy.

Thanks.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top