• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Controls in form - how to know class and use properties?

Hi.

I have form with X controls (visivle and non-visible components).

I want to scan form to know number of controls and assign local variable 'Botao' (TButton) to this control, using there properties and function.

var
Control: TButton;
i: integer;
begin
  for i:=0 to MyForm.ControlCount-1 do
    begin
    if MyForm.Controls[i].ClassName = 'TLabel' then
      begin
 
      // How can i assign local 'Control' to this control on "MyForm" and use function and properties, like "Control.caption := ..."
 
      end;
    end;
   settings.Save;
   
0
Jmaurin
Asked:
Jmaurin
  • 3
  • 2
1 Solution
 
arnismitCommented:
is this what you want to do ?

create a new project
drop a button and two labels on the form

and use the code shown below:

good luck,
arni

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    procedure MyButtonClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyButtonClick(Sender: TObject);
begin
  ShowMessage('You clicked me!') ;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
 Index:Integer;
 TempObject: TControl;
 MyButtonVariable: TButton;
begin
  For Index := Controlcount - 1 downTo 0 Do
  Begin
    If Controls[Index].ClassType = TLabel Then
    Begin
      TempObject := Controls[Index];
      Try
        MyButtonVariable := TButton.Create(Self);
        With MyButtonVariable Do
        Begin
          Parent := Self;
          Caption := 'Some Caption';
          OnClick := MyButtonClick;
          Left := TempObject.Left;
          Top := TempObject.Top;
          Height := 19;
          Width := 60;
        End;
      Finally
        TempObject.Free;
      End;
    End;
  end;

end;

end.
0
 
TheRealLokiSenior DeveloperCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
    procedure MyButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
    var
        Control: TButton;
        i: integer;
    begin
        for i := 0 to pred(Form1.ControlCount) do
        begin
            if Form1.Controls[i] is TLabel then
              (Form1.Controls[i] AS TLabel).Caption := 'press the button to beep'
            else if Form1.Controls[i] is TButton then
              (Form1.Controls[i] AS TButton).OnClick := Form1.MyButtonClick;
        end;
    end;

procedure TForm1.MyButtonClick(Sender: TObject);
    begin
        showmessage('beep');
    end;

end.
0
 
TheRealLokiSenior DeveloperCommented:
if you really want to assign a local variable you can do it like this
var
    control: TButton;
if Form1.Controls[i] is TButton then
begin
    Control := (Form1.Controls[i] as TButton);
    Control.Caption := 'press button to beep';
end;

0
 
arnismitCommented:
but you cannot change  a label to a button or vica versa
0
 
TheRealLokiSenior DeveloperCommented:
shouldn't need to. if you need to use a variable you'd declare it as tcontrol and typecast it, but in fact there's really no need to even do that, as it's already in the controls[] list. I didn't actually use the control: tbutton in my first example, and inthe second i just showed how it could be done
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now