-
February 25th, 2010, 02:27 PM
#16
Re: Numbers Only Textboxes!!
Rocky, a couple of things:
1) please remember to post WPF related items in the WPF forum.
2) when someone gives you code, try the code before asking other questions. For example, rather than speculating that the code won't work with TABS, you can load up the code and determine that yourself.
As far as moving the post..., I'm going to leave this post in this forum because the solution is a C# solution.
If you still need a WPF solution, please post another question in the WPF forum.
-
February 25th, 2010, 10:21 PM
#17
Re: Numbers Only Textboxes!!
Hi! Arjay, with regards
Thaks for the suggestion. IT was stupid of me not to remember that I was working on WPF...And not trying code given by foamy, was all my bad but i had seen the same code on 1 other site where many users complained that tab key was not working,so i posted my reply... but I Beg my pardon to foamy for complaining before trying out myself...
THANKS
-
February 26th, 2010, 08:51 AM
#18
Re: Numbers Only Textboxes!!
Quick comment on Foamy's solution, that would still let people copy & paste invalid characters into the text box, wouldn't it?
-
March 5th, 2010, 03:43 AM
#19
Re: Numbers Only Textboxes!!
Originally Posted by Zaccheus
Quick comment on Foamy's solution, that would still let people copy & paste invalid characters into the text box, wouldn't it?
EDIT: I should test things properly before posting
It seems if you use the right click->insert method, you can insert invalid text to it ... I will have to take a look at that. Also, CTRL + V doesn't work
There we go:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace Foamy
{
public class NumericTextBox : TextBox
{
bool allowSpace = false;
bool changing = 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
e.Handled = true;
}
}
protected override void OnTextChanged(EventArgs e)
{
if (!changing)
{
changing = true;
//Check the text and make sure no invalid characters were pasted.
for (int i = this.Text.Length - 1; i >= 0; i--)
{
char c = this.Text[i];
if (!Char.IsDigit(c) && c != '\b' && !(this.allowSpace && c == ' '))
this.Text = this.Text.Remove(i, 1);
}
changing = false;
}
base.OnTextChanged(e);
}
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;
}
}
}
}
Update: added base.OnTextChanged(e); to enable eventhandling by user
Last edited by foamy; March 5th, 2010 at 05:29 AM.
It's not a bug, it's a feature!
-
March 10th, 2010, 11:56 AM
#20
Re: Numbers Only Textboxes!!
This code by foamy is a work of art!
Following on from what someone else had suggested - how can I validate the text when [OK] is pressed in my form?
Sorry to ninja the thread, but I hope others would be wondering the same.
Thanks
-
March 10th, 2010, 12:10 PM
#21
Re: Numbers Only Textboxes!!
What sort of validation?
You can handle the OK button press event, and do conditional checks there. Perhaps a regex validator. Plenty of options.
-
March 10th, 2010, 10:52 PM
#22
Re: Numbers Only Textboxes!!
I still think vaildation is easier, either in response to the [Ok] button or when the Textbox loses focus.
However, it may be better to do it at key stroke time, but that can lead to much more complicated code. This case would surely be better written as a UserControl beased on the original TextBox control.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
March 11th, 2010, 02:31 AM
#23
Re: Numbers Only Textboxes!!
Originally Posted by Ibzy
This code by foamy is a work of art!
Following on from what someone else had suggested - how can I validate the text when [OK] is pressed in my form?
Sorry to ninja the thread, but I hope others would be wondering the same.
Thanks
Woah.. I can't take full credit for this. Although I wrote some of it, it's based on someone else's code. Probably from CodeProject.com, but I can't remember
Originally Posted by rliq
I still think vaildation is easier, either in response to the [Ok] button or when the Textbox loses focus.
However, it may be better to do it at key stroke time, but that can lead to much more complicated code. This case would surely be better written as a UserControl beased on the original TextBox control.
Easier when starting from scratch maybe. But with this thing you can just drag it into the form and not have to worry.
I've never used UserControls, do you have an example of how this might work as one? It'd be a nice addition to the thread ^^
Last edited by foamy; March 11th, 2010 at 02:34 AM.
It's not a bug, it's a feature!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|