Question

I have this function on Form1, and I want to use the same function for Form2, Form3 and so on, instead of duplicate the function on each Form is there any way to make it accessible for all? I've tried to make a new Class : Form and then call the function from the forms, but not working...

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

Thank you for your answers, they work fine, but what if I want to use the same method for X textboxes? (Like I was doing with the tb_Leave function)

I mean, with my old method I just select X textboxes and send the leave event to my function, the way you mention I need to create a method to call another method within a helper class... But I still need to create a method inside every form, to call that class, am I right? Although, your answers are very helpful actually as I only need to create a new .cs file with all my helper classes :)

UPDATE 2 I'm having problems migrating this method

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

Of course I know processTabKey(); will only work on the active form, but how to make it working outside de Form class?

Was it helpful?

Solution

This is a really simplified version of your code. To create a method for reuse everywhere create a new Utility.cs file containing a simple static class

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

Now you could call this method from every form that has a reference to the namespace where you define the class.

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

Another elegant method to achieve the same result is through an extension method for the 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');
        }
    }
}

and call it with

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

By the way, using these methods allow you to get rid also of that ugly sequence of casts.

Of course your tb_Leave method is an event handler and as that it should be linked to the textboxes.
If you want to have a textbox event handler common for every textboxes in you apps indipendently from the form in which the textboxes are created, then you cannot rely on the WinForm Designer, but you need to add manually the event handler to your textboxes in the form constructor just after the InitializeComponent call. All in all I prefer to leave this task to the designer and add the single line above when needed. For example:

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

OTHER TIPS

Create a new helper static class like :

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

and in your Leave Event for textbox just call it :

HelperClass.LeaveMethos(sender, e);

Have a Helpers.cs file (or whatever you like), and make a Helper method, such as this:

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

Then simply include the namespace in your form and use this:

public void tb_Leave(object sender, EventArgs e)
{  
   TextBox textBox = sender as TextBox
   textBox.Text = GetTextBoxText(textBox);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top