2013-04-25 48 views
1

我在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'); 
} 

谢谢你的建议。

UPDATE

谢谢你的答案,他们的工作很好,但如果我想使用的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类以外工作?由于

+1

由于该方法没有任何状态,你可以将它作为静态方法放入静态类中。 – sloth 2013-04-25 11:52:49

+0

设置你的方法public static void,然后你可以通过formName.methodName() – Obama 2013-04-25 11:56:01

+0

关于第二个更新来从其他窗体调用它。这是TextBox扩展方法的签名。扩展方法应遵循其规则。请检查http://csharp.net-tutorials.com/csharp-3.0/extension-methods/ – Steve 2013-04-25 16:55:19

回答

2

这是一个真正简化的代码版本。 要创建可重用的方法到处创建一个包含一个简单的静态类

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'); 
     } 
    } 
} 

现在你可以调用从有到您定义的类的命名空间的引用每个表单此方法的新Utility.cs文件。

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'); 
} 
+0

好的非常感谢你,我会用设计师的方式,因为它更干净。 – 2013-04-25 13:56:57

+0

如果你能看到我的第二次更新,谢谢 – 2013-04-25 15:29:03

1

有一个Helpers.cs文件(或任何你喜欢的),做出一个辅助方法,比如这个:

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); 
} 
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); 
相关问题