【问题标题】:How can I create a tool's event at the same time while creating a tool? (C#, windows forms)如何在创建工具的同时创建工具事件? (C#,Windows 窗体)
【发布时间】:2021-08-10 18:01:18
【问题描述】:

作为我的作业要求,我必须在创建表单后立即创建与存储在 ProductManager 中的数组中的产品数量一样多的按钮。按下按钮时,将打开另一个表单,显示产品的属性并允许将其添加到购物篮中。但是,由于这些按钮是最初创建的,因此我无法在它们的事件中按照我的意愿行事。比如我需要获取点击的按钮对应的是哪个产品,我需要以另一种形式展示这个产品的特性。这里出现了两个不同的问题:

1- 创建按钮后,我只需要使用它们的 Click 事件,但我无法访问它。

2- 我无法控制在显示产品详细信息的表单中单击按钮的产品。

加载表单时按钮自动出现的表单: click here to see what it seems like

private void Form2_Load(object sender, EventArgs e)
    {
        int buttonId = 0;
        int locationX = 2;
        int locationY = 2;
        for (int i = 0; i < productManager.getAll().Count; i++)
        {
            Button newButton = new Button();
            newButton.Image = Image.FromFile(productManager.getAll()[i].Path);
            newButton.Text =locationX.ToString();
            newButton.Size= new Size(180, 180);
            newButton.Location = new Point(locationX,locationY);
            locationX += 200;
            if (locationX > 805)
            {
                locationY += 200;
                locationX = 2;
            }
            this.Controls.Add(newButton);

            currentProduct = productManager.getAll()[i];
            newButton.Click += new EventHandler(button_Click);
        }
        
    }

我尝试使用的两个主题函数(事件):

 private void CurrentButton_Click(object sender, EventArgs e)
    {
        productDetailsWindow.Show();
    }

    protected void button_Click(object sender, EventArgs e)
    {
        Product product = sender as Product;
        productDetailsWindow.Show();
        
    }

包含产品属性和添加到购物车按钮click here to see what it seems like的表单

【问题讨论】:

    标签: c# forms winforms object events


    【解决方案1】:

    最简单的方法是将匿名函数指定为单击事件处理程序,该处理程序将打开当前产品的对话框。

        ...
        // Inside Form2_Load
        currentProduct = productManager.getAll()[i];
        newButton.Click += (sender, e) => OpenProduct(currentProduct);
    }
    
    private void OpenProduct(Product product)
    {
       // open product in other form
    }
    

    由于您没有使用事件发送者和参数参数,您可以使用丢弃运算符告诉 VS,它们是故意未使用的并删除警告。

    newButton.Click += (_, _) => OpenProduct(currentProduct);
    

    【讨论】:

      【解决方案2】:

      我认为最好的解决方案是将对象与按钮相关联,WinFroms 中的每个控件都有一个名为“TAG”的属性,在该属性中我将关联对象(在您的情况下是产品)并关联使用“NewButton_Click”方法的“Click”事件。因此,在您的窗口的 Load 事件中,它看起来像这样:

          private void Form2_Load(object sender, EventArgs e)
          {
              int buttonId = 0;
              int locationX = 2;
              int locationY = 2;
              for (int i = 0; i < productManager.getAll().Count; i++)
              {
                  Button newButton = new Button();
                  newButton.Image = Image.FromFile(productManager.getAll()[i].Path);
                  newButton.Text = locationX.ToString();
                  newButton.Size = new Size(180, 180);
                  newButton.Location = new Point(locationX, locationY);
                  locationX += 200;
                  if (locationX > 805)
                  {
                      locationY += 200;
                      locationX = 2;
                  }
                  this.Controls.Add(newButton);
      
                  currentProduct = productManager.getAll()[i];
                  newButton.Tag = currentProduct;
                  newButton.Click += NewButton_Click;
              }
      
          }
      

      在“NewButton_Click”方法中,“sender”参数与您按下的按钮相关联,因此我将“sender”参数转换为按钮,然后获得“Tag”属性,我知道它关联了一个对象,如果它是一个产品,那么我想你的“productDetailsWindow”表单将创建一个产品类型属性,你可以将它们与之关联,你现在可以在窗口中显示产品的详细信息。 “NewButtpm_Click”方法如下所示:

          private void NewButton_Click(object sender, EventArgs e)
          {
              var button = sender as Button;
              var product = button.Tag as Product;
              productDetailsWindow.Product = product;
              productDetailsWindow.Show();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        相关资源
        最近更新 更多