Recent

Author Topic: Case Statement Syntax  (Read 3640 times)

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Case Statement Syntax
« on: November 24, 2016, 06:07:53 pm »
can any1 help me figure out proper syntax for the following code snippet?

Code: Pascal  [Select][+][-]
  1. [  case of TradioButton
  2.       radiobutton1: SaveDialog1.Filename := 'HeliTour.xml';
  3.       radiobutton2: SaveDialog1.Filename := 'SeaPlane.xml';
  4.     end; code]
  5.  
  6. thx in adv
  7.  
  8. Greer

howardpc

  • Hero Member
  • *****
  • Posts: 3549
Re: Case Statement Syntax
« Reply #1 on: November 24, 2016, 06:20:26 pm »
Simplest would be to drop a TRadioGroup on your form, and add two Items to it - HeliTour, Seaplane.
Then add an OnClick handler thus (untested):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.RadioGroup1Click(Sender: TObject);
  2. begin
  3.   case (Sender as TRadioGroup).ItemIndex of
  4.     0: SaveDialog1.Filename := 'HeliTour.xml';
  5.     1: SaveDialog1.Filename := 'SeaPlane.xml';
  6.   end;
  7. end;  

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: Case Statement Syntax
« Reply #2 on: November 24, 2016, 06:22:36 pm »
I've modified and extended the code you provided, but when I click on the load or save buttons, the relevant dialog opens, but filename field doesn't have anything in it.

If a radio button is checked, I need the user to then be able to click on either the load or save button and load or save the file.

