【问题标题】:how to create a list of events for a list of buttons c#?c# - 如何为按钮列表创建事件列表?
【发布时间】:2018-06-25 02:56:26
【问题描述】:

我有一个按钮列表...但我想为列表中的每个按钮创建一个事件——我已经尝试过这段代码

ButtonName.Click += (sender, args) =>
            {
                Point p = new Point(20 * j, 70);
                Product[j].Location = p;
                Product[j].Width = 200;
                Product[j].Height = 250;
                this.Controls.Add(Product[j]);
            };

我想做的事情是,当单击任何按钮时,它应该显示一个与该按钮相关的列表框。 并且每个列表框都有自己的数据....我想要解决的唯一问题是为列表中的每个按钮制作事件 这可能吗??

更新

for (int j = 0; j < Data.BTN_Name.Count; j++) 
{ 
   Category[j].Click += (sender, args) => 
     { 
        Point p = new Point(20 * j, 70); 
        Product[j].Location = p; 
        Product[j].Width = 200; 
        Product[j].Height = 250; 
        this.Controls.Add(Product[j]); 
      }; 

【问题讨论】:

  • 按钮名称是按钮列表,不仅仅是一个按钮)
  • 每个按钮的事件内容有何不同?
  • 点击一个按钮应该显示一个列表框,每个列表框都有自己的数据
  • foreach (var button in ButtonList) button.Click += ButtonClickHandler;?
  • 什么是Product[j]

标签: c# winforms oop user-interface


【解决方案1】:

在我看来你想这样做:

        for (int j = 0; j < Data.BTN_Name.Count; j++)
        {
            Product[j] = new ListBox()
            {
                Location = new Point(20 * j, 70),
                Width = 200,
                Height = 250,
                Visible = false,
            };
            this.Controls.Add(Product[j]);
            var captured_j = j;
            Category[j].Click += (s, ea) => Product[captured_j].Visible = true;
        }

您必须捕获 j 变量才能在事件处理程序中使用它 - 因此代码 var captured_j = j; 就在事件处理程序之前。

【讨论】:

  • 那么你需要改进你的问题,以明确你想要的行为是什么。没有关于点击的顺序以及它如何影响元素的位置。
  • 这是解决方案.. var capture_j = j;谢谢
【解决方案2】:

正如我所见,您实际上是在尝试将某个按钮绑定到某个列表框。正如@TheGeneral 所说,您可以使用按钮的Tag 属性,但我不喜欢这种方法(虽然它有权这样做,但这只是习惯问题)。这是我的例子:

public class YourForm : Form
{
    private IDictionary<Button, ListBox> _listboxes = new Dictionary<Button, ListBox>();

    // use this if you create a button and listbox simultaneously
    protected void CreateButtonAndList()
    {
        var listbox = new ListBox();
        // initialize listbox as needed
        var button = new Button();
        // initialize button as needed
        button.Click += ButtonClickHandler;
        _listboxes.Add(button, listbox);
    }

    // use this if you create a button for already existing listbox
    protected void CreateButtonFor(ListBox listbox)
    {
        var button = new Button();
        // initialize button as needed
        button.Click += ButtonClickHandler;
        _listboxes.Add(button, listbox);
    }

    private void ButtonClickHandler(object sender, EventArgs e)
    {
        var listbox = _listboxes[sender];
        // do what you want with listbox
    }
}

另外请注意,您可能不需要CreateButtonAndList()CreateButtonFor() 方法。您可以只留下一个适合您需要的。

【讨论】:

  • @JeremyThompson,我没有向 ListBox 添加按钮:\ 它们(ButtonsListBoxes)应该添加到 // initialize listbox as needed// initialize button as needed 部分的 YourForm.Controls这种方法。我只想指出,OP 实际上不需要按钮(或列表框)的索引来对相应按钮单击时的列表框进行操作。
  • @JeremyThompson 你应该注意到IDictionary&lt;Button, ListBox&gt; _listboxes 那里,他正在为每个按钮添加一个列表框,然后在单击事件中,他正在获取与单击按钮相关联的列表框。
  • 忽略我的评论
猜你喜欢
  • 2017-12-07
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2011-07-09
相关资源
最近更新 更多