Solved

How to get a form handle/variable

Posted on 1998-12-31
5
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 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