Hi Friends,

I have a numbervalidation method that validate phone no textbox.
What i need is i have different windows forms, so where can i define this method so that i can access it on different forms globally.
Like in VB.NET we have Modules, is their something in C#.NET too.
I am using C# over windows form.
Code is as below:

 public void Validation_Number(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Only Digits are Allowed");
            }

        }

With c# it's a little more complicated

Add a new code file to your project
create a namespace for it
create a static class in that namespace
add whatever variables,methods you want to that class, remember to mark them public and static:

namespace Globals
{
    public static class Global
    {
        public static string test = "";
        public static void testThis()
        {

        }
         public static void Validation_Number(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Only Digits are Allowed");
            }
        }            
    }
}

now any file in your project that wants to access the members of that class just needs to add a using statement for that namespace:

 using Globals;

and access the members of the class

Global.test = "test";

For event handlers you'll need to add them in code rather than in the Designer:

Form1()
{
    InitializeComponent();    
    textBox1.KetPress += Global.Validation_Number;
}

One caveat to this is event handlers that access other controls on the form. Those controls will need their Modifiers set to atleast Internal. There may be other thing needed depending on your requirements.

Edited 2 Weeks Ago by tinstaafl

I don't like to annoy my users with messages saying: "He, stupid! Only digits allowed here!"
When I wanted only real numbers entered in a textbox I use this snippet