Re: Numbers Only Textboxes!!
I can't remember where I originally got this, but it might be close to what you need:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace Namespace
{
public class NumericTextBox : TextBox
{
bool allowSpace = false;
// Restricts the entry of characters to digits (including hex), the negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;
string keyInput = e.KeyChar.ToString();
if (Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
}
else if (e.KeyChar == '\b')
{
// Backspace key is OK
}
// else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
// {
// // Let the edit control handle control and alt key combinations
// }
else if (this.allowSpace && e.KeyChar == ' ')
{
}
else
{
// Consume this invalid key and beep
e.Handled = true;
// MessageBeep();
}
}
public int IntValue
{
get
{
return Int32.Parse(this.Text);
}
}
public decimal DecimalValue
{
get
{
return Decimal.Parse(this.Text);
}
}
public bool AllowSpace
{
set
{
this.allowSpace = value;
}
get
{
return this.allowSpace;
}
}
}
}
Re: Numbers Only Textboxes!!
Quote:
Originally Posted by
rocky_upadhaya
this method does not allow the use of tabs for shifting through the textboxes
what kind of method?
have you tried to catch the KeyDown event?
Re: Numbers Only Textboxes!!
I will look into the method that memelo gave...but for now I have somewhat a solution in this
Code:
try
{
if(Convert.ToInt32(a1.Text)==5)
//code
if(Convert.ToInt32(b1.Text)==6)
//code
}
catch(FormatException)
{
}
but can I know exactly which which statement generated the exception??
Re: Numbers Only Textboxes!!
Quote:
Originally Posted by
rocky_upadhaya
I will look into the method that memelo gave...but for now I have somewhat a solution in this
Code:
try
{
if(Convert.ToInt32(a1.Text)==5)
//code
if(Convert.ToInt32(b1.Text)==6)
//code
}
catch(FormatException)
{
}
but can I know exactly which which statement generated the exception??
Uhm...I don't think this a good solution to your problem. Why don't use the NumericTextBox that 'Foamy' posted? If you don't want to create a new control perhaps you can use a regular expression validator? That way the user get's some feedback and you wont' have to worry about invalid values...
Re: Numbers Only Textboxes!!
actually you should use the code @foamy posted. it exacly what you're looking for.
@foamy: I didn't know that the Char class has so cool methods like IsDigit. thx for posting it.
Re: Numbers Only Textboxes!!
Thanks for the suggestion fells...now i will surely use the code example posted by the foamy...but I am not sure it will support tab to move through the textboxes.....but if it is possible I will update my work and its result....Thank you guys........
Re: Numbers Only Textboxes!!
Alternatively, let them type anything they want but then validate it, either as textbox loses focus (can be painful) or when they clikc [OK] on the form (assuming that is an option).
Re: Numbers Only Textboxes!!
any example or little insight on that? so that i can compare both the methode and evaluate which one suites me...coz i dont want to loose tab key navigation through textboxes...thanks
Re: Numbers Only Textboxes!!
Code:
private void textBox1_KeyPress( object sender, KeyPressEventArgs e )
{
if( e.KeyChar == (char)Keys.Tab ) return;
e.Handled = !Char.IsDigit( e.KeyChar );
}
That is obviously a contrived example, it just shows how to keep tab working.
Re: Numbers Only Textboxes!!
Quote:
Originally Posted by
rliq
Alternatively, let them type anything they want but then validate it, either as textbox loses focus (can be painful) or when they clikc [OK] on the form (assuming that is an option).
A good suggestion. I would go for this option unless there was a specific requirement to validate as the data in entered.
rliq: I've just noticed .NET 4 in your location. how are you finding .NET 4? Are you using VS2010 or you just using the SDK? What about the new language features?
Re: Numbers Only Textboxes!!
Um, tabbing works fine with the control I posted :)
(Just tested it to make sure)
Glad everyone likes it - I just wish the original author had put his/her name in the comments :-P
Re: Numbers Only Textboxes!!
if you validate it at the 'end', when the user clicks [OK] you won't lose the tab functionality. Is there some reason why you have to evaluate after each character is typed, as that can start to complicate your code.
Re: Numbers Only Textboxes!!
Quote:
Originally Posted by
rliq
if you validate it at the 'end', when the user clicks [OK] you won't lose the tab functionality. Is there some reason why you have to evaluate after each character is typed, as that can start to complicate your code.
Why let users enter bad data to begin with? It is just annoying, and it is trivial to pass the Tab messages through the control. I would ask why anyone *would* validate at the end instead of up front.
Re: Numbers Only Textboxes!!
I am extreamly sorry for not mentioning that I was working on WPF and is the code given by foamy is not valid for WPF Applications?? because if comes with
Code:
Error 1 The type or namespace name 'KeyPressEventArgs' could not be found (are you missing
a using directive or an assembly reference?)
I am again sorry for my mistake...anyway this thread would help other guys......and I urge Moderators of this site to change shift this thread to WPF section.......