Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace HUMAN_RESOURCES.MANAGEMENT
{
    class function
    {
        public void keypress(object Sender, KeyPressEventArgs e)
        {
            int x = e.KeyChar;
            if (x != 8)
            {
                if (!(x >= 48 && x <= 57))
                    e.Handled = true;
            }
        }
    }
}

this is my class were i have written a key press event code for numbers,
i want to call this code in my different form

where i can use this code by overloading please help me i am in trouble ,,,
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-12 4:53am    
I don't know computer language called "Beginner". I don't know such platform. Please put relevant tags.
--SA

1 solution

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:
C#
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:
C#
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:

C#
public static void IgnoreUnwantedKeys(KeyPressEventArgs e)
{
    int x = e.KeyChar;
    if (x != 8)
    {
        if (!(x >= 48 && x <= 57))
            e.Handled = true;
    }
}

C#
public void keypress(object Sender, KeyPressEventArgs e)
{
    function.IgnoreUnwantedKeys(e);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900