C#如何处理多动态创建的按钮的事件

发布于 2024-09-17 22:27:22 字数 1091 浏览 4 评论 0原文

我创建了一个 WinForm 并向其中添加了动态按钮,我如何处理它的事件

public static void Notify()
{

    var line = 3;

    Form fm = new Form();
    fm.Text = "Hello!";
    fm.ShowInTaskbar = false;
    fm.ShowIcon = false;
    fm.MinimizeBox = false;
    fm.MaximizeBox = false;
    fm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    fm.TopMost = true;
    fm.ClientSize = new Size(150, 75 * line/2);
    Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
    int left = workingArea.Width - fm.Width-5;
    int top = workingArea.Height - fm.Height-4;
    fm.Location = new Point(left, top);
    fm.StartPosition = FormStartPosition.Manual;

    var buttomArray = new Button[line];

    for (int i = 0; i < line; i++)
    {
        buttomArray[i] = new Button();
        buttomArray[i].Text = "Button " + (i + 1);
        buttomArray[i].Location = new Point(10,30*(i+1) - 16);
        buttomArray[i].Size = new Size(130,25);
        fm.Controls.AddRange(new Control[] { buttomArray[i] });
    }

    fm.Show();
}

我希望能够在单击不同按钮时执行一些不同的操作(也许我可以使用“名称”作为标识符?)

干杯

I have created a WinForm and I added to it dynamic Buttons, how I can deal with it's events

public static void Notify()
{

    var line = 3;

    Form fm = new Form();
    fm.Text = "Hello!";
    fm.ShowInTaskbar = false;
    fm.ShowIcon = false;
    fm.MinimizeBox = false;
    fm.MaximizeBox = false;
    fm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    fm.TopMost = true;
    fm.ClientSize = new Size(150, 75 * line/2);
    Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
    int left = workingArea.Width - fm.Width-5;
    int top = workingArea.Height - fm.Height-4;
    fm.Location = new Point(left, top);
    fm.StartPosition = FormStartPosition.Manual;

    var buttomArray = new Button[line];

    for (int i = 0; i < line; i++)
    {
        buttomArray[i] = new Button();
        buttomArray[i].Text = "Button " + (i + 1);
        buttomArray[i].Location = new Point(10,30*(i+1) - 16);
        buttomArray[i].Size = new Size(130,25);
        fm.Controls.AddRange(new Control[] { buttomArray[i] });
    }

    fm.Show();
}

I want to be able to do some different things when I click on different Button (maybe I can use the "name" as an identifier?)

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-09-24 22:27:22

只需分配 Click 处理程序:

for (int i = 0; i < 10; i++)
{
    var btn = new Button();
    btn.Text = "Button " + i;
    btn.Location = new Point(10, 30 * (i + 1) - 16);
    btn.Click += (sender, args) =>
    {
        // sender is the instance of the button that was clicked
        MessageBox.Show(((Button)sender).Text + " was clicked");
    };
    Controls.Add(btn);
}

Simply assign the Click handler:

for (int i = 0; i < 10; i++)
{
    var btn = new Button();
    btn.Text = "Button " + i;
    btn.Location = new Point(10, 30 * (i + 1) - 16);
    btn.Click += (sender, args) =>
    {
        // sender is the instance of the button that was clicked
        MessageBox.Show(((Button)sender).Text + " was clicked");
    };
    Controls.Add(btn);
}
独闯女儿国 2024-09-24 22:27:22

订阅 Button.Click 事件。在创建循环中时,将要在点击处理程序中使用的数据附加到标签属性。

for (int i = 0; i < line; i++) 
    { 
        buttomArray[i] = new Button(); 
        buttomArray[i].Tag=i;
    .....

在点击处理程序中,发送者将是按钮(您可以转换为它),并且标记将包含您的值。

Button btn=(Button)sender;
int value=(int)btn.Tag;

Tag 属性接受任何类型。因此你可以赋予它任何价值。

Subscribe to the Button.Click event. Attach the data you want to use in the click-handler to the Tag-property while your are in the creation loop.

for (int i = 0; i < line; i++) 
    { 
        buttomArray[i] = new Button(); 
        buttomArray[i].Tag=i;
    .....

In the click handler, the sender will be the Button (you can cast to it) and the tag will contain your value.

Button btn=(Button)sender;
int value=(int)btn.Tag;

The Tag-property accepts any type. Therefore you can attach any value to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文