开发者

Copy button label to clipboard

开发者 https://www.devze.com 2023-03-04 04:51 出处:网络
I\'m using private void Form1_Load(object sender, EventArgs e) { int i = 1; var allLines = File.ReadAllLines(@\"c:\\text.txt\");

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.Contro开发者_如何学运维ls.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消