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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking an array of strings to a control 2

Status
Not open for further replies.

jimcart

Programmer
Jul 26, 2003
3
AU
I am creating controls on the fly from button presses and
then need to have additional information stored with each control.

example:
paintbox := TPaintBox.Create(Self);
paintbox.parent := somepanel;

where can I store these
paintboxshape := rectangle
otherHolder := othervalue
etc... etc...
without creating a new component.


What I need is to store the shape that I want drawn
on the paintbox + other values in variables associated with the paintbox

I am currently using a TList array for the controls
and a duplicate array of strings for the types etc...

I would prefer a more efficient method without adding extra properties to the control.

Thanks in advance,

Regards,
Jimcart.








 
The very thing you say you don't want to do is probably the most efficient and easiest way to do it.
[blue]
Code:
type
  TMyPaintBox = class(TPaintBox)
  private
    FPaintBoxShape:TRect;
    FOtherValue:string;
  public
  published
    property PaintBoxShape:TRect read FPaintBoxShape write FPaintBoxShape;
    property OtherValue:string   read FOtherValue    write FOtherValue;
  end;
:
:
implementation
:
:
procedure TForm1.Button1Click(Sender: TObject);
var
  APaintBox:TMyPaintBox;
begin
  APaintBox := TMyPaintBox.Create(Self);
  APaintBox.OtherValue := 'Hello World';
  ShowMessage( APaintBox.OtherValue );
  APaintBox.Free;
end;
[/color]


But if you really don't want to do that, a couple of things come to mind. However, both still start with creating an object to hold the things you want to keep track of. Then you could either store the pointer to that object in the Tag property of the paintbox (with a cast to Integer) or maintain two TObjectLists in parallel such that element
Code:
[i]
in one list is the paintbox and element
Code:
[i]
in the other list is the companion object. Very messy, error prone, and doesn't buy you much.

What's your problem with simply sub-classing the TPaintBox object?
 
Zathras,

Thanks for your answer.

The problem with simple subclassing
is that I want to be able to add additional properties
on the fly at runtime.

My next try was to use the names of the controls as index
and set various information for each in an ansistring
with "getProperties" - "setProperties" - "DeleteProperties"
This has shown to be way to Slo
I am now looking at using an in memory table to hopefully achieve the results I require.

**** Still looking for ideas though ****.

Regards,
JimCart.
 
I can't quite picture what you are doing, but you can include a string list as one of the properties. That should make it open-ended enough. Use the Strings property to identify your "property" and use the Objects property to hold the value if it is a simple integer or a pointer to another object, (perhaps a simple wrapper for a string).

 
I'd follow what Zathras was suggesting, except I'd use 2 string lists. 1 to hold the button name and a pointer to another dynamically created string list which holds the name=value pairs for you properties(TStringList.Names, TStringList.Value).

You could always:

TControlProperties = class(TObject)
private
FControl: TWinControl;
FProperties: TStringList;
public
...
end;

TControlList = class(TList)
protected
function Get(Index: integer): TControlProperties;
function Put(Index: integer; Value: TControlProperties);
public
function PropertyByControl(Control: TWinControl): TControlProperties;
function PropertyByName(ControlName: string): TControlProperties;

property Items[Index: integer]: TControlProperties read Get write Put;
end;

That sort of thing....
 
Thank you both for your answers

I will try implementing your ideas.

Regards,
Jimcart.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top