PC Review


Reply
Thread Tools Rate Thread

Customized TextBox

 
 
=?Utf-8?B?S2V2ZW4gQ29yYXp6YQ==?=
Guest
Posts: n/a
 
      16th Dec 2004
Hi, I need to extend the behaviour of TextBox control and add to it the
option to accept only integer or float number and so on.

Does any one know if there is some code already written from which start or
better if there is some control with this functionality ready to use ?

Thank you in advance.

Keven Corazza
 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      16th Dec 2004
The project here
http://www.codeproject.com/vb/net/maskedbox_control.asp
To create a masked textbox might provide a few pointers, the code is for the
desktop but you should be able to modify it to suit your project.

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.opennetcf.org

"Keven Corazza" <(E-Mail Removed)> wrote in message
news:9F303961-C870-4FE1-BD7E-(E-Mail Removed)...
> Hi, I need to extend the behaviour of TextBox control and add to it the
> option to accept only integer or float number and so on.
>
> Does any one know if there is some code already written from which start
> or
> better if there is some control with this functionality ready to use ?
>
> Thank you in advance.
>
> Keven Corazza



 
Reply With Quote
 
PeterB
Guest
Posts: n/a
 
      16th Dec 2004
I have been looking for something similar myself but found nothing really
useful. Truth is it is quite hard to create a textbox with this
functionality because of some limitations in .NET Compact Framework.

If I understand Keven right, the article on code project won't help him very
much, especially if he is working in C#.

OpenNETCF.org has it's TextBoxEx that you can force to be "numeric only". It
does have some major limitations though, such as not beeing able to set
negative numbers and decimal numbers. Anyway, I think that is a better
start...

I'm actually working on this kind of TextBox, but I am quite a newbie when
it comes to creating custom controls, and it is not a final product yet for
a couple of days.

/ Peter


"Peter Foot [MVP]" <(E-Mail Removed)> skrev i meddelandet
news:%(E-Mail Removed)...
> The project here
> http://www.codeproject.com/vb/net/maskedbox_control.asp
> To create a masked textbox might provide a few pointers, the code is for
> the desktop but you should be able to modify it to suit your project.
>
> Peter
>
> --
> Peter Foot
> Windows Embedded MVP
> www.inthehand.com | www.opennetcf.org
>
> "Keven Corazza" <(E-Mail Removed)> wrote in message
> news:9F303961-C870-4FE1-BD7E-(E-Mail Removed)...
>> Hi, I need to extend the behaviour of TextBox control and add to it the
>> option to accept only integer or float number and so on.
>>
>> Does any one know if there is some code already written from which start
>> or
>> better if there is some control with this functionality ready to use ?
>>
>> Thank you in advance.
>>
>> Keven Corazza

>
>



 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      16th Dec 2004
You could, with the help of a native DLL, subclass the EDIT control that
you're working with and have it respond to some extra messages telling it
whether float or integer data is to be accepted and to have it do the right
things to prevent alpha characters (for example), from being added to the
EDIT text.

Paul T.

"PeterB" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I have been looking for something similar myself but found nothing really
>useful. Truth is it is quite hard to create a textbox with this
>functionality because of some limitations in .NET Compact Framework.
>
> If I understand Keven right, the article on code project won't help him
> very much, especially if he is working in C#.
>
> OpenNETCF.org has it's TextBoxEx that you can force to be "numeric only".
> It does have some major limitations though, such as not beeing able to set
> negative numbers and decimal numbers. Anyway, I think that is a better
> start...
>
> I'm actually working on this kind of TextBox, but I am quite a newbie when
> it comes to creating custom controls, and it is not a final product yet
> for a couple of days.
>
> / Peter
>
>
> "Peter Foot [MVP]" <(E-Mail Removed)> skrev i meddelandet
> news:%(E-Mail Removed)...
>> The project here
>> http://www.codeproject.com/vb/net/maskedbox_control.asp
>> To create a masked textbox might provide a few pointers, the code is for
>> the desktop but you should be able to modify it to suit your project.
>>
>> Peter
>>
>> --
>> Peter Foot
>> Windows Embedded MVP
>> www.inthehand.com | www.opennetcf.org
>>
>> "Keven Corazza" <(E-Mail Removed)> wrote in message
>> news:9F303961-C870-4FE1-BD7E-(E-Mail Removed)...
>>> Hi, I need to extend the behaviour of TextBox control and add to it the
>>> option to accept only integer or float number and so on.
>>>
>>> Does any one know if there is some code already written from which start
>>> or
>>> better if there is some control with this functionality ready to use ?
>>>
>>> Thank you in advance.
>>>
>>> Keven Corazza

