Results 1 to 3 of 3

Thread: Re: Insert only numbers in textbox

  1. #1

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Insert only numbers in textbox

    How can I Force the user to insert only numbers in a textbox?
    is there a way to do this in c#?
    Last edited by Hack; Apr 12th, 2007 at 01:56 PM.

  2. #2

    Thread Starter
    Addicted Member KingSatan's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Angry Re: Insert only numbers in textbox

    i figured you could loop the the textbox.text and delete characters which arent numbers

    but i dont know how to do this

    so i tried something like this

    Code:
    string txtPriceRangeSpecialAddNewString;
    		
    
    private int checkdec()
    		{ txtPriceRangeSpecialAddNewString=txtPriceRangeSpecialAddNew.Text.ToString();
    			int x = 0;
    			for(int i =0; i<txtPriceRangeSpecialAddNew.Text.Length; i++)
    			{
    			string CurrentChar = txtPriceRangeSpecialAddNewString.Substring(i,1);
    				try
    				{
    				Convert.ToInt32(CurrentChar);
    				}
    				catch(System.FormatException ex)
    				{
    					if(CurrentChar == ".")
    					{
    					x++;
    					}
    				}
    			}
    			return x;
    		}
    and the method to delete the characters which arent numbers
    Code:
    private string DeleteChar()
    		{txtPriceRangeSpecialAddNewString=txtPriceRangeSpecialAddNew.Text.ToString();
    		for(int i=0 ; i<txtPriceRangeSpecialAddNew.Text.Length; i++)
    			{
    				string CurrentChar=txtPriceRangeSpecialAddNewString.Substring(1,i);
    				try
    				{
    				Convert.ToInt32(CurrentChar);
    				}
    				catch(FormatException ex)
    				{
    					if(CurrentChar!=".")
    					{
    					txtPriceRangeSpecialAddNewString=txtPriceRangeSpecialAddNewString.Remove(i,1);
    					i--;
    					lblDGMIError.Text=ex.ToString();
    					}
    				}
    			}
    		return txtPriceRangeSpecialAddNewString;
    		}
    			void txtPriceRangeAddNew_TextChanged(object sender, System.EventArgs e)
    			{	string pricerange = txtPriceRangeSpecialAddNewString;
    				checkdec();
    				DeleteChar();	
    				
    			}
    the methods to run both methods
    Code:
    void txtPriceRangeSpecialAddNew_TextChanged(object sender, System.EventArgs e)
    		{
    				string pricerange = txtPriceRangeSpecialAddNewString;
    			checkdec();
    			DeleteChar();	
    				
    		}
    but it gives me an error saying Index and length must refer to a location within the string. Parameter name: length
    Last edited by KingSatan; Apr 13th, 2007 at 01:03 PM.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Allow Only Numbers In TextBox

    Split from this thread in ClassicVb.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width