Change button caption text...

Simple question:
I have only 1 button.Its caption is
"yes".Now,when I click on it I want
the caption to chanhe to "No" and
viceversa...

EnejAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
King_DavidConnect With a Mentor Commented:
Thanks.
0
 
King_DavidCommented:
Simple answer:

if Button.Caption = 'Yes' then
  Button.Caption := 'No'
else
  Button.Caption := 'Yes';
0
 
wimmeyvaertCommented:
Hello,

The easiest way to do this is really
by checking the button's caption and
change it to the caption you want
This is the code to do this
(the code is, like you see, in the Onclick-Event of the button) :

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Button1.Caption = '&Yes' then
    Button1.Caption := '&No'
  else
    Button1.Caption := '&Yes';
end;


BUT !!
I think that you want the button to do different things, depending on his caption.
Then I suggest you just put 2 buttons on your Form with the same coordinates (Top, Left, Width, Height).
The only thing you have to do then is be sure that when button1 is pressed, his code is executed and button2 becomes visible (i.e. put button1 to the background).
Below the code :


procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.SendToBack;
  // The code for Button1 should be put here.
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Button2.SendToBack;
  // The code for Button2 should be put here.
end;



I Hope this answer is good enough for you. Good Luck
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AttarSoftwareCommented:
Or of course, the nicer way would surely be to redirect the OnClick event...

procedure tForm1.ClickEvent1( sender : tObject ) ;
begin
  ShowMessage( 'YES!' ) ;
  Button1.Caption := '&No!' ;
  Button1.OnClick := ClickEvent2 ;
end ;

procedure tForm1.ClickEvent2( sender : tObject ) ;
begin
  ShowMessage( 'NO!' ) ;
  Button1.Caption := '&Yes!' ;
  Button1.OnClick := ClickEvent1 ;
end ;

Tim.

0
 
EnejAuthor Commented:
What I am trying to do is actually
to make mediaplayer1 play...
Something like:
if Button1.Caption = '&Play' then
Mediaplayer1.Play
Button1.Caption := '&Stop'
else
Button1.Caption := '&Play';
mediaplayer1.Stop
When I put in Mediaplayer1.Play I get
error (;) not allowed before ELSE?!
So,how is this supposed to be written..?

0
 
King_DavidCommented:
if Button1.Caption = '&Play' then
  begin
    MediaPlayer1.Play;
    Button1.Caption := '&Stop';
  end
else
  begin
    Button1.Caption := '&Play';
    MediaPlayer1.Stop;
  end;
0
 
CalvinDayCommented:
Use the ol' begin-end block.

if Button1.Caption = '&Play' then
  begin
  Mediaplayer1.Play;
  Button1.Caption := '&Stop';
  end
else
  begin
  Button1.Caption := '&Play';
  mediaplayer1.Stop;
  end;
0
 
CalvinDayCommented:
Why you little fast typer.
0
 
EnejAuthor Commented:
King David :
Points go to you as your answer is OK!
0
All Courses

From novice to tech pro — start learning today.