使表单功能可以访问所有表单

时间:2013-04-25 11:48:13

标签: c# forms function public-method

我在Form1上有这个功能,我想对Form2Form3等使用相同的功能,而不是重复每个Form上的函数有什么办法使所有人都可以访问?我试图创建一个新的Class : Form,然后从表单调用函数,但没有工作...

public void tb_Leave(object sender, EventArgs e)
{
    if ((sender as TextBox).Text.Count() < (sender as TextBox).MaxLength)
        (sender as TextBox).Text = (sender as TextBox).Text.PadLeft((sender as TextBox).MaxLength, '0');
}

更新

感谢您的回答,它们运行正常,但如果我想对X文本框使用相同的方法呢? (就像我在使用tb_Leave函数一样)

我的意思是,我的老方法,我只是选择X文本框和休假事件发送到我的功能,你提到我需要创建调用一个辅助类中的另一个方法的方法的方式......但我仍然需要在每个表单中创建一个方法,调用该类,我是对的吗?虽然,您的答案实际上非常有用,因为我只需要创建一个包含所有帮助程序类的新.cs文件:)

更新2 我在迁移此方法时遇到问题

public static void TextBoxKeyDown(this TextBox tb, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Enter:
            case Keys.Add:
                e.SuppressKeyPress = true;
                processTabKey(true);
                break;
            case Keys.Decimal:
                if (tb.Tag == "importe")
                {
                    e.SuppressKeyPress = true;
                    processTabKey(true);
                }
                break;
            case Keys.Subtract:
                e.SuppressKeyPress = true;
                processTabKey(false);
                break;
        }
    }

我当然知道processTabKey();只适用于活动表单,但如何让它在de Form类之外工作?

3 个答案:

答案 0 :(得分:2)

这是您的代码的真正简化版本。 要创建一个在任何地方重用的方法,请创建一个包含简单静态类

的新Utility.cs文件
namespace MyApp.Utilities
{
    public static class MyUtility
    {
        public static void PadForTextBox(TextBox tb)
        {
            if (tb.Text.Length < tb.MaxLength)
                tb.Text = tb.Text.PadLeft(tb.MaxLength, '0');
        }
    }
}

现在,您可以从每个具有对您定义类的命名空间的引用的表单中调用此方法。

public void tb_Leave(object sender, EventArgs e)
{
    Utility.PadForTextBox(sender as TextBox);
}

实现相同结果的另一种优雅方法是通过TextBox的扩展方法

namespace MyApp.Utilities
{
    public static class TextBoxExtensions
    {
        public static void PadForTextBox(this TextBox tb)
        {
            if (tb.Text.Length < tb.MaxLength)
                tb.Text = tb.Text.PadLeft(tb.MaxLength, '0');
        }
    }
}

并用

调用它
public void tb_Leave(object sender, EventArgs e)
{
    (sender as TextBox).PadForTextBox();
}

顺便说一句,使用这些方法可以让你摆脱那些丑陋的演员序列。

当然你的tb_Leave方法是一个事件处理程序,因为它应该链接到文本框。
如果您希望在应用程序中的每个文本框中都有一个通用的文本框事件处理程序,而不依赖于创建文本框的表单,那么您不能依赖WinForm Designer,但是您需要手动将事件处理程序添加到文本框中。在InitializeComponent调用之后的表单构造函数。总而言之,我更喜欢将此任务留给设计师,并在需要时添加上面的单行。 例如:

InitializeComponent();
// connect the leave event for 3 textboxes to the same static method inside the
// MyUtility static class
textBox1.Leave+=MyUtility.PadEventForTextBox;
textBox2.Leave+=MyUtility.PadEventForTextBox;
textBox3.Leave+=MyUtility.PadEventForTextBox;

.....

public static void PadEventForTextBox(object sender, EventArgs e)
{
    TextBox tb=sender as TextBox;
    if (tb.Text.Length<tb.MaxLength)
        tb.Text=tb.Text.PadLeft(tb.MaxLength, '0');
}

答案 1 :(得分:2)

创建一个新的辅助静态类,如:

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;

public class HelperClass
{
    public static void LeaveMethos(object sender, EventArgs e)
    { 
        if ((sender as TextBox).Text.Count() < (sender as TextBox).MaxLength)
            (sender as TextBox).Text = (sender as TextBox).Text.PadLeft((sender as TextBox).MaxLength, '0');
    }
}

并在您的文本框离开事件中调用它:

HelperClass.LeaveMethos(sender, e);

答案 2 :(得分:1)

有一个Helpers.cs文件(或任何你喜欢的文件),并制作一个Helper方法,例如:

Yournamespace.Helpers
{
    public string GetTextBoxText(TextBox sender)
    {
        if (sender.Text.Count() < sender.MaxLength)
        return sender.Text.PadLeft(sender.MaxLength, '0');
    }
}

然后只需在表单中包含命名空间并使用:

public void tb_Leave(object sender, EventArgs e)
{  
   TextBox textBox = sender as TextBox
   textBox.Text = GetTextBoxText(textBox);
}
相关问题