Search code examples
delphisubtractionadditioneditcontrol

How do I make a function to subtract and add values in a TEdit?


I'm new to programming, I'm looking to make a command to add and subtract values.

Where I would have a TEdit.Value with the initial value 0 and 2 more buttons, one of " + " and one of " - ", to increase or decrease the value, but I still don't know how to do that, can someone give me one light?

I looked up some examples of how to do this, but I didn't find anything that could help me.


Solution

  • You could use StrToInt() to convert the Edit's current Text string to an integer, then increment/decrement the integer, then use IntToStr() to convert the integer to a string and assign it back to the Text property. For example:

    var Value: Integer := StrToInt(Edit1.Text);
    Inc(Value); // or Dec()
    Edit1.Text := IntToStr(Value);
    

    However, a better option would be to use a UI control that is specifically designed to handle numeric input, such as a TSpinEdit, or a TUpDown attached to an Edit control.