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
Solved

How to get a form handle/variable

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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