Recent

Author Topic: TCaption as a value  (Read 3065 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
TCaption as a value
« on: February 17, 2016, 06:27:08 pm »
I'm trying to make a calculator programme for fun.

Originally each button click applied a value and this is applied to the calculation.
Seeing all these button clicks made me ask the question of how necessary all this is.

So, would it be possible to have a procedure called with each numeric button keypress where, based on the TCaption of the Sender, ie. 1,2,3 etc., the value of the TCaption is then applied to the calculation ?

Furthermore, would it be possible for the keypress events of the numeric buttons to be seen by a single event on the Form rather than have 10 separate Buttonclick events ?

If I can use the Caption as a value, is TCaption regarded as a string.

I'm asking all this to see how small the code can be for such a project and is its possible before I spend hours pulling my hair out on an impossible idea.

Thanks for any input.


balazsszekely

  • Guest
Re: TCaption as a value
« Reply #1 on: February 17, 2016, 06:33:32 pm »
There is a Tag property, use that. First button's tag should be 0, second 1, etc...Create the on click event for only one button, then copy paste(object inspector) for all other:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button0Click(Sender: TObject);
  2. begin
  3.   case (Sender as TButton).Tag of
  4.     0: //button 0 pressed
  5.     1: //button 1 pressed;
  6.   end;
  7. end;
  8.  

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: TCaption as a value
« Reply #2 on: February 17, 2016, 06:37:45 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonsClick(Sender: TObject);
  2. begin
  3.   Edit1.Text := Edit1.Text + (Sender as TButton).Caption;
  4. end;
  5.  

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: TCaption as a value
« Reply #3 on: February 17, 2016, 06:57:29 pm »
By the way, FPC supports case with strings.
So, you can write:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonsClick(Sender: TObject);
  2. begin
  3.   case (Sender as TButton).Caption of
  4.   '0','1','2','3','4','5','6','7','8','9': ShowMessage('Digit');
  5.   '+','-','*','/': ShowMessage('Operation');
  6.   '=': ShowMessage('Equal sign');
  7.   else
  8.     ShowMessage('Other');
  9.   end;
  10. end;
  11.  

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TCaption as a value
« Reply #4 on: February 17, 2016, 07:08:33 pm »
Thanks for the swift response, both of you.
I particularly like the tag idea.

FTurtle, your original post was the sort of idea I originally had but GetMem's single event for all is just what I was after.

That said, FTurtle, your second post also gives some ideas.

Thanks !

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: TCaption as a value
« Reply #5 on: February 17, 2016, 07:35:06 pm »
Create the on click event for only one button, then copy paste(object inspector) for all other:

There is an easier way:

1. Create event handler for one button.
2. Rename that function (or not).
3. Select all buttons in designer via Shift-Click or in tree of objects in Object Inspector.
4. Select Events tab in Object Inspector.
5. Choose your function in drop-down list at OnClick.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TCaption as a value
« Reply #6 on: February 17, 2016, 07:50:29 pm »
Create the on click event for only one button, then copy paste(object inspector) for all other:

There is an easier way:

1. Create event handler for one button.
2. Rename that function (or not).
3. Select all buttons in designer via Shift-Click or in tree of objects in Object Inspector.
4. Select Events tab in Object Inspector.
5. Choose your function in drop-down list at OnClick.

Yes, just did that, looked on here for more replies and you posted this. Strange !

 

TinyPortal © 2005-2018