I've just begun to learn C# and am trying to create a text box that will accept only integers and backspace key. I've created the code for doing that within the 'KeyPress' event for the text box:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
I realize that I can use a MaskedTextBox, but my real interest is in trying to understand how to use the above example to better understand OOP. I would like to create a generic method that can be called upon from any number of text boxes and I'm not sure how to do that for a KeyPress event. If someone can provide an example with some explanatory notes, I would greatly appreciate it.
Thank you,
imadman
Scott LyslePosted Oct 15, 2008, 1:16 AM
Create a custom control by extending the textbox; here is an example of a control built to display currency:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MuControls
{
///
/// C U R R E N C Y T E X T B O X C O N T R O L
///
/// Extended textbox control; the purpose of the control is
/// to permit the user to enter values as currency
///
public partial class CurrencyTextBox : TextBox
{
///
/// member variable used to keep dollar value
///
private Decimal mDollarValue;
///
/// constructor
///
public CurrencyTextBox()
{
InitializeComponent();
DollarValue = 0;
}
///
/// Default OnPaint
///
///
protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
}
///
/// Limit user input to numbers, decimals, and control chars
///
///
///
private void CurrencyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
// allows only numbers, decimals and control characters
if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && this.Text.Contains("."))
{
e.Handled = true;
}
if (e.KeyChar == '.' && this.Text.Length < 1)
{
e.Handled = true;
}
}
///
/// Valid inputs into the control and show as currency
///
///
///
private void CurrencyTextBox_Validated(object sender, EventArgs e)
{
try
{
// format the value as currency
Decimal dTmp = Convert.ToDecimal(this.Text);
this.Text = dTmp.ToString("C");
}
catch { }
}
///
/// Manange entry into the control whenever it is selected;
/// make sure that the dollar value is set and the cursor
/// positioned to end of the string
///
///
///
private void CurrencyTextBox_Click(object sender, EventArgs e)
{
this.Text = mDollarValue.ToString();
if (this.Text == "0")
this.Clear();
this.SelectionStart = this.Text.Length;
}
///
/// Update the property containing the decimal value;
/// this is to support display and edit of value
/// after it has been converted to a currency string
/// in the application
///
///
///
private void CurrencyTextBox_TextChanged(object sender, EventArgs e)
{
try
{
DollarValue = Convert.ToDecimal(this.Text);
}
catch { }
}
///
/// property to maintain value of control
///
public decimal DollarValue
{
get
{
return mDollarValue;
}
set
{
mDollarValue = value;
}
}
}
}
Scott LyslePosted Oct 15, 2008, 1:16 AM
Create a custom control by extending the textbox; here is an example of a control built to display currency:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MuControls
{
///
/// C U R R E N C Y T E X T B O X C O N T R O L
///
/// Extended textbox control; the purpose of the control is
/// to permit the user to enter values as currency
///
public partial class CurrencyTextBox : TextBox
{
///
/// member variable used to keep dollar value
///
private Decimal mDollarValue;
///
/// constructor
///
public CurrencyTextBox()
{
InitializeComponent();
DollarValue = 0;
}
///
/// Default OnPaint
///
///
protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
}
///
/// Limit user input to numbers, decimals, and control chars
///
///
///
private void CurrencyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
// allows only numbers, decimals and control characters
if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.' && this.Text.Contains("."))
{
e.Handled = true;
}
if (e.KeyChar == '.' && this.Text.Length < 1)
{
e.Handled = true;
}
}
///
/// Valid inputs into the control and show as currency
///
///
///
private void CurrencyTextBox_Validated(object sender, EventArgs e)
{
try
{
// format the value as currency
Decimal dTmp = Convert.ToDecimal(this.Text);
this.Text = dTmp.ToString("C");
}
catch { }
}
///
/// Manange entry into the control whenever it is selected;
/// make sure that the dollar value is set and the cursor
/// positioned to end of the string
///
///
///
private void CurrencyTextBox_Click(object sender, EventArgs e)
{
this.Text = mDollarValue.ToString();
if (this.Text == "0")
this.Clear();
this.SelectionStart = this.Text.Length;
}
///
/// Update the property containing the decimal value;
/// this is to support display and edit of value
/// after it has been converted to a currency string
/// in the application
///
///
///
private void CurrencyTextBox_TextChanged(object sender, EventArgs e)
{
try
{
DollarValue = Convert.ToDecimal(this.Text);
}
catch { }
}
///
/// property to maintain value of control
///
public decimal DollarValue
{
get
{
return mDollarValue;
}
set
{
mDollarValue = value;
}
}
}
}