You can't.
That has nothing to do with overloading.
Overloading: Haveing different versions of the same method, distinguished by having differing parameters. For example:
public void Print(int i) { Console.WriteLine("Integer: {0}", i);}
public void Print(float f) { Console.WriteLine("Float: {0}", f);}
What you want to do is call the same method as an event handler from many different files.
What you are trying to do is to make it
static
- which means it is shared and does not refer to any specific instance of the class it is a part of. But that won't work either, because the designer always refers to the class instance when adding eveny handlers:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MyForm_KeyDown);
So, the best you can do is declare a
static
method, which accepts the KeyPressEventArgs and call it from each of your individual handlers:
public static void IgnoreUnwantedKeys(KeyPressEventArgs e)
{
int x = e.KeyChar;
if (x != 8)
{
if (!(x >= 48 && x <= 57))
e.Handled = true;
}
}
public void keypress(object Sender, KeyPressEventArgs e)
{
function.IgnoreUnwantedKeys(e);
}