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
and the method to delete the characters which arent numbersCode: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; }
the methods to run both methodsCode: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(); }
but it gives me an error saying Index and length must refer to a location within the string. Parameter name: lengthCode:void txtPriceRangeSpecialAddNew_TextChanged(object sender, System.EventArgs e) { string pricerange = txtPriceRangeSpecialAddNewString; checkdec(); DeleteChar(); }





Reply With Quote