Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Validating user's input before creating TChart graph

tchart delphi validation

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 AlecR

AlecR

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 23 December 2014 - 08:10 AM

The user enters coefficients into input boxes onto a form, entitled GetFunction.

 

Upon clicking an 'OK' button, the input from each input box is checked. If all input boxes are valid, the procedure, CreateGraph, is called. Otherwise, it is not.

 

Here is how i did it:

procedure TfrmGetFunction.btnAddFunctionClick(Sender: TObject);
  var
    i : integer; // Loop counter.
  begin
    for i := 1 to 7 do
      begin
        CoefficientEdit[i].CoValid := CheckCoefficientBoxesValidInput(CoefficientEdit[i].CoEditBox);
        if not CoefficientEdit[i].CoValid then // If any input box is invalid.
          Exit;
      end;
    frmGraphingMode.CreateGraph; // Finally if all input boxes valid.
    frmGetFunction.Visible := false;
    frmGraphingMode.Visible := true;
  end;

This works with no errors but using Exit does concern me. Is it okay in this instance or is there something better a kind person could suggest?



#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 23 December 2014 - 08:35 AM

I don't see an issue with doing it that way. One trick I sometimes use is I'll have a boolean called bValid. For each chunk of logic, I'll wrap it in a check so that I can proceed through the procedure normally.

procedure doSomething;
var
  bValid : boolean;
begin
  bValid := true;
  //do first piece of logic that may raise issues
  if bValid then
  begin
    //do second piece of logic that may raise issues
  end;
  if bValid then
  begin
    //do third piece of logic that may raise issues
  end;
  if bValid then
  begin
    //do final piece of logic
  end;
end;  


This way, the logic just feels cleaner. I can use a second variable (bSkip or whatever) if I want to handle error messages in an else clause.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 24 December 2014 - 07:44 AM

Similar to WP's answer, I would create a flag that is set to false inside the loop if any item is invalid.

var
isValid : boolean;

begin

isValid = true;
for ........
begin
     if not valid
    begin
        isValid = false;
        invalidResponses.Add(theControlThatContainsInvalidData);
    end;
end;

if isValid == false
  return an object with a boolean value and the list of invalid items

show graph

My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#4 AlecR

AlecR

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 24 December 2014 - 08:10 AM

Thanks for the responses thus far, very much appreciated :)

 

 

Similar to WP's answer, I would create a flag that is set to false inside the loop if any item is invalid.

var
isValid : boolean;

begin

isValid = true;
for ........
begin
     if not valid
    begin
        isValid = false;
        invalidResponses.Add(theControlThatContainsInvalidData);
    end;
end;

if isValid == false
  return an object with a boolean value and the list of invalid items

show graph

 

How do I keep showing the GetFunction form with this until valid input is entered though?


Edited by AlecR, 24 December 2014 - 08:10 AM.


#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 03 January 2015 - 08:19 PM

Make a procedure for validation.

Make a while with getfunction inside, and break the loop when the validation procedure returns true.