G
TextBox It only works with the lines. Even if you put a number in there, it' remain a row of symbols that mark that number.It follows from the above allegation that after var balance = textBox1.Text;♪ balance to have a type stringand after var ost = balance; ♪ ost It's gonna be like, string♪textBox6 - apple; That's not what the compiler says, because the operator. - not defined for type TextBox♪How's that?There are different ways to decide, one of them, in the comments, @lexander Petrov - use the element for the entry of the cells https://msdn.microsoft.com/ru-ru/library/system.windows.forms.numericupdown(v=vs.110).aspx (for use, see reference documentation), but it's not all that simple. First, this controller uses a type. Decimal as values, and it's still gonna have to lead to the numerical types you need, for example. int♪ Second, it requires a clear indication of the maximum and minimum values. In some cases it's good, in some cases it's not. Thirdly, the control requires a clear indication of the number of signs after the comma and a step to change the value of arrows up/downwards that cannot be concealed. Perhaps the characteristics listed will not be a deficiency in your specific case, if so, simply a substitute. TextBox ♪ NumericUpDownand use the explicit introduction of the types for the transmission of values to your variables, for example int a = Convert.ToInt32(numericUpDown1.Value);
или так
int a = (int)numericUpDown1.value;
The other option is to transform it into into TextBox row, if possible, and if not possible (the user has not imposed a number), erroneous entry or simply repaint the background, thus making it clear that the value introduced does not meet the requirements of the programme.This option will require some of the following:Transformation string Number.Use class https://msdn.microsoft.com/ru-ru/library/system.convert(v=vs.110).aspx (referencing)Use the method https://msdn.microsoft.com/ru-ru/library/b3h1hf19(v=vs.110).aspx (referencing)Use the method https://msdn.microsoft.com/ru-ru/library/f02979c7(v=vs.110).aspx (referencing)The first two options would lead to exclusion when attempting to transform the wrong entry line. TryParseUnlike them, he returns true if conversion is successful and records the converted value into a variable transferred to the second mandatory retrofit parameter outor false In all other cases, the content of the variable to which the value is to be placed will not change. It's convenient, because we can place this method in a condition. if♪ By the way, Convert♪ Parseand TryParse Applicable to all standard numerical types.To let the user know he didn't make the right point.It can be done differently, but in any case, we'll need to know that the user has actually introduced something. This is what we need to take advantage of. https://msdn.microsoft.com/ru-ru/library/system.windows.forms.control.textchanged(v=vs.110).aspx ♪ The reference also gives an example of a change in the colour of the text with an incorrect entry.I wanted the text box to take the variable meaning.To this end, one of the ways mentioned above should transform the meaning TextBox.Text of string Number required, complete deduction from the value obtained and record the result back TextBox.Text by means of a method ToString()which, for any numerical type, returns his symbolic performance.In case of use NumericUpDown A little easier: numericUpDown1.Value -= 10;
или
int a = 10;
numericUpDown1.Value -= a;
Another important point is the visibility of variables. The local variables you announce in the method are visible only within this method. That means you can't treat them in other ways. For the variable to be visible to all class methods, it needs to be made by the field or class properties, and declared outside the method, before or after, it doesn't matter, it's not really inside.There's only one thing left to decide what kind of decision you're going to do and how to practice it.