Solved

How to get a form handle/variable

Posted on 1998-12-31
5
143 Views
Last Modified: 2010-04-04
Hi,

I have an MDI application.  I guarantee that each MDIChild window can only have one copy open at a time.  Within the code of the window, I need to do things or check things related to that window.  For example, I need to check if the window is visible.

I would normally do this with...
 if MyForm.Visible = true ...

Then question is:  Is there an easy way to get 'MyForm'.
MyForm is a variable of type TMyForm.  I know that when I look at the source code for the window, I have a variable declaration of
  MyForm : TMyForm;

but this is not set to anything.  I need to set it to a valid value.  I can do this with this code...

// Create a handle to the Current Window
  ThisWindow := nil;
  with Application.MainForm do
    for N := 0 to MDIChildCount - 1 do
      if MDIChildren[N] is TMyForm then
        ThisWindow := MDIChildren[N] as TMyFormF;

Isn't there an easier way to do this?

Please respond to gman@mics.net
0
Comment
Question by:gspears060598
5 Comments
 
LVL 5

Expert Comment

by:heathprovost
ID: 1353984
var
  N: Integer;
begin
  with Application.MainForm do
    for N := 0 to MDIChildCount - 1 do
    begin
      if not (MDIChildren[I] is TMyForm) then continue;
      with TMyForm(MDIChildren[I]) do
      begin
        //do whatever
      end;
    end;
end;

This isnt much better, but it will run a little faster due to the removal of the second type check (the as).  TypeCasting this way is much faster and you are already typechecking with the is statement.
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1353985
Code correction -

var
  N: Integer;
begin
  with Application.MainForm do
    for N := 0 to MDIChildCount - 1 do
    begin
      if not (MDIChildren[N] is TMyForm) then continue;
      with TMyForm(MDIChildren[N]) do
      begin
        //do whatever
      end;
    end;
end;
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1353986
This would be even faster...

.....
ThisWindow := TMyFormF(MDIChildren[N]);
....

so that's not a solution also....

I don;t understand are you trying to see if a window is visible or if it is of the same type?!?!

-Viktor
-Ivanov
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
ID: 1353987
I am not altogether sure if you have a problem here:

If you just want to do things generically with the current form then you can get it like this:

MyForm := Screen.ActiveForm; // Get the active window (the one with focus)

If you need to do things specific to an individual MDI child (for which you have said there is only ever one of any particular kind of MDI form) then just declare variables for them:

var
  DoThisForm : TDoThisForm;

and when you create the MDI form assign it to it.

Then if you need to do something with the TDoThisForm you do it like this

if assigned(DoThisForm) then
  // do whatever here

Cheers,

Raymond.




0
 
LVL 1

Expert Comment

by:adeng
ID: 1353988
Have you try this :

1. After you make a new form in delphi IDE, goto menu : Project | Option : and remove MyForm from 'Auto-Create forms' List Box.

2. write this code :

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     Action := caFree;
end;

procedure TMyForm.FormDestroy(Sender: TObject);
begin
     MyForm := nil;
end;

3. Use this code to make MyForm visible and prevent multiple copy :

procedure TForm1.Button1Click(Sender: TObject);
begin
       if not assigned(MyForm) then      
            MyForm := TMyForm.Create(Application);
       MyForm.Show;
end;

Regards Adeng;
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

19 Experts available now in Live!

Get 1:1 Help Now