2

I am in need of assistance, I've been looking all day in Google and so far I have not found an article for what I'm trying to do. I am working on a little project in C# using SharpDevelop as my IDE, in the user interface for my project I have several Labels, 11 of which indicate what the field is about (i.e. "Name:" , "E-mail:") and the other 11 which will auto fill with information from a DB after pressing the Search button and typing in a keyword.

What I need to do is copy all of the labels onto the Clipboard so that the copied information may be used in another program - I have this very same application in Excel and it does what I want but I need a little more versatility thus I decided to give it a go on C#.

Is there any way to accomplish this in C#? I have come accross ListView and DataGrids and I've thought about copying the labels to ListView (as an alternative and if possible) so that I may copy the information from ListView but with format for example: Name: Tim Turner

hectormtnezg
  • 132
  • 5
  • 18

2 Answers2

1

Place all of the output controls in a panel (or identify them however you think is best) and then you can use the following code:

StringBuilder clipboard = new StringBuilder();
foreach (Label label in outputPanel.Controls.OfType<Label>())
    clipboard.Append(label.Text + "\n");

Clipboard.SetText(clipboard.ToString());

Update

It was my understanding that you only wanted to copy the values of a series of check boxes and that would suffice. All you need to do if you would rather not iterate over a collection of controls but append values to the clipboard manually all you need to do is well, exactly that.

private void SetClipboard()
{
    StringBuilder clipboard = new StringBuilder();
    clipboard.Append(label1.Text + "\n");
    clipboard.Append(label2.Text + "\n");
    clipboard.Append(textBox1.Text);
    Clipboard.SetText(clipboard.ToString());
}
User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • Thanks ByteBlast, I've studied this piece of code and I think I can make the object read every label individually, why do that? Because I also have a couple of textboxes, a combo box and 2 rich textboxes that need to be read, – hectormtnezg Jan 29 '13 at 21:46
0

IF you are using Windows Forms you can use ContextMenuStrip , by naming an option "copy", then applying named ContextMenuStrip to each label option that says ContextMenuStrip

    private void copyUserInfoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string UserInfo = $"{lblFirstName.Text}\n" +
                        $"{lblLastName.Text}\n" +
                        $"{lblEmailAddress.Text}\n" +
                        $"{lblPhysicalAddress.Text}\n" +
                        $"{lblCountry.Text}\n" +
                        $"{lblCompany.Text}\n" +
                        $"{lblStatus.Text}\n" +
                        $"{lblFirstContact.Text}\n" +
                        $"{lblLastContact.Text}\n" +
                        $"{lblNotes.Text}\n ";
        Clipboard.SetText(UserInfo);
    }

IF you want to select FOR a single label use a second option on the ContextMenuStrip and use the following:

Clipboard.SetText(labelContextMenuStrip.SourceControl.Text);

See the following: https://stackoverflow.com/a/53263702/7444103 C# How to copy text from different labels using only one context menu when right click

That was with help from @CoolBots and @Jimi.

John B
  • 120
  • 1
  • 9