Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2003, 07:41 AM
Micro Dot Micro Dot is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 Micro Dot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Button Caption

As someone new to delphi, I am trying to get the following loop which will number 5 buttons to work

For x:=1 to 5 do
begin
button(x).caption:=x;
end;

Can anyone tell my why this doesn't work?

Reply With Quote
  #2  
Old December 3rd, 2003, 04:21 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,506 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 23 h 42 m 25 sec
Reputation Power: 865
Are the components named Button1 ... Button5? You can't tell it to work on a component name like that. Besides, the subscripting array operator in Delphi is [], not (). Also, you need to convert x to string, if you want to assign it to a Caption property (use IntToStr for this). With that said, here's one way to do it (assuming that the buttons are already created by your form designer)
Code:
var
  button: array[1..5] of TButton;
  x : integer;
begin
   button[1] := Button1;
   button[2] := Button2;
   button[3] := Button3;
   button[4] := Button4;
   button[5] := Button5;

   for  x := 1 to 5 do
     button[x].Caption := IntToStr(x);
end;


If you want to create buttons on the fly, here's how to do it.
Code:
var
   button : array[1..5] of TButton;
   x : integer;
begin
   for  x := 1 to 5 do
   begin
     button[x] := TButton.Create(self);
     button[x].Parent := self;
     button[x].Left := (x - 1) * 100 + 10;
     button[x].Top := 20;
     button[x].Caption := IntToStr(x);
   end;
end;
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Reply With Quote
  #3  
Old December 5th, 2003, 07:42 AM
pahunt pahunt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 3 pahunt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Or you could do something like the following

Code:
var
  x: integer;
begin
  for x := 1 to 5 do
    TButton(Self.FindComponent('Button' + IntToStr(x))).Caption := IntToStr(x);
end;


Obviously you would have to be sure that buttons were actually named Button1, Button2 etc.

Last edited by pahunt : December 5th, 2003 at 07:45 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Button Caption


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway