简体   繁体   English

如何添加到 button_click 的事件参数

[英]how to add to event args of button_click

I have a button_click(object sender, EventArgs s) and a class Purchase:EventArgs plus a dynamically created button.我有一个 button_click(object sender, EventArgs s) 和一个 class Purchase:EventArgs 加上一个动态创建的按钮。 I want to button to click, then send 'Purchase' instead of event args.我想单击按钮,然后发送“购买”而不是事件参数。 ie, I want to edit event args to the button as it clicks.即,我想在按钮单击时将事件参数编辑到按钮。 such that I'll use that information instead, as it is a dynamically created button.这样我就可以使用该信息,因为它是一个动态创建的按钮。 how do I do that?我怎么做? I have several ideas such as creating a master class for the button, but I want answers.我有几个想法,例如为按钮创建主控 class,但我想要答案。

I know it's a difficult question.我知道这是一个很难的问题。 but what use are event args if they are inaccessible?但是如果事件参数不可访问,它们有什么用呢?

var button = new Button();
button.Text = "change";
button.Click += button_Click;

table.Controls.Add(new Button() { Text = "change" }, 0, table.RowCount - 1);
            table.Controls.Add(new TextBox() { Text = pr.value.ToString() }, 0, table.RowCount - 1);
            table.Controls.Add(new DateTimePicker() { Text = pr.time.ToString() }, 0, table.RowCount - 1);
            table.Controls.Add(new TextBox() { Text = "transaction" }, 0, table.RowCount - 1);

            table.RowCount++;

I want something我想要某个东西

like button.Click+=button_click(pr) 

when什么时候

/*when pr:*/ Purchase pr = new Purchase()//...;

Set the click handler for each button to the same将每个按钮的点击处理程序设置为相同

Set the Tag attribute for the button to the object you want to handle in the click将按钮的Tag属性设置为点击中要处理的object

 Button MakeButton(string text, object tag){
       var b = new Button{
         Text = text,
         Tage = tag
      };
       b.Click += button_Click;
      return b;
  }
     
 

 table.Controls.Add(MakeButton("change", new Froodle()), 0, table.RowCount - 1);
 table.Controls.Add(MakeButton(pr.value.ToString(), new Froodle()), 0, table.RowCount - 1);

etc ETC

Now in the click handler pick up the object from the tag现在在点击处理程序中从标签中获取 object

 void button_Click(object sender, Eventargs e){
     var target = sender.Tag;
 }

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

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