Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a component and setting its font properties

Status
Not open for further replies.

rogerte

Programmer
Nov 9, 2001
164
0
0
GB
Hi,

Simple button click handler that applies to several buttons on a page.

The code determines which (speed)button is pressed (all have a numeric suffix (e.g. MainButton1, MainButton2 etc) and then displays a corresponding Tabsheet. That bit works ok, but I am also trying to set the font properties of the buttons, such that the pressed button's font is coloured and bold, and the other buttons are set to default (black and non-bold).

Code:
procedure TForm1.MainButtonClick(Sender: TObject);
var
i, j, k : integer;
sName, sTab, sBtn, sNo : string;   //for testing
sl : Tstringlist ;
begin
sl := Tstringlist.Create;
for i := 0 to ComponentCount-1 do
  begin
  if Components[i] is TSpeedButton then
    sl.add((Components[i] as TSpeedButton).Name);
  end;
sName := TComponent(Sender).Name;
sNo := copy(sName, length(sName),1);
i := StrToInt(sNo);
for j := 0 to sl.count-1 do
  begin
  k := j+1;
  if i = strToInt(copy(sl[j], length(sl[j]),1)) then
    begin
    sTab := 'TabSheet'+ IntToStr(i);
    PageControl1.ActivePage := (FindComponent(sTab) As TTabSheet);
    end;
  with TSpeedButton(sl[j]) do
    begin
    if i = k then Font.Color := clBlue else Font.Color := clBlack;
    if i = k then Font.style := [fsBold] else Font.style:=[];
    end;
  end;
sl.Destroy;
end;

It all works until it gets to the first "if i = k ..." line and then it throws an exception error.

I know it is probably something simple, but have not used Delphi in about 5 years.

Thanks for any suggestions

Roger
 
Sorted it - so everyone can stand down!

used:
with FindComponent(sl[j])As TSpeedButton do

instead of
with TSpeedButton(sl[j]) do

Cheers

Roger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top