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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
King_DavidCommented:
Thanks.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.