简体   繁体   中英

How to make more than one label copy to clipboard with a button

I set up a button called "copyButton". Now I have 7 different labels displaying text. I was wondering if there is a way to set it up so I can copy all the labels at once and past them so they are all together. My labels are named Label1, Label2, and so on until Label7. If they display 1 2 3 4 5 6 7, all seperately. With clicking the copy button it should be able to be pasted as 1234567. I am not sure if this can be done, I may just have to move them all to a text box or into one label.

you can do something like this.

var textCopy = Labe1.Text + Label2.Text; // upto 7
Clipboard.SetText(textCopy);

and if you want a general solution you can do this.. this will copy all text of Labels inside a form. and will set it to clipboard. later on you can paste.

private void button1_Click(object sender, EventArgs e)
        {
            string textToCopy = "";
            foreach (var control in this.Controls) {
                var label =  control as Label;
                if (label !=null) {
                      //by this way will show in assending order
                      textToCopy = label.Text + textToCopy;
                }

            }

            Clipboard.SetText(textToCopy);
        }

Give this a try:

Clipboard.SetText(Label1.Content+ Label2.Content+ Label3.Content+ Label4.Content+ Label5.Content+ Label6.Content+ Label7.Content)

This is for WPF, if you are using Windows Form then you replace Content with Text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM