简体   繁体   中英

Copy button label to clipboard

I'm using

private void Form1_Load(object sender, EventArgs e)
{
    int i = 1;
    var allLines = File.ReadAllLines(@"c:\text.txt");

    foreach (var line in allLines)
    {
        var b = new Button();
        b.Text = line;
        b.AutoSize = true;
        b.Location = new Point(22, b.Size.Height * i);
        this.Controls.Add(b);
        i++;
    }
}

to create a bunch of buttons from a text file

how can i control the behaviour of all buttons - i want them to copy the label to clipboard

Add this just before the this.Controls.Add(b) line:

b.Click += EventHandler((s, e) => Clipboard.SetText(line));

This creates an handler for the Click event that copies the line to the clipboard.

For more information about Windows Forms programming a good starting point is Microsoft's own WindowsClient.NET website. A lot of the information is skewed towards WPF these days but there should still be plenty of Forms stuff around.

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