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 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. 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... 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
|