【问题标题】:Checkbox event handling复选框事件处理
【发布时间】:2012-04-19 16:25:40
【问题描述】:

我有一个 MyControl 类。 MyClass 对象内部有文本框和复选框。

我正在尝试添加一个复选框事件处理程序,但它不起作用。可能是什么问题?

private List<MyControls> _myControls = new List<MyControls>();
MyControls mc = new MyControls();

public void CreateFormElements(int i, StringReader sr)
{
    ProductForm form2 = new ProductForm();
    form2.Visible = true;
    form2.Activate();
    String line = "";

    for (int n = 0; n < i; n++)
    {
        line = sr.ReadLine();
        mc = new MyControls();

        if (line.Length > 3)
        {
            String[] _line = line.Split(new char[] { '\t' });
            mc.SetY(30 + n * 20);
            mc.initElements(_line, n);
            _myControls.Add(mc);
            **mc.cb.CheckedChanged += cb_CheckedChanged;**
        }
    }
}


private void cb_CheckedChanged(Object sender, EventArgs e)
{
    string NameSet  = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
    MessageBox.Show(NameSet);
}

MyControls 类的代码如下:

class MyControls
{
    int x=5;
    int y=30;
    public CheckBox cb = new CheckBox();
    public TextBox tb1 = new TextBox();

    public TextBox tbSpecs = new TextBox();
    public TextBox tb3 = new TextBox();
    public TextBox tb4 = new TextBox();

    public void initElements(String[] name, int i)
    {
        cb.Width = 10;
        cb.Height = 10;
        cb.Name = "cb_" + i.ToString();
        cb.Location = new Point(x, y+5);
        cb.Checked = false;
        Form.ActiveForm.Controls.Add(cb);

        x += 15;

        tb1.Width = 50;
        tb1.Height = 20;
        tb1.Location = new Point(x, y);
        tb1.Name = "tb1_" + i.ToString();
        tb1.Text = name[0];
        Form.ActiveForm.Controls.Add(tb1);
        x += 60;

        tbSpecs.Width = 150;
        tbSpecs.Height = 20;
        tbSpecs.Name = "tb2_" + i.ToString();
        tbSpecs.Text = name[1];
        tbSpecs.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tbSpecs);
        x += 160;

        tb3.Width = 40;
        tb3.Height = 20;
        tb3.Name = "tb3_" + i.ToString();
        tb3.Text = name[2];
        tb3.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb3);
        x += 50;

        tb4.Width = 450;
        tb4.Height = 20;
        tb4.Name = "tb4_" + i.ToString();
        tb4.Text = name[3];
        tb4.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb4);

        x = 0;
    }

    public int SetX(int X)
    {
        x = X;
        return x;
    }

    public int SetY(int Y)
    {
        y = Y;
        return y;
    }
}

【问题讨论】:

  • MyControls.cs 是一个类。它包含 4 个文本框和 1 个复选框。我现在添加它
  • 大卫,当我点击复选框时,我希望复选框功能运行。但它不起作用。
  • 这是winforms还是webforms?
  • 我的一个小毛病,但完全跑题了——String.Split() 的参数是“params char[]”,所以你不需要实际构建一个新的 char 数组。你可以做 str.Split('_');
  • 与此类似,这条线mc.cb.CheckedChanged += cb_CheckedChanged; 是否曾经受到打击?

标签: c# winforms checkbox


【解决方案1】:
mc.cb.CheckedChanged += new System.EventHandler(this.cb_CheckedChanged)

【讨论】:

  • 不能直接分配委托,必须通过EventHandler。
  • 据我所知,编译器会为您隐式执行此操作。
【解决方案2】:

签到CreateFormElements(int i, StringReader sr){...}的几点:

i 是否大于零?否则,循环将永远不会运行,并且事件处理程序将永远不会被附加。

line.Length 是否永远大于零?如果不是,您将永远无法进入 if 块,并且处理程序将不会被附加。

【讨论】:

  • 问题解决了乔伊,谢谢,现在我遇到了另一个问题:) 很快我会发布它。我需要更改文本框的位置...
猜你喜欢
  • 2011-03-27
  • 2014-02-01
  • 1970-01-01
  • 2014-12-29
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
相关资源
最近更新 更多