Recent

Author Topic: One button with two functions  (Read 5923 times)

Chocolate123

  • Newbie
  • Posts: 1
One button with two functions
« on: December 16, 2012, 09:06:20 pm »
Hello,
im new programming and Lazarus. I need some help.
Im making a really simple program, and a part of it is that One button writes and deletes the text in Form1.
So my problem is i really dont know how to write the code.

TL;DR: Need a code for program that when you click on a button, it writes out a text, and if you click on it again, it deletes the text. Basicly 1 button 2 functions.

Thank you for your help.

lainz

  • Guest
Re: One button with two functions
« Reply #1 on: December 16, 2012, 09:21:40 pm »
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Form1.Caption <> '' then
    Form1.Caption := ''
  else
    Form1.Caption := 'Hi';
end;

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: One button with two functions
« Reply #2 on: December 16, 2012, 09:32:01 pm »
 :D
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain1.Button2Click(Sender: TObject);
  2. const Captions: array[ boolean ] of string = ('', 'Howdy world');
  3. begin
  4.   Caption := Captions[ Caption = '' ]
  5. end;
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

lainz

  • Guest
Re: One button with two functions
« Reply #3 on: December 16, 2012, 10:30:31 pm »
nice, it's so nice!

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: One button with two functions
« Reply #4 on: December 16, 2012, 10:50:28 pm »
Yes, paranoia can be a science. Now my paranoia is directed to user experience.

Paranoids (like me) who can become programmers are lucky men.
« Last Edit: December 16, 2012, 11:08:48 pm by typo »

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: One button with two functions
« Reply #5 on: December 17, 2012, 08:52:33 pm »
I know this has been answered but:

I would like to add one thing, if you are going to use one command button for more then one function
I would recomend that you change the command caption accordingly per the new task of the
buttons function etc.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if edit1.Text <> '' then
     begin
  edit1.text := '';
  button1.Caption := 'Write';
     end

  else
    begin
  edit1.text := 'You Text Here';
   button1.caption := 'Erase';
    end;
end;     

In this way you will allow the user to know whats going on, making your app user
friendly, so assuming your app starts out with empty text in edit1,  make your caption
at design time for button1 to read Write,  so when you run  and click on button1 two things
will happen,  The caption of the button will change to erase, and edit1 will now have the text
you want, so next time you click the button, edit1 value will clear and caption for button1 will change to
write again. Hope this helps
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: One button with two functions
« Reply #6 on: December 17, 2012, 09:06:15 pm »
Changing button's label is confusing, I recommend you: don't do it.
Or at least use TToggleBox (on Standard palette). Or better, use two buttons and change their property "Enabled" to True or False.

Even professionals do not recommend it: http://techbase.kde.org/Projects/Usability/HIG/Buttons
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: One button with two functions
« Reply #7 on: December 17, 2012, 09:21:26 pm »
Hello,
im new programming and Lazarus. I need some help.
Im making a really simple program, and a part of it is that One button writes and deletes the text in Form1.
So my problem is i really dont know how to write the code.

TL;DR: Need a code for program that when you click on a button, it writes out a text, and if you click on it again, it deletes the text. Basicly 1 button 2 functions.

Thank you for your help.


I agree however the user states  in his/her post (1 button 2 functions)  so in this case
its not a bad idea to use caption changing events. since it would be easier for the user
to know whats going on as apposed to ok i wrote the text, how in the world do i erase
it now. Since there is no communication between the user and the program.   That was
my point :)  So using two buttons defeats the authors request.
« Last Edit: December 17, 2012, 09:23:56 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

 

TinyPortal © 2005-2018