Tell me what text to enter and save it into a variable of type string, you can use a TextBox , and if I need to enter and save numeric values int or double , what should I do?

Is there a textbox for numeric values? ( int , double , etc.). One way to use a TextBox for int or double is to use Convert.ToDouble .

The question is whether there is a TextBox for int and double, or should TextBox + Convert.ToDouble be used?

There is also a question when entering a double value, if I suppose I put a period, for a comma, an error pops up, then I need to create a method that will process the string first, replace all points with commas and remove spaces, and only then convert or does the built-in method exist?

3 answers 3

You can create your own NumericTextBox control that will accept only numbers and return an int via the IntValue property and a double via DecimalValue as described here http://msdn.microsoft.com/en-us/library/ms229644 ( v=vs.90).aspx

Alternatively, you can, through DataBindings, bind the Text property to an object property via a BindingSource, which is of a numeric type. At the same time, if the user has entered an incorrect value, he will not be able to remove the focus until he has entered the correct one.

    To replace characters in a string, use the String.Replace method (Char, Char) And then convert the string to double

    void ConvertToDoubleFromTextBox() { try { var p = Convert.ToDouble(textBox1.Text.Replace(',', '.')); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 
    • one
      crutch Don't do it - Veikedo