Board index » delphi » Creating Edit's, Memo's and ComboBox'es at runtime

Creating Edit's, Memo's and ComboBox'es at runtime

Im writing a program which must create a number of components, depending on
the information contained in an ini file. The possible components are TEdit,
TMemo and TComboBox. After I create them I need to use their properties and
arrange them (for example their text properties) in the exact order of the
components' creation (in a single string). I know I have to create a dynamic
array of TWinControls, for example and then create and put the components on
the form programatically, but then I cant use their text properties... How
do i do that?

Thanx in advance,
Tzvetelin Gospodinov

</end>

 

Re:Creating Edit's, Memo's and ComboBox'es at runtime


Quote
In article <3b1faba1_1@dnews>, Tzvetelin Gospodinov wrote:
> Im writing a program which must create a number of components, depending on
> the information contained in an ini file. The possible components are TEdit,
> TMemo and TComboBox. After I create them I need to use their properties and
> arrange them (for example their text properties) in the exact order of the
> components' creation (in a single string). I know I have to create a dynamic
> array of TWinControls, for example and then create and put the components on
> the form programatically, but then I cant use their text properties... How
> do i do that?

You can use the Text property, either by typecasting the TwinControls back to
their true classes, or by using run-time type information. The unit below may
be of use here.

{== Unit UIHelpers ====================================================}
{: Collects some helper routines for manipulating controls.
@author Dr. Peter Below
@desc   Version 1.0 created 13 Dezember 2000<BR>
        Current revision 1.0<BR>
        Last modified       13 Dezember 2000<P>                        }
{======================================================================}
Unit UIHelpers;
Interface

Uses Controls, Graphics;

Procedure SetControlColor( aControl: TControl; aColor: TColor );
Function GetControlColor( aControl: TControl ): TColor;
Procedure SetControlFontColor( aControl: TControl; aColor: TColor );
Function GetControlFontColor( aControl: TControl ): TColor;
Procedure SetControlText( aControl: TControl; Const aText: String );
Function GetControlText( aControl: TControl ): String;

Implementation

Uses
  TypInfo, Sysutils;

{-- SetControlColor ---------------------------------------------------}
{: Changes a controls Color property, if it has one.
@Param aControl is the control to modify
@Param aColor is the new color to use
@Precondition  aControl <> nil

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Procedure SetControlColor( aControl: TControl; aColor: TColor );
  Begin { SetControlColor }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Color' ) Then
      SetOrdProp( aControl, 'Color', aColor );
  End; { SetControlColor }

{-- SetControlFontColor -----------------------------------------------}
{: Set a controls font color, if it has a font property.
@Param aControl is the control to modify
@Param aColor is the new color to use
@Precondition  aControl <> nil

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Procedure SetControlFontColor( aControl: TControl; aColor: TColor );
  Begin { SetControlFontColor }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Font' ) Then
      TFont( GetObjectProp( aControl, 'Font', TFont )).color := aColor;
  End; { SetControlFontColor }

{-- GetControlColor ---------------------------------------------------}
{: Get a controls Color property, if it has one.
@Param aControl is the control to look at
@Returns the controls color, or clWindow if it does not have a Color
  property.
@Precondition  aControl <> nil

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Function GetControlColor( aControl: TControl ): TColor;
  Begin { GetControlColor }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Color' ) Then
      Result := TColor( GetOrdProp( aControl, 'Color' ))
    Else
      Result := clWindow;
   End; { GetControlColor }

{-- GetControlFontColor -----------------------------------------------}
{: Get a controls Font.Color property, if it has one.
@Param aControl is the control to look at
@Returns the controls font color, or clWindowText if it does not have a
  Font property.
@Precondition  aControl <> nil

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Function GetControlFontColor( aControl: TControl ): TColor;
  Begin { GetControlFontColor }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Font' ) Then
      Result := TFont( GetObjectProp( aControl, 'Font', TFont )).color
    Else
      Result := clWindowText;
  End; { GetControlFontColor }

{-- SetControlText ----------------------------------------------------}
{: Set a control Text propery, if it has one. If it does not, try the
   Caption property.
@Param aControl is the control to work on.
@Param aText is the text to copy.

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Procedure SetControlText( aControl: TControl; Const aText: String );
  Begin { SetControlText }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Text' ) Then
      SetStrProp( aControl, 'Text', aText )
    Else If IsPublishedProp( aControl, 'Caption' ) Then
      SetStrProp( aControl, 'Caption', aText );
  End; { SetControlText }

{-- GetControlText ----------------------------------------------------}
{: Set a control Text propery, if it has one. If it does not, try the
   Caption property.
@Param aControl is the control to work on.
@Returns the properties content, or an empty string, if the control has
  neither Text nor Caption property.

Quote
}{ Created 13.12.2000 by P. Below

-----------------------------------------------------------------------}
Function GetControlText( aControl: TControl ): String;
  Begin { GetControlText }
    Assert( Assigned( aControl ));
    If IsPublishedProp( aControl, 'Text' ) Then
      Result := GetStrProp( aControl, 'Text' )
    Else If IsPublishedProp( aControl, 'Caption' ) Then
      Result := GetStrProp( aControl, 'Caption' )
    Else
      Result := EmptyStr;
  End; { GetControlText }

End { UIHelpers }.
Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads