如何在 C# 方法中设置动态参数

发布于 2024-11-29 09:50:50 字数 848 浏览 0 评论 0原文

我为按钮 btnTrial1 编写了一个方法 Trial

public void trial(object sender, EventArgs e, Button btn, TextBox txt, Label lbl)
{

}

在我的应用程序中,我通过代码动态生成更多按钮、文本框和标签,并按顺序命名它们,如 btnTrial2< /code>、txtTrial2lblTrial2 然后 btnTrial3txtTrial3lblTrial3 等等。现在我想将 Trial2 设置为 trial 的 EventHandler,然后将 btnTrial3 设置为 EventHandler,依此类推。

因此,现在当我从 btnTrial1 调用方法 Trial 时,我的参数应该是:

Public void (sender, e, btnTrail1, txtTrial1, lblTrial1)

但是当我从 btnTrial2< 调用方法 Trial 时, 我的参数应该是: /code>,我的参数应该是:

Public void (sender, e, btnTrail2, txtTrial2, lblTrial2)

等等...

I have written a method trial for button btnTrial1:

public void trial(object sender, EventArgs e, Button btn, TextBox txt, Label lbl)
{

}

In my application, i am generating more buttons and textboxes and labels dynamically through code and naming them sequentially like btnTrial2, txtTrial2, lblTrial2 then btnTrial3, txtTrial3, lblTrial3 and so on. Now i want to set trial as EventHandler for btnTrial2 then for btnTrial3 and so on.

So now when i call the method trial from btnTrial1, my parameters should be:

Public void (sender, e, btnTrail1, txtTrial1, lblTrial1)

But when i call the method trial from btnTrial2, my parameters should be:

Public void (sender, e, btnTrail2, txtTrial2, lblTrial2)

and so on...

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

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

发布评论

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

评论(4

沦落红尘 2024-12-06 09:50:50
btnTrial1.YourEvent += (sender, args) => trial(sender, args,
        btnTrial1, txtTrial1, lblTrial1);
btnTrial2.YourEvent += (sender, args) => trial(sender, args,
        btnTrial2, txtTrial2, lblTrial2);

您提到“动态生成它们” - 这很好,但是如果您处于循环中,您还需要注意臭名昭著的“捕获变量/循环”问题 - 特别是变量“捕获”必须在循环内;例如:(

for(int i = 1 ; i <= 10 ; i++) {
    var btnTrial = ...
    var txtTrial = ...
    var lblTrial = ...
    btnTrial.YourEvent += (sender, args) => trial(sender, args,
        btnTrial, txtTrial, lblTrial);
}

如果,例如,btnTrial 被声明在循环之外,就会发生不好的事情)

btnTrial1.YourEvent += (sender, args) => trial(sender, args,
        btnTrial1, txtTrial1, lblTrial1);
btnTrial2.YourEvent += (sender, args) => trial(sender, args,
        btnTrial2, txtTrial2, lblTrial2);

You mention "generating them dynamically" - that is fine, but if you are in a loop you will also need to watch out for the infamous "captured variable / loop" problem - notably, the variables "captured" must be inside the loop; for example:

for(int i = 1 ; i <= 10 ; i++) {
    var btnTrial = ...
    var txtTrial = ...
    var lblTrial = ...
    btnTrial.YourEvent += (sender, args) => trial(sender, args,
        btnTrial, txtTrial, lblTrial);
}

(if, for example, btnTrial was declared outside the loop, bad things would happen)

北音执念 2024-12-06 09:50:50

为什么不让你的试用功能

public void trial(object sender, EventArgs e)
{

}

然后根据发件人,使用不同的控件:

public void trial(object sender, EventArgs e)
{
    Button btn;
    TextBox txt;
    Label lbl;

    if (sender == btnTrial1){
        btn = btnTrail1;
        txt = txtTrial1;
        lbl = lblTrial1;
    }

    if (sender == btnTrial2){
        btn = btnTrail2;
        txt = txtTrial2;
        lbl = lblTrial2;
    }

    if (sender == btnTrial3){
        btn = btnTrail3;
        txt = txtTrial3;
        lbl = lblTrial3;
    }

    //rest of the method
}

Why not make your trial function

public void trial(object sender, EventArgs e)
{

}

And then based on the sender, use different controls:

public void trial(object sender, EventArgs e)
{
    Button btn;
    TextBox txt;
    Label lbl;

    if (sender == btnTrial1){
        btn = btnTrail1;
        txt = txtTrial1;
        lbl = lblTrial1;
    }

    if (sender == btnTrial2){
        btn = btnTrail2;
        txt = txtTrial2;
        lbl = lblTrial2;
    }

    if (sender == btnTrial3){
        btn = btnTrail3;
        txt = txtTrial3;
        lbl = lblTrial3;
    }

    //rest of the method
}
千柳 2024-12-06 09:50:50

两个建议:

  1. 重构Trial()以返回EventHandler(或者理想情况下EventHandler):

    public EventHandler GetTrialEventHandler(按钮 btn, 文本框 txt, 标签 lbl)
    {
         返回(发件人,参数)=>
         {
             // 对 btn、txt、lbl 执行某些操作
         };
    }
    
  2. 相当比命名您的表单元素 btnTrial1btnTrial2 等,为什么不只制作元素列表(或一组列表) 按钮+文本框+标签)?然后,您只需枚举列表即可设置事件处理程序,而不是为每个事件处理程序进行硬编码。

Two suggestions:

  1. Refactor trial() to return an EventHandler (or ideally EventHandler<T>):

    public EventHandler GetTrialEventHandler(Button btn, TextBox txt, Label lbl)
    {
         return (sender, args) =>
         {
             // Do something with btn, txt, lbl
         };
    }
    
  2. Rather than name your form elements btnTrial1, btnTrial2, etc, why not just make lists of the elements (or a list of sets of Button+TextBox+Label)? Then you just have to enumerate over the list(s) to set up your event handlers, rather than hard-code for each.

白色秋天 2024-12-06 09:50:50

您可以为每个按钮添加:

this.button.Click += new System.EventHandler(this.button_Click);

方法,如下所示:

private void button_Click(object sender, EventArgs e)
{
    String name = (sender as Control).Name;
    int number = Int32.Parse(name.Substring(name.Length-1));
    trial(sender, e, this.Controls["btnTrial"+number], this.Controls["txtTrial"+number], Controls["lblTrial"+number]);
}

To each button You can add:

this.button.Click += new System.EventHandler(this.button_Click);

method like this:

private void button_Click(object sender, EventArgs e)
{
    String name = (sender as Control).Name;
    int number = Int32.Parse(name.Substring(name.Length-1));
    trial(sender, e, this.Controls["btnTrial"+number], this.Controls["txtTrial"+number], Controls["lblTrial"+number]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文