How to checkText Box value is a String or Float?
-
hi all, How to check value of a Text Box is a String or Float? this.textBox4.Text I want to check above value is a String Value or Float Value.. please help.. Thanks in advance.
-
hi all, How to check value of a Text Box is a String or Float? this.textBox4.Text I want to check above value is a String Value or Float Value.. please help.. Thanks in advance.
In a short I can only tell about the "dirty" way.. bool isFloat = false; float value = 0f; try { value = float.Parse(this.textBox4.Text, System.Globalization.NumberStyles.Float); isFloat = true; } catch { // isFloat is not set to true so it's not a float value. } By trying to parse the input text as float, you can determine wether the input is a valid float as string or not. If not, an exception will be thrown upon "float.Parse(string)" and isFloat will not be set to true. Someone correct me, if I missed something on that example *cough* :) Daniel Monzert
-
In a short I can only tell about the "dirty" way.. bool isFloat = false; float value = 0f; try { value = float.Parse(this.textBox4.Text, System.Globalization.NumberStyles.Float); isFloat = true; } catch { // isFloat is not set to true so it's not a float value. } By trying to parse the input text as float, you can determine wether the input is a valid float as string or not. If not, an exception will be thrown upon "float.Parse(string)" and isFloat will not be set to true. Someone correct me, if I missed something on that example *cough* :) Daniel Monzert
Zarrab wrote: In a short I can only tell about the "dirty" way.. bool isFloat = false; float value = 0f; try { value = float.Parse(this.textBox4.Text, System.Globalization.NumberStyles.Float); isFloat = true; } catch { // isFloat is not set to true so it's not a float value. } There is a neat way to do this: the static method
Double.TryParse
. You can bring it back to a float (or integer) afterwards if you want.