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!

String as component name? 2

Status
Not open for further replies.

KempCGDR

Programmer
Jan 10, 2003
445
0
0
GB
Hi, I have an application where I have a Tabbed Notebook component and each tab contains an identical set of components. These are 10 rows of 1 Combobox and 3 Editboxes. I need to change which Editboxes are enabled based on the selection in that row's Combobox. So far, I have the following shared event handler that all of the comboboxes use:

Code:
var
   no:string;
begin
   no := Copy((Sender As TComboBox).Text,8,2);
   If Copy((Sender As TComboBox).Text,1,6) = 'String' then
   begin
      edtMin11.Enabled := False;
      edtMax11.Enabled := True;
   end;
end;

The value of 'no' will be of the form xy, where x is the tab number and y is the row number. Basically, the lines with edtMin11 and edtMax11 need to actually be changing editMinxy and editMaxxy. I've tried

((edtMin+no) As TEdit).Enabled

but this doesn't work, the only other way I have in my box of tricks is to check the name of every component on the form and modify the right one when I find it, but that seems too much of a brute force approach for me to use it if there's another option.

So, anyone know how to do this?
 
Included classes in uses.

var
LEdit:TCustomEdit;
begin
LEdit := FindComponent(edtMin + no);
LEdit.Enabled;
end;

Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group." - tag line I stole
 
"Incompatible types TCustomEdit and TComponent" apparently. It's meant to be:

LEdit := FindComponent('edtMin' + no);
LEdit.Enabled := False;

I assumed, as the parameter needs to be a string.

I tried casting it thus:

LEdit := (FindComponent('edtMin' + no) As TEdit);
LEdit.Enabled := False;

but this gives me an access violation. Any more ideas?
 
var
LEdit:TComponent;
begin
LEdit := FindComponent(edtMin + no);
if (LEdit <> nil) and (LEdit is TEdit) then
(LEdit as TEdit).Enabled := False;
end;

I assume that your combo-boxes are named in the same fashion? In your example you are doing:

no := Copy((Sender As TComboBox).Text,8,2);
If Copy((Sender As TComboBox).Text,1,6) = 'String' then

Surely you are not tacking the xy value on the end of hte comboboxe's text? but rather to the end of hte comboboxes name????

I would name the combos like this: cbx<tab>X<row>
e.g. cbx0X0, cbx0X1, cbx0X2, cbx1X0....
Otherwise you could run into problems...e.g.
Tab Row Name
0 0 cbx00
1 10 cbx110
11 0 cbx110 !!!!!!- same name as 1,10!


then:

var
no:string;
comboName: string;
i: integer;
EditBox: TComponent;
begin
comboName := (Sender As TComboBox).Name;
Delete(comboName,1,3);
//comboName is now <tab>X<row>
If Copy((Sender As TComboBox).Text,1,6) = 'String' then
begin
EditBox := FindComponent('edtMin' + comboName);
if (EditBox <> nil) and (EditBox is TEdit) then
(EditBox as TEdit).Enabled := False;
EditBox := FindComponent('edtMax' + comboName);
if (EditBox <> nil) and (EditBox is TEdit) then
(EditBox as TEdit).Enabled := True;
end;
end;
 
LEdit := FindComponent(('edtMin' + no) as TCustomEdit;
LEdit.Enabled := false;

I think this is correct. Sorry about not checking my first post.

If you used
var
LEdit : TCustomEdit;

then maybe you need to cast back to TCustomEdit.

I have this code in my project for locating the correct grid on a series of tabsheets:

LGrid := FindComponent('wwDBGrid_Activity_User' + IntToStr(PageControl_ActivityLineItems.ActivePageIndex + 1)) as TwwDBGrid;

And this to locate a label:
LLabel := (FindComponent('cfLabel_TOV_' + Char(LINT_i)) as TcfLabel);

Are you sure the access violation is tied to the FindComponent call?

FindComponent typically means self.FindComponent (or you can call it for a particular component &quot;Panel1.FindComponent&quot;), so if you are not calling this from the relevant form itself, there may be a problem.


Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
BillDoorNZ:
&quot;Surely you are not tacking the xy value on the end of hte comboboxe's text? but rather to the end of hte comboboxes name????&quot;

Damn, you know my program better than I do. No idea what I was thinking when I wrote that, put it down to it being at 1am.

It now works perfectly as:

no := Copy((Sender As TComboBox).Name,8,2);
If Copy((Sender As TComboBox).Text,1,6) = 'String' then
begin
LEdit := (FindComponent('edtMin' + no) As TEdit);
LEdit.Enabled := False;
LEdit := (FindComponent('edtMax' + no) As TEdit);
LEdit.Enabled := True;
end;

Thanks a lot guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top