Mega Code Archive

 
Categories / Delphi / Forms
 

Get access to Controls on other forms

Title: Get access to Controls on other forms Question: How can I get access to controls on other forms? Answer: You can not have code like this working for you: form2.ListBox.Items.Add( 'Hello' ); It won't work. I thought and could not find a solution till I got an idea: why not define a variable and work with that. IT WORKS! Here is the project. You have to create two forms (form1 (unit1.pas) and form2 (unit2.pas). But all of this, see the dfm-file at the end of this artxcle... Here is unit1 (form1): unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, Buttons, ExtCtrls; type TForm1 = class(TForm) BitBtn_AddItemsOnForm2ListBox: TBitBtn; EditAddItemsOnForm2ListBox: TEdit; StatusBar1: TStatusBar; BitBtn_ShowForm2Modal: TBitBtn; Panel1: TPanel; procedure BitBtn_AddItemsOnForm2ListBoxClick(Sender: TObject); procedure BitBtn_ShowForm2ModalClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation uses unit2; //unit2 for Form2 var SecondForm: tForm2; {$R *.DFM} procedure TForm1.BitBtn_AddItemsOnForm2ListBoxClick(Sender: TObject); begin SecondForm.ListBox1.Items.add(EditAddItemsOnForm2ListBox.Text); end; procedure TForm1.BitBtn_ShowForm2ModalClick(Sender: TObject); begin SecondForm.ShowModal; end; //======================================================================== procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin SecondForm.release; end; procedure TForm1.FormCreate(Sender: TObject); begin SecondForm := tform2.create(nil); end; end. .......................................................................... ........and here you've got unit2.pas (form2): unit Unit2; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons; type TForm2 = class(TForm) ListBox1: TListBox; BitBtn_Exit: TBitBtn; StatusBar1: TStatusBar; procedure BitBtn_ExitClick(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form2: TForm2; implementation {$R *.DFM} procedure TForm2.BitBtn_ExitClick(Sender: TObject); begin close; end; end. .......................................................................... unit1.dfm is this: object Form1: TForm1 Left = 267 Top = 337 Width = 261 Height = 167 Caption = 'Delphi-programming -- HOW TO' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnClose = FormClose OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object BitBtn_AddItemsOnForm2ListBox: TBitBtn Left = 8 Top = 8 Width = 193 Height = 25 Caption = '&Add on ListBox1 on Form2' TabOrder = 0 OnClick = BitBtn_AddItemsOnForm2ListBoxClick end object EditAddItemsOnForm2ListBox: TEdit Left = 40 Top = 40 Width = 121 Height = 21 TabOrder = 1 Text = 'Item1' end object StatusBar1: TStatusBar Left = 0 Top = 121 Width = 253 Height = 19 Panels = item Text = 'Access Controls on other forms' Width = 155 end item Text = '(ent.w.Delphi5)' Width = 50 end SimplePanel = False end object BitBtn_ShowForm2Modal: TBitBtn Left = 8 Top = 72 Width = 97 Height = 25 Caption = '&Show Form2' TabOrder = 3 OnClick = BitBtn_ShowForm2ModalClick end object Panel1: TPanel Left = 0 Top = 104 Width = 253 Height = 17 Align = alBottom Caption = 'Dir:\AccessControlsOtherForms' TabOrder = 4 end end ........................................................................... you need unit2.dfm, too: object Form2: TForm2 Left = 478 Top = 520 Width = 250 Height = 167 Caption = 'Form2' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 0 Top = 0 Width = 241 Height = 97 ItemHeight = 13 TabOrder = 0 end object BitBtn_Exit: TBitBtn Left = 0 Top = 96 Width = 113 Height = 25 Caption = '&Exit' TabOrder = 1 OnClick = BitBtn_ExitClick end object StatusBar1: TStatusBar Left = 0 Top = 121 Width = 242 Height = 19 Panels = SimplePanel = False end end ......................................................................... Code entered with Delphi 5 by Omer Yasar Can (living in Holland) ALI BABA Computin' omercan@home.nl