0

我试图从另一个类调用一个方法,但在该方法内部,从第一个类调用一个方法......我无法解释得更好,所以这就是我想要用代码做的......

我的类.cs

public static void validarCampos(object sender) {
**some code here**
}

// here, a KeyDown function calls MyHelper.cs=>TextBoxKyeDown method
private void tb_KeyDown(object sender, KeyEventArgs e)
{
    (sender as TextBox).TextBoxKeyDown(e, this);
}

我的助手.cs

public static void TextBoxKeyDown(this TextBox tb, KeyEventArgs e, Control container)
{
    switch (e.KeyCode)
    {
        case Keys.Enter:
        case Keys.Add:
            tb.ZeroFill(e);
            // I want to call MyClass.cs=>validarCampos(tb);
            // here, before it moves to next TB, because on
            // validarCampos(tb) I can tell if the next TB is
            // enabled or not, if I do not call it HERE
            // when I press ENTER or ADD, it wont move next TB
            // until I press it twice...
            e.SuppressKeyPress = true;
            container.SelectNextControl(tb, true, true, false, true);
            break;
        case Keys.Decimal:
            if ((tb.Tag as string) == "importe")
            {
                e.SuppressKeyPress = true;
                container.SelectNextControl(tb, true, true, false, true);
            }
            break;
        case Keys.Subtract:
            e.SuppressKeyPress = true;
            container.SelectNextControl(tb, false, true, false, true);
            break;
    }
}

真的很抱歉解释,如果你需要更多线索告诉我......我不粘贴整个valdarCampos代码,因为它是〜140行......它只是检查TextBoxes的内容并确定哪些是启用或禁用取决于结果...

4

2 回答 2

3

这是一个公共静态方法,所以你可以这样称呼它:

MyClass.validarCampos(tb);
于 2013-05-09T13:52:27.810 回答
1

If the helper method varies depending on context, then take a look at Action delagates. You could pass the function in as a parameter to TextBoxKeyDown. I'd expect the function to look something like:

void TextBoxKeyDown(this TextBox tb, KeyEventArgs e, Control container, 
                    Action<object> CallBack)
{
   CallBack(tb);
}

and could be called with:

(sender as TextBox).TextBoxKeyDown(e, this, validarCampos);
于 2013-05-09T13:56:52.163 回答