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!

Problems with using components in 2 forms

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
I have just started writing my first application in Delphi and I am now stuck communicating between forms. I am using a serial port component TnrComms which has a main serial port component and a Device dropdown box component. I wish to select the dropdown box on a different form in the same project, but I don't know how to reference the main serial port component so that I can select that component in the object inspector. Have a look and point me in the right direction.

form 1


code:--------------------------------------------------------------------------------
unit datacollex;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ExtCtrls, ToolWin, ActnMan, ActnCtrls, StdCtrls, ComCtrls,
nrclasses, nrcomm, dataport;

type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
A1: TMenuItem;
Options1: TMenuItem;
Options2: TMenuItem;
Disconnect1: TMenuItem;
Help1: TMenuItem;
SerialPortSettings1: TMenuItem;
Quit1: TMenuItem;
Panel2: TPanel;
ActionToolBar1: TActionToolBar;
IncomingDataEdit: TRichEdit;
Panel1: TPanel;
OutgoingSQLEdit: TRichEdit;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
nrComm1: TnrComm; // this is the component
SelectPort1: TMenuItem;
procedure SelectPort1Click(Sender: TObject);
procedure SerialPortSettings1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.SelectPort1Click(Sender: TObject);
var dlg: TPortForm;
begin
dlg := TPortForm.Create( Self);
dlg.ShowModal;
end;

procedure TMainForm.SerialPortSettings1Click(Sender: TObject);
begin
nrComm1.ConfigDialog;
end;

end.
--------------------------------------------------------------------------------


Form 2

code:--------------------------------------------------------------------------------
unit dataport;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ComCtrls, nrcomm, nrcommbox;

type
TPortForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Quit1: TMenuItem;
PortOk: TButton;
PortCancel: TButton;
nrDeviceBox1: TnrDeviceBox; //this is the dropdown box
private
{ Private declarations }
public
{ Public declarations }
end;

var
PortForm: TPortForm;

implementation

{$R *.dfm}

end.
--------------------------------------------------------------------------------

Thanx
 
Add
Code:
 datacollex
to the [blue]
Code:
 uses
[/color]
list and reference the component by qualifying with the form name (as named in the [blue]
Code:
 var
[/color]
section):
Code:
   MainForm.nrComm1
 
Thanks Zathras for your help. Had to sort out the circular reference, but ended up with the following.

Code:-----------------------------------------------------

unit dataport;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, datacollex, nrcomm, nrcommbox;

type
TPortForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Quit1: TMenuItem;
PortOk: TButton;
PortCancel: TButton;
nrDeviceBox1: TnrDeviceBox;
procedure PortOkClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
PortForm: TPortForm;

implementation

{$R *.dfm}

procedure TPortForm.PortOkClick(Sender: TObject);
begin
MainForm.nrComm1.Active:=true;
PortForm.Close;
end;

end.
-----------------------------------------------------------
Can you poin me in the right direction for closing this form for PortOkClick?
 
Easiest way when using .ShowModal is to set the ModalResult property of the button to mrOk and eliminate the line where you have
Code:
 PortForm.Close

You could also use a BitBtn and set the Kind property to bkOK. Delphi automatically then sets the ModalResult property to mrOk.

Also, to get around circular references (assuming you meant in the
Code:
 uses
clause), you can put a secondary
Code:
 uses
line in the implementation section. -- see the help file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top