>>
>>

>
>



 
Reply With Quote
 
Michael Lipp [MSFT]
Guest
Posts: n/a
 
      17th Dec 2004
The NumberOnlyTextBox class below subclasses the TextBox control so it only
accepts numbers:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;

public class Test : System.Windows.Forms.Form
{
public Test()
{
this.Text = "numbers only";
this.MinimizeBox = false;

NumberOnlyTextBox x = new NumberOnlyTextBox();
x.Parent = this;
}

static void Main()
{
Application.Run(new Test());
}
}


/// <summary>
/// Summary description for NumberOnlyTextBox.
/// </summary>
public class NumberOnlyTextBox : 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(this.allowSpace && e.KeyChar == ' ')
{

}
else
{
// Swallow this invalid key
e.Handled = true;
}
}

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;
}
}
}


 
Reply With Quote
 
Michael Lipp [MSFT]
Guest
Posts: n/a
 
      17th Dec 2004
The NumberOnlyTextBox class below subclasses the TextBox control so it only
accepts numbers:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;

public class Test : System.Windows.Forms.Form
{
public Test()
{
this.Text = "numbers only";
this.MinimizeBox = false;

NumberOnlyTextBox x = new NumberOnlyTextBox();
x.Parent = this;
}

static void Main()
{
Application.Run(new Test());
}
}


/// <summary>
/// Summary description for NumberOnlyTextBox.
/// </summary>
public class NumberOnlyTextBox : 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(this.allowSpace && e.KeyChar == ' ')
{

}
else
{
// Swallow this invalid key
e.Handled = true;
}
}

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;
}
}
}



---------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.


 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      17th Dec 2004
I think there's still a hole there: copy/paste. And, of course, you can
have any number of decimal points in the text... Those things can be
addressed, though!

Paul T.

"Michael Lipp [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The NumberOnlyTextBox class below subclasses the TextBox control so it
> only
> accepts numbers:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> using System;
> using System.Drawing;
> using System.Windows.Forms;
> using System.Globalization;
>
> public class Test : System.Windows.Forms.Form
> {
> public Test()
> {
> this.Text = "numbers only";
> this.MinimizeBox = false;
>
> NumberOnlyTextBox x = new NumberOnlyTextBox();
> x.Parent = this;
> }
>
> static void Main()
> {
> Application.Run(new Test());
> }
> }
>
>
> /// <summary>
> /// Summary description for NumberOnlyTextBox.
> /// </summary>
> public class NumberOnlyTextBox : 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(this.allowSpace && e.KeyChar == ' ')
> {
>
> }
> else
> {
> // Swallow this invalid key
> e.Handled = true;
> }
> }
>
> 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;
> }
> }
> }
>
>
>
> ---------------------------------
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[2008] Clear contents of textbox, reverting transaction with textbox method undo Rob W Microsoft VB .NET 0 6th Mar 2009 10:39 AM
Textbox input from a customized location Sujay Ghosh Microsoft Dot NET Framework Forms 1 16th Feb 2008 01:45 PM
HELP! I Lost The Ability To Advance From TextBox To TextBox With the ENTER Or The TAB Keys Minitman Microsoft Excel Programming 0 22nd Feb 2005 08:50 PM
Setting the Control source of a textbox to a query based on the valueof another textbox on the same form Joey Microsoft Access Queries 1 26th Jan 2005 03:35 AM
Customized "read" form for IPM.note with image and textbox Volker Bartheld (SPAM only) Microsoft Outlook Form Programming 0 23rd Nov 2004 02:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:12 AM.