ANyway, I'
m posting the code below to let u see where I'm at..

Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   CheckLst, ExtCtrls, Grids, ComCtrls, Types;
  10.  
  11. type
  12.  
  13.   { TForm3 }
  14.  
  15.   TForm3 = class(TForm)
  16.     Button1: TButton;
  17.     Button4: TButton;
  18.     Button5: TButton;
  19.     Button6: TButton;
  20.     GroupBox2: TGroupBox;
  21.     GroupBox3: TGroupBox;
  22.     GroupBox4: TGroupBox;
  23.     Image1: TImage;
  24.     Image2: TImage;
  25.     Label1: TLabel;
  26.     Label3: TLabel;
  27.     OpenDialog1: TOpenDialog;
  28.     ProgressBar1: TProgressBar;
  29.     RadioButton1: TRadioButton;
  30.     RadioButton10: TRadioButton;
  31.     RadioButton11: TRadioButton;
  32.     RadioButton12: TRadioButton;
  33.     RadioButton13: TRadioButton;
  34.     RadioButton14: TRadioButton;
  35.     RadioButton15: TRadioButton;
  36.     RadioButton2: TRadioButton;
  37.     RadioButton3: TRadioButton;
  38.     RadioButton4: TRadioButton;
  39.     RadioButton5: TRadioButton;
  40.     RadioButton6: TRadioButton;
  41.     RadioButton9: TRadioButton;
  42.     RadioGroup1: TRadioGroup;
  43.     SaveDialog1: TSaveDialog;
  44.     StringGrid1: TStringGrid;
  45.     procedure Button4Click(Sender: TObject);
  46.     procedure Button5Click(Sender: TObject);
  47.     procedure Button6Click(Sender: TObject);
  48.     procedure FormCreate(Sender: TObject);
  49.     procedure ProgressBar1ContextPopup(Sender: TObject; MousePos: TPoint;
  50.       var Handled: Boolean);
  51.     procedure RadioGroup1Click(Sender: TObject);
  52.   private
  53.     { private declarations }
  54.   public
  55.     { public declarations }
  56.  
  57.   end;
  58.     //Class Helper
  59.  
  60.  
  61. var
  62.   Form3: TForm3;
  63.  
  64. implementation
  65.  
  66. {$R *.lfm}
  67.  
  68. { TForm3 }
  69.  
  70. procedure TForm3.ProgressBar1ContextPopup(Sender: TObject; MousePos: TPoint;
  71.   var Handled: Boolean);
  72. begin
  73.  
  74. end;
  75.  
  76. procedure TForm3.Button4Click(Sender: TObject);
  77. begin
  78.   Form3.Visible := False;
  79. end;
  80.  
  81. procedure TForm3.Button5Click(Sender: TObject);
  82. begin
  83.  If (radiobutton1.checked=true) then
  84.    OpenDialog1.Filename := 'HeliTour.xml';
  85.  if OpenDialog1.Execute then
  86.    StringGrid1.LoadFromFile(OpenDialog1.FileName);
  87. end;
  88.  
  89.  
  90. procedure TForm3.RadioGroup1Click(Sender: TObject);
  91. begin
  92.  
  93.   case (Sender as TRadioGroup).ItemIndex of
  94.     0: begin
  95.         SaveDialog1.Filename := 'HeliTour.xml';
  96.         OpenDialog1.Filename := 'HeliTour.xml';
  97.        end;
  98.  
  99.     1: begin
  100.          SaveDialog1.Filename := 'SeaPlane.xml';
  101.          OpenDialog1.Filename := 'SeaPlane.xml';
  102.        end;
  103.  
  104.     2: begin
  105.          SaveDialog1.Filename := 'SglEng.xml';
  106.          OpenDialog1.Filename := 'SglEng.xml';
  107.        end;
  108.     3: begin
  109.          SaveDialog1.Filename := 'TwinEng.xml';
  110.          OpenDialog1.Filename := 'TwinEng.xml';
  111.        end;
  112.     4: begin
  113.          SaveDialog1.Filename := 'ExecJet.xml';
  114.          OpenDialog1.Filename := 'ExecJet.xml';
  115.        end;
  116.     5: begin
  117.          SaveDialog1.Filename := 'HvyMetal.xml';
  118.          OpenDialog1.Filename := 'HvyMetal.xml';
  119.        end;
  120.     6: begin
  121.          SaveDialog1.Filename := 'NAmerica.xml';
  122.          OpenDialog1.Filename := 'NAmerica.xml';
  123.        end;
  124.  
  125.     7: begin
  126.          OpenDialog1.Filename := 'Carribean.xml';
  127.          SaveDialog1.Filename := 'Carribean.xml';
  128.        end;
  129.  
  130.     8: begin
  131.          SaveDialog1.Filename := 'SAmerica.xml';
  132.          OpenDialog1.Filename := 'SAmerica.xml';
  133.        end;
  134.  
  135.     9: begin
  136.          SaveDialog1.Filename := 'Pacific.xml';
  137.          OpenDialog1.Filename := 'Pacific.xml';
  138.        end;
  139.  
  140.     10: begin
  141.          SaveDialog1.Filename := 'Europe.xml';
  142.          OpenDialog1.Filename := 'Europe.xml';
  143.        end;
  144.  
  145.     11: begin
  146.          SaveDialog1.Filename := 'Asia.xml';
  147.          OpenDialog1.Filename := 'Asia.xml';
  148.        end;
  149.  
  150.     12: begin
  151.          SaveDialog1.Filename := 'Africa.xml';
  152.          OpenDialog1.Filename := 'Africa.xml';
  153.        end;
  154.   end;
  155. end;
  156.  
  157. procedure TForm3.Button6Click(Sender: TObject);
  158. begin
  159.   if SaveDialog1.Execute then
  160.     StringGrid1.SaveToFile(SaveDialog1.FileName);
  161. end;
  162.  
  163.  
  164. procedure TForm3.FormCreate(Sender: TObject);
  165. begin
  166.  
  167. end;
  168.  
  169. end.
  170.  
  171.  

thx

Greer
« Last Edit: November 24, 2016, 08:01:59 pm by greertr »

howardpc

  • Hero Member
  • *****
  • Posts: 3549
