【问题标题】:Change text of a dynamically created checkbox更改动态创建的复选框的文本
【发布时间】:2015-09-13 08:15:11
【问题描述】:

在 C# Windows 窗体中,如何添加事件以在单击时更改复选框的文本? 我的代码在需要时调用了“criarCheckBox”,但我无法引用它来添加操作。

    public void criarCheckBox(String nome, String texto)
    {
        CheckBox box = new CheckBox();
        box.Name = nome;
        box.Text = texto;
        listaCheckBox.Add(box);
        box.CheckedChanged += new EventHandler(checkBoxClick);
    }

    void checkBoxClick(object sender, EventArgs e)
    {

    }

【问题讨论】:

  • 这是 WinForms、WPF、Windows Phone、Windows Store 应用程序、通用应用程序吗?猜测它一定是基于 CheckedChanged 事件的 winforms?
  • 是的,它是 winForms!我忘了添加
  • 在事件中您可以投射发件人。外部可以在父控件列表中搜索,或者更好的是imo,保留对它的引用,也许在列表中......

标签: c# winforms checkbox event-handling


【解决方案1】:

使用sender参数:

void checkBoxClick(object sender, EventArgs e)
{
    var checkBox = (CheckBox)sender;
    checkBox.Text = "Hello";
}

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多