I’m working on a school project where I have 10 textboxes (5 pairs).
The idea is that the sum of each pair can at most be “3”. So
[1] [2]
[1] [0]
…
[2] [1]
I figured that checking this during a “onChange” event is too time consuming so I decided to do it with a button which checks all of them. It will throw a showmessage() as soon as any pair does not satisfy the requirement.
Now is there a way to go over each pair and check if their value is less than or equal to three and is NOT negative? I know I can do it manually by writing a big chunk of code, but I would like to keep it as clean as possible.
Thanks!
Drop ten
TEditcontrols on a form. Since you have created these manually, you need to make them easily accessible in code, and there is no super-elegant way to do this. But this works:In your implementation section, define
and add a private field
FEditors: array[0..4] of TEditorPair;to your form class. And doNow, select all your ten editor controls, and add a common
OnChangeevent to them.Sample compiled EXE
If you are in to code golf, the
EditorChangeprocedure can be shortened towhere we make use of a couple of ‘tricks’ such as boolean short-circuit (lazy) evaluation.