【问题标题】:Make form function accessible to all forms使所有表单都可以访问表单功能
【发布时间】:2026-01-27 01:55:01
【问题描述】:

我在Form1 上有此功能,我想对Form2Form3 等使用相同的功能,而不是在每个表单上重复该功能有什么方法可以让所有人都可以访问它?我尝试创建一个新的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(); 只能在活动表单上工作,但是如何使它在Form 类之外工作?

【问题讨论】:

  • 由于该方法没有任何状态,您可以将其作为静态方法放入静态类中。
  • 设置你的方法 public static void 然后你可以通过 formName.methodName() 从其他表单调用它
  • 关于第二次更新。这是 TextBox 扩展方法的签名。扩展方法应该遵循它们的规则。请查看csharp.net-tutorials.com/csharp-3.0/extension-methods

标签: c# forms function public-method


【解决方案1】:

这是您的代码的真正简化版本。 要创建一种可在任何地方重用的方法,请创建一个包含简单静态类的新 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 设计器,但您需要手动将事件处理程序添加到您的文本框中在 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');
}

【讨论】:

  • 好的,非常感谢,我会使用设计师的方式,因为它更干净。
【解决方案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);

【讨论】:

    【解决方案3】:

    有一个 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);
    }
    

    【讨论】: