Monday, 4 February 2019

Extension methods in C#

Extension methods in C#


public static void AllowWholeNumberOnly(this KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar)) e.Handled = true;
if (e.KeyChar == (char)8) e.Handled = false;
}

public static void AllowNumbericValueOnly(this KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar)) e.Handled = true;
if (e.KeyChar == (char)8) e.Handled = false;
if (e.KeyChar == (char)46) e.Handled = false;
}

public static bool AllowDecimalValueOnly(object sender, KeyPressEventArgs e)
{
var IsValid = false;
IsValid = (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)) ? true : false;

// checks to make sure only 1 decimal is allowed
if (e.KeyChar == 46)
{
IsValid = ((sender as TextBox).Text.IndexOf(e.KeyChar) != -1) ? true : false;
}

return IsValid;
}

public static void AllowAlphabeticalValueOnly(this KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar)) e.Handled = true;
if (e.KeyChar == (char)8) e.Handled = false;
if (e.KeyChar == (char)46) e.Handled = false;
}

No comments:

Post a Comment

Factorial of a Number

Recently Viewed