Re: Is this Curve to the Left or to the Right

Posted by Guido, DelphiLand Team on April 03, 2006

In Reply to Is this Curve to the Left or to the Right posted by Lionel on April 01, 2006

: I ask the user to select which way a curve turns in the code below by letting him/her indicate by typing in L or R in an Edit10.Text box. For some unknown (Unknown to me) reason, I am getting the wrong response?
: This might be handled better by using two CheckBoxes (Check Left or Check Right) but I am getting a wrong response with this also.
: ***********************************
: I don't know if I should stick with a Edit Box entry or to use two CheckBoxes, one for Left and one for Right?

: I also tried RadioButtons, but when I select Left or Right the other buttons loose their settings?

The easiest way to tackle this type of choice is either using a RadioGroup component, or a set of RadioButton components.

1. RadioGroup component: at design time, give it two items labeled "Right" and "Left". TRadioGroup.ItemIndex holds the number of the selected radio button, the first button is 0. If you want one of the buttons to be pre-selected when the application starts, assign that button to ItemIndex at design time; otherwise, leave ItemIndex set to its default value of -1, which means that no button is pre-selected.

In the calculation part of the program, you check which item was selected like this: (we assume that one of the 2 buttons was pre-selected by the program)

procedure TForm1.ButtonCalcClick(Sender: TObject);
begin
  // ...
  if RadioGroupRL.ItemIndex = 0 then begin // Right 
    // ...
  end
  else begin // Left
    // ...
  end;
  // ...
end;

2. A set of RadioButton components.
Important: if you have *several* sets of RadioButtons (for example, one set for Right/Left, another set for Red/Green/Blue, and so on...) then the RadioButtons must be grouped. RadioButtons are grouped if they are contained by the same "container", such as a TPanel, TGroupBox or TForm.
For example, put 2 RadioButtons on Panel1 and 3 RadioButtons on Panel2. Note: if they were all 5 *directly* on Form1, then selecting RadioButton 1 would deselect all the others!

In the calculation part of the program, you check which RadioButton was selected like this: (we assume that one of the 2 buttons was pre-selected by the program)

procedure TForm1.ButtonCalcClick(Sender: TObject);
begin
  // ...
  if RadioButton1.Checked then begin // Right
    // ...
  end
  else begin // Left
    // ...
  end;
  // ...
end;
-------------------------------

If no RadioGroup item (1) or RadioButton (2) was pre-selected by the program, then the code of ButtonCalcClick must also check for the possibility that the user didn't select any radio button, for example:

procedure TForm1.ButtonCalcClick(Sender: TObject);
begin
  // ...
  if RadioGroupLR.ItemIndex = 0 then begin // Right
    // ...
  end
  else if RadioGroupLR.ItemIndex = 1 then begin // Left
    // ...
  end
  else begin // nothing was selected
    ShowMessage('Please select RIGHT or LEFT'); 
    exit; // don't calculate!
  end;
  // ...
end;

3. You can also use an Edit component, but then it's a more complicated to check if the users' input was valid. You expect "R" or "L", but the user can type "r" or "l" or "right" or simply nothing...
Example: use Edit10 as in your original question:

procedure TForm1.ButtonCalcClick(Sender: TObject);
begin
  // ...
  if UpperCase(Edit10.Text) = 'R' then begin // Right
    // ...
  end
  else if UpperCase(Edit10.Text) = 'L' then begin // Left
    // ...
  end
  else begin // something else or nothing was entered
    ShowMessage('Please enter R or L'); // error message
    exit; // don't calculate!
  end;
  // ...
end;
 

Related Articles and Replies