繁体   English   中英

如何获取动态制作的单击按钮的索引

[英]how do i get the index of a dynamically made clicked button

所以我有问题。 在这种情况下,我总共有 12 个按钮,0-11。 我希望能够单击第 5 个(创建它的列表中的第 4 位)并让它识别它在创建它的列表中的 position 4 中。

List<Button> test = new List<Button> { };

        private void Details_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++) //this for loop runs 12 times
            {

                Button var = new Button(); //makes a button "var"
                var.Text = ("test2.0"); //makes the text on the button 
                var.Click += new System.EventHandler(this.neat); //when you click on the button it runs the "neat function"
                test.Add(var); //adds it to the list called "test"


            }
            testing();
        }
        void neat(object sender, EventArgs e)
        {
            MessageBox.Show("test"); //a message box
        }
        private void testing()
        {
            int topValue = 50; //this is just used to give each button a position on the panel on the form
            int leftvalue = 60;
            int count = 0;
            foreach (Button i in test) //this is pretty much just displaying the on the screen
            {


                i.Left = leftvalue;
                i.Top = topValue;
                panel1.Controls.Add(i); //adds the buttons to the pannel
                count += 1;
                leftvalue += 200;
                if (count % 3 == 0)
                {
                    topValue += 200;
                    leftvalue = 60; //every 4th button makes a new row
                }
            }
        }

是的。 我只是想知道如何知道当我单击 position 4 中的按钮时,它位于位置 4。我能想到的唯一方法将只给出误报或不会一起工作。

是的,我最终会将其更改为填充,不要私刑我

一种简单、干净且可靠的方法是使用内置 System.Linq class。 调用列表中的 IndexOf() 方法并传入被单击的按钮。 因此,如果您的列表名为“test”,则获取按钮的索引,如下所示:

test.IndexOf(button);

考虑将其与通过继承 Button 并将其烘焙到继承的 class 中来制作您自己的自定义按钮相结合,如下所示:

    class MyIndexedButton : Button
    {
        readonly List<Button>_list;
        public MyIndexedButton(List<Button> list)
        {
            // This button adds itself to the list.
            list.Add(this);
            _list = list;   // Here we keep track of the list it's in.
        }
        protected override void OnClick(EventArgs e)
        {
            // Here the button handles its own click event. 
            // Note: You DON'T need to do a += to subscribe!
            base.OnClick(e);
            // Now, to show that it works, we'll have it look 
            // in the list and retrieve its own index using
            // a System class called Linq.
            MessageBox.Show(
                "Clicked " + 
                _list.IndexOf(this) // Using System.Linq to detect the index.
                .ToString());
        }
    }

一件好事是,即使删除或添加按钮,它们仍然会动态地知道它们在列表中的正确索引。 这是一个简单的测试驱动程序。 如果您想尝试一下,您可以复制代码或从 GitHub下载工作示例。

    public Form1()
    {
        InitializeComponent();
    }
    // We'll just make a little layout panel to manage the button positions.
    TableLayoutPanel _layout;   
    // Here's the list of Buttons you already had. We still need it.
    List<Button> _test = new List<Button> { };
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        _layout = new TableLayoutPanel() { ColumnCount = 4, RowCount = 4, Dock = DockStyle.Fill };
        Controls.Add(_layout);
        int row, column;
        for (int count = 0; count < 12; count++)
        {
            row = count / 4; column = count % 4;
            // When you make your custom button, just 
            // make sure you pass in the list like so:
            _layout.Controls.Add(new MyIndexedButton(_test), column, row);
        } 
    }

现在只需运行它,看看它是否适合您。 当您单击它时,应该会弹出一条消息,说明按钮 ID 介于 0 和 11 之间(实际从零开始的索引)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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