Solved

How to get a form handle/variable

Posted on 1998-12-31
5
152 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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