Hello AD1408,
Hooooooo, that's a tricky question !! I love that !
I adapted the code for the purpose you describe, but here is a little explanation :
What you want is make sure that the 4 edit fields ARE NOT empty.
To do that, YOU CAN NOT use a comparison with NULL or NIL or even with 0 :
- NIL means the variable if of type unknown, which is not true because you declared your variables as real
- NULL (IS NULL or = NULL) could be used, but not in your case because 0 value is acceptable
- For the same reason, you can not use 0 because in some case this value is acceptable.
The solution ?
You declared your variables as real, but they display in edit fields which contain text (even if you specified numbers only). So what we are going to test is that those edit fields are not empty. To do that, you can use :
This will make sure that the edit field is not empty, or
Length(Form1.Edit1.Text) <> 0;
which will test that the length of the content of the edit field is not 0
And if you want to be really thorough on your test (because some of your fields or not marked as Numbers Only), you could also make sure that white-spaces are taken into account, because they count as characters with the Trim command.
This command erases leading and trailing white-spaces.
Trim(Form1.Edit1.Text) <> '';
to make sure that the trimed content is not empty
or
Length(Trim(Form1.Edit1.Text)) <> 0;
to make sure that the Length of the Trimed content is not equal to 0 (trim before counting the length)
Now, unfortunatly, this is not enough in your case, because of the formula you use : there is a division in your second formula, and you can not divide by 0 (well, you can, but the NaN result does not interest you).
So there is a another serie a test to conduct before obtaining the result.
I added another variable in your code, that I called Divisor. Divisor is the result of
SLossPips * StdLotPipValue
If the result of this operation is 0, we know that the result of
(AccBalance * RiskPercent / 100) / Divisor;
will give you an error (the famous "invalid floating point operation you had previously).
The final code structure I opted for is like :
if any of the 4 edit fields is empty then
begin
Display a warning
end
else if none of the fields are empty then
begin
if Divisor = 0 then
begin
Display a Warning
end
else if Divisor <> 0 then
begin
Perform the calculation and display the results
end;
end;
Another possible approach would be to test that the two last edit fields are NOT EMPTY and NOT equal to 0, that(s up to you.
Hope this helped
Cheers
Mathias
I'm a very good housekeeper !
Each time I get a divorce, I keep the house
Zaza Gabor