Re: Case Statement Syntax
« Reply #3 on: November 24, 2016, 08:20:18 pm »
I think you have not used a TRadioGroup before.
You need only one TRadioGroup, and can get rid of all the individual radiobuttons.
The RadioGroup constructs the needed buttons for you.
All you have to do is type the captions of each button. Click the ellipsis [...] button by the Items property of RadioGroup1 in the Object Inspector, and enter the button captions as a list in the memo that is presented.

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: Case Statement Syntax
« Reply #4 on: November 24, 2016, 08:45:56 pm »
ou're right, I'm a noob -- I got it working using if-then's

thx for the help -- I did learn some stuff

Greer

M+AUDIO

  • New Member
  • *
  • Posts: 47
Re: Case Statement Syntax
« Reply #5 on: November 25, 2016, 09:14:13 am »
Hi,
As mentioned, a RadioGroup is preferred instead of multiple radio buttons, but IMHO for more than 4 radios, a read only ComboBox (Style = csDropDownList) would be better. Below suggestion works for both RadioGroup and CompoBox:
Add your items to the Items property in this order (the order matters) and change ItemIndex property to 0:
Code: [Select]
...HeliTour...
...SeaPlane...
...SglEng...
...TwinEng...
...ExecJet...
...HvyMetal...
...NAmerica...
...Carribean...
...SAmerica...
...Pacific...
...Europe...
...Asia...
...Africa...

And:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, Grids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     SaveFile: TButton;
  17.     OpenFile: TButton;
  18.     ComboBox1: TComboBox;
  19.     OpenDialog1: TOpenDialog;
  20.     Panel1: TPanel;
  21.     RadioGroup1: TRadioGroup;
  22.     SaveDialog1: TSaveDialog;
  23.     StringGrid1: TStringGrid;
  24.     procedure OpenFileClick(Sender: TObject);
  25.     procedure SaveFileClick(Sender: TObject);
  26.   private
  27.     function DialogFileName: String;
  28.   public
  29.  
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.SaveFileClick(Sender: TObject);
  42. begin
  43.   SaveDialog1.FileName := DialogFileName;
  44.   if SaveDialog1.Execute then
  45.     StringGrid1.SaveToFile(SaveDialog1.FileName);
  46. end;
  47.  
  48. procedure TForm1.OpenFileClick(Sender: TObject);
  49. begin
  50.   OpenDialog1.FileName := DialogFileName;
  51.   if OpenDialog1.Execute then
  52.     StringGrid1.LoadFromFile(OpenDialog1.FileName);
  53. end;
  54.  
  55. function TForm1.DialogFileName: String;
  56. begin
  57.   Result := EmptyStr;
  58.   case RadioGroup1.ItemIndex of //or ComboBox1.ItemIndex
  59.   0: Result := 'HeliTour.xml';
  60.   1: Result := 'SeaPlane.xml';
  61.   2: Result := 'SglEng.xml';
  62.   3: Result := 'TwinEng.xml';
  63.   4: Result := 'ExecJet.xml';
  64.   5: Result := 'HvyMetal.xml';
  65.   6: Result := 'NAmerica.xml';
  66.   7: Result := 'Carribean.xml';
  67.   8: Result := 'SAmerica.xml';
  68.   9: Result := 'Pacific.xml';
  69.   10: Result := 'Europe.xml';
  70.   11: Result := 'Asia.xml';
  71.   12: Result := 'Africa.xml';
  72.   end;
  73. end;
  74.  
  75. end.
  76.  
EDIT: sample project attached.
« Last Edit: November 26, 2016, 10:20:46 am by M+AUDIO »

mangakissa

  • Hero Member
  • *****
  • Posts: 1096
Re: Case Statement Syntax
« Reply #6 on: November 25, 2016, 12:53:39 pm »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

greertr

  • Full Member
  • ***
  • Posts: 113
    • Virtual Pilot Dashboard
Re: Case Statement Syntax
« Reply #7 on: November 25, 2016, 02:35:50 pm »
thx mangakissa - I got my code working already, but I'll play withthis, for sure!

I like simple :)


Greer

 

TinyPortal © 2005-2018