problem in cs file for validation
hello every one,
i have very big problem . i want to create cs file for validation where i can make function for numeric validation and character validation when i m created its give me error i m sending u what kind of function i m making on there please check it and reply me if u know easy way to do same thing then plz let me know..........
code//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ContaxSoftware
{
class Validation
{
public void price_validation(object sender, KeyPressEventArgs e)
{
int num = e.KeyChar;
if (e.KeyChar == '.' || num == 8)
{
e.Handled = false;
}
else if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
thanks in Advcance

theLizardPosted Dec 3, 2009, 4:18 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class validate
{
public validate()
{
}
public bool isDigitKey(char key)
{
return(Char.IsDigit(key)? true : false);
}
public bool isCharKey(char key)
{
return(!Char.IsDigit(key)? true : false);
}
}
}
Key press event
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
validate v = new validate();
e.Handled = v.isDigitKey(e.KeyChar);
if(!e.Handled)
{
//do what you want...
}
}
But it might be better to validate in the key up event
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
validate v = new validate();
if(!v.isDigitKey((char)e.KeyCode)) //e.Handled its not a digit
{
//is it character aA-zZ
if(!v.isCharKey((char)e.KeyCode)) //its not a valid character
e.Handled = false;
//do what you want...
}
}
Much simpler don't you think...
But you could also do this in the key up event without a class
if(Char.IsDigit((char)e.KeyCode)? true : false)
e.Handled = true;
if(!Char.IsDigit((char)e.KeyCode)? true : false)
e.Handled = true;
Much more simpler to do...
Kirtan PatelPosted Dec 3, 2009, 4:42 AM
----------------
class Class1
{
public static void PriceValidation(KeyPressEventArgs e)
{
int num = e.KeyChar;
if (e.KeyChar == '.' || num == 8)
{
e.Handled = false;
}
else if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
Using the Class1 PriceValidate Method in Form
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
Class1.PriceValidation(e);
}
}
}
Naeem KhanPosted Dec 3, 2009, 3:56 AM
Kirtan PatelPosted Dec 3, 2009, 3:39 AM
if my answer helps you then check "Do you like this answer" please :)
1. Add the name space
using System.Windows.Forms;
2. you forgot to close the braces of class and Name space :)
hope this will solve your problem :)
so Final Code is below
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
class Validation
{
public void price_validation(object sender, KeyPressEventArgs e)
{
int num = e.KeyChar;
if (e.KeyChar == '.' || num == 8)
{
e.Handled = false;
}
else if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
}