Solved

Referencing objects on an ActiveForm from another unit

Posted on 2001-08-27
6
371 Views
Last Modified: 2010-04-06
Easy to do with a normal TForm and unit - ie in the second unit have a "uses uMain" where uMain defines for example "var MyMainForm:TMyMainForm" and within the second unit we can have MyMainForm.memo1.lines.text:='xx'

I'm trying to do this with an ActiveX Form:
I have a TMyAForm=class(TActiveForm, IMyAForm) and a datamodule unit - everythin has been working a treat so far, opening database connection etc - but now I require one of the query components in the datamodule to alter a property of an object on the main form.

Any help would be greatly appreciated.
0
Comment
Question by:djmcrae
6 Comments
 

Author Comment

by:djmcrae
Comment Utility
The ActiveXForm is being compiled as an OCX! (forgot to mention)
0
 

Expert Comment

by:alshaikh
Comment Utility
From my own experiments, I don't think it is possible to use more than one form in an ActiveFrom project. In our own projects, we try to put all components and functionality we need in just one ActiveForm and use the Visible property to show/hide these components at run time.

I noticed however, that it is possible to have more than one form, provided that the second form is created at run time dynamically. We have not explored this in details yet.

Regards,
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
Comment Utility
I don't think it's possible. Besides, I think it's bad programming to link units that way.

Pass the Query object (or the specific property) to the function in the 2nd unit, so that function can change the property that belongs to the query object on the main form.

example:

unit Unit2;

...

procedure ChangeQuery(sql: TStrings);
begin
  sql.Text := 'select * from table1';
end;


Call this function from the main unit like this:

uses Unit2;

ChangeQuery(Query1.SQL);
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:djmcrae
Comment Utility
 It may not be possible - I will try for just a bit longer though (~1 week). I have a huge application - the main form is 7,500 lines of code (it also has 8 other units, although these are much smaller). I want to shift the dozen odd database components to a datamodule (this worked OK) and move a fair bit of database code from the main to this. This has been proceeding along well, until one of the TADOQuery's onOpen code was moved over.
  This onOpen refreshed the strings in a TComboBox on the main form - grief - cannot get to the objects on the main form. I could just have the onOpen populate a TStringList which the opening code in main could the reference after issueing the opening command - yuk!!
  Another work around may be to make the whole main ActiveForm a normal TForm, then have the activeForm create an instance of this (outlined at http://pw2.netcom.com/~cherrman/cvtforms.htm) - better choice than my first, and much better than...
  Have a global var in the main pointing to the object on the form, ie var objPersonnel:TDBComboBox; then in the ActiveForm's onCreate, assign objPersonnel:=cbPersonnel ir the combobox on the form, the dataModule then uses objPersonnel. I just tried this and, dammit, it works. I do not like it one bit!! Global variables for reference by other units, it cannot be good!!
  Hitting the books at the moment on the COM world - we may be able to use an interface - my knowledge here could do with some honing!!
  BTW, alshaikh, creating a second form is not a problem - this application has about 8 including an about form, & not including the many messageboxes - it's handled no differently from normal.
0
 

Author Comment

by:djmcrae
Comment Utility
 It may not be possible - I will try for just a bit longer though (~1 week). I have a huge application - the main form is 7,500 lines of code (it also has 8 other units, although these are much smaller). I want to shift the dozen odd database components to a datamodule (this worked OK) and move a fair bit of database code from the main to this. This has been proceeding along well, until one of the TADOQuery's onOpen code was moved over.
  This onOpen refreshed the strings in a TComboBox on the main form - grief - cannot get to the objects on the main form. I could just have the onOpen populate a TStringList which the opening code in main could the reference after issueing the opening command - yuk!!
  Another work around may be to make the whole main ActiveForm a normal TForm, then have the activeForm create an instance of this (outlined at http://pw2.netcom.com/~cherrman/cvtforms.htm) - better choice than my first, and much better than...
  Have a global var in the main pointing to the object on the form, ie var objPersonnel:TDBComboBox; then in the ActiveForm's onCreate, assign objPersonnel:=cbPersonnel ir the combobox on the form, the dataModule then uses objPersonnel. I just tried this and, dammit, it works. I do not like it one bit!! Global variables for reference by other units, it cannot be good!!
  Hitting the books at the moment on the COM world - we may be able to use an interface - my knowledge here could do with some honing!!
  BTW, alshaikh, creating a second form is not a problem - this application has about 8 including an about form, & not including the many messageboxes - it's handled no differently from normal.
0
 
LVL 17

Expert Comment

by:geobul
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept Epsylon's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now