Recent

Author Topic: Change button at run time (Loop) [SOLVED]  (Read 247 times)

AlphaInc.

  • New Member
  • *
  • Posts: 13
Change button at run time (Loop) [SOLVED]
« on: September 17, 2020, 12:08:59 am »
Hello everybody,

I want to implement a speedbutton that changes it caption at run time. There should be different modes and captions.

For example the speedbutton has the caption „helloWorld“ as default. When i push it it changes the caption to „hello1“ and executes a cmd-script (hello1.bat). When i press the button a second time it should change the caption to „hello2“ and execute another cmd-script (hello2.bat). And, when i press the button again, it should change back to „hello1“ so that it creates a loop (it should always change from hello1 to hello 2 to hello1 and so on.)

I hope it get’s clear what i want and someone can help me, since i’m a total noob.
« Last Edit: September 17, 2020, 10:31:48 am by AlphaInc. »

speter

  • New Member
  • *
  • Posts: 12
Re: Change button at run time (Loop)
« Reply #1 on: September 17, 2020, 01:50:48 am »
Try something like this; it doesn't run a batch file - it simply writes a message in a memo - but you'll probably get the idea:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Buttons, StdCtrls,
  9.   ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Memo1: TMemo;
  17.     SpeedButton1: TSpeedButton;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure SpeedButton1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.     counter : byte;
  24.   end;
  25.  
  26.   Trec = record
  27.     cap, msg : string;
  28.   end;
  29.   Tinfo = array [0..2] of Trec;
  30. var
  31.   Form1: TForm1;
  32. const
  33.   info : Tinfo = ((cap:'Hello 1'; msg:'message 1'),
  34.                   (cap:'Hello 2'; msg:'message 2'),
  35.                   (cap:'Hello 3'; msg:'message 3'));
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.FormCreate(Sender: TObject);
  44. begin
  45.   counter := 0;
  46.   speedbutton1.caption := info[counter].cap;
  47. end;
  48.  
  49. procedure TForm1.SpeedButton1Click(Sender: TObject);
  50. begin
  51.   // do something...
  52.   memo1.lines.add(info[counter].msg);
  53.  
  54.   inc(counter);
  55.   if counter = 3 then counter := 0;
  56.  
  57.   speedbutton1.caption := info[counter].cap;
  58. end;
  59.  
  60. end.

AlphaInc.

  • New Member
  • *
  • Posts: 13
Re: Change button at run time (Loop)
« Reply #2 on: September 17, 2020, 10:31:32 am »
Thank you, speter, that was what i was looking for.  :)

martijnn

  • New Member
  • *
  • Posts: 14
Re: Change button at run time (Loop) [SOLVED]
« Reply #3 on: September 17, 2020, 11:55:19 am »
Although already solved it might be worth trying to address this simply with a few TAction instances. As there are only two states to change to that could be done something like:

  • Drop a TActionList on your form (Standard tab)
  • Double-click and add two actions "acCmd1", "acCmd2". Change captions as you like e.g. "hello1", "hello2"
  • For above 2 implement the OnExecute handlers (Object Inspector --> Events tab)   (event see code below)
  • Select the speedbutton and choose "acCmd2" from the Action property dropdown
  • Additionally set the speedbutton initial caption to "helloWorld"

Event handlers code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.acCmd1Execute(Sender: TObject);
  2. begin
  3.   // switch to state 2
  4.   Memo1.Lines.Add('Executed 2nd command');
  5.   SpeedButton1.Action := acCmd2;
  6. end;
  7.  
  8. procedure TForm1.acCmd2Execute(Sender: TObject);
  9. begin
  10.   // switch to state 1
  11.   Memo1.Lines.Add('Executed 1st command');
  12.   SpeedButton1.Action := acCmd1;
  13. end;
  14.  

Assigning the Action property of your speedbutton updates the caption automatically. Some benefits of using actions is that you can assign shortcut keys and reuse them at other places (e.g. the menu bar).
« Last Edit: September 17, 2020, 11:58:27 am by martijnn »

 

TinyPortal © 2005-2018