smithvoice.com
 Y'herd thisun? 

“It is my understanding that NASA Headquarters recently lost hundreds of positions, and after those halls were emptied no one could remember what in the world all those people did.”

from Getting to Space by Dr. Jerry Pournelle

Integer-only Textbox

TaggedCoding, CSharp

I was working on a project today and needed an integer-only textbox and I thought 'don't I have one of these?'

Yep, found it... it's in the free image control eBook.

But I was typing in c#2005 so it had to be ported.

To save you the effort if you need one with braces, here's the port.

To use it just copy the text into two text files (PosIntTextBox.cs and PosIntTextBox.Designer.cs) and then do Add > Existing Item and add both to your project then recompile and you'll see it in the toolbox. Or do Add > User Control give it a filename of 'PosIntTextBox' then open each of the cs files, do SelectAll and replace the complete default text with the appropriate text copied from below.

PosIntTextBox.cs

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
  
public partial class PosIntTextBox : TextBox
private int m_MinValue = 0;
private int m_MaxValue = int.MaxValue;
private int m_DefaultValue = 0;
  
//wish c# had static variables.
//Wish more C# people would just look at VB and see that
//   they should be asking MS for a bunch of great existing things
private bool m_bFixing = false;
 
public PosIntTextBox()
{
InitializeComponent();
this.Text = "0";
}
private void PosIntTextBox_Layout(object sender, LayoutEventArgs e)
{
this.Text = m_DefaultValue.ToString();
this.MaxLength = m_MaxValue.ToString().Length + 1;
}
public int DefaultValue
{
get { return m_DefaultValue; }
set
{
 if (value < m_MinValue || value > m_MaxValue)
 {
 m_DefaultValue = (m_MaxValue - m_MinValue) / 2;
 }
 else
 {
 m_DefaultValue = value;
 }
}
}
public override int MaxLength
{
get
{
 return base.MaxLength;
}
set
{
 if (value != (m_MaxValue.ToString().Length + 1))
 {
 value = m_MaxValue.ToString().Length + 1;
 }
  
 base.MaxLength = value;
}
}
public int MinValue
{
get { return m_MinValue; }
set 
{
 if (value <= m_MaxValue)
 {
 m_MinValue = value;
 if (m_DefaultValue < m_MinValue)
 
 m_DefaultValue = m_MinValue;
 }
 if( Convert.ToInt32(this.Text) < m_MinValue)
 {
 this.Text = FixEntry(this.Text);
 }
 }
}
}
public int MaxValue
{
get { return m_MaxValue; }
set
{
 if (value >= m_MinValue)
 {
 m_MaxValue = value;
 base.MaxLength = m_MaxValue.ToString().Length + 1;
 if (m_DefaultValue > m_MaxValue)
 {
 m_DefaultValue = m_MaxValue;
 }
 if(Convert.ToInt32(this.Text) > m_MaxValue)
 {
 this.Text = FixEntry(this.Text);
 }
 }
}
}
protected override void OnTextChanged(System.EventArgs e )
{
if (m_bFixing)
{
 return;
}
m_bFixing = true;
int iCurPos = this.SelectionStart;
string sOrig = this.Text;
string sFixed = FixEntry(sOrig);
if (sOrig != sFixed)
{
 this.Text = sFixed;
}
if (this.Text == "0")
{
 this.SelectAll();
}
else if (iCurPos > 0)
{
 this.SelectionStart = iCurPos;
}
m_bFixing = false;
}
 
private string FixEntry(string value)
{
string s = value.Trim().Replace(" ", "");
long lVal = 0;
char[] arc = s.ToCharArray();
s = "";
for (int i = 0; i < arc.Length; i++)
{
 if (IsNumeric(arc[i].ToString()))
 {
 s += arc[i];
 }
}
if (s.Length == 0)
{ return m_DefaultValue.ToString(); }
else
{
 lVal = Convert.ToInt64(s);
 if (lVal > (long)m_MaxValue)
 {
 return m_MaxValue.ToString();
 }
 else if (lVal < (long)m_MinValue)
 {
 return m_MinValue.ToString();
 }
 else
 {
 return lVal.ToString();
 }
}
}
private bool IsNumeric(string value)
{
int iTemp = 0;
try
{
 iTemp = int.Parse(value);
 return true;
}
catch (FormatException)
{
 return false;
}
}
}

PosIntTextBox.Designer.cs

 

using System;
partial class PosIntTextBox: System.Windows.Forms.TextBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">if managed resources should be disposed</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
 components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// PosIntTextBox
//
this.Layout += new System.Windows.Forms.LayoutEventHandler(this.PosIntTextBox_Layout);
this.ResumeLayout(false);
}
#endregion
}

 

Hope it helps! (and even if you're a VB-hater check out that eBook, gotta admit it's pretty neat <g>)

Robert Smith
Kirkland, WA



home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better smithvoice.com