Solved

Child Form Focus Control

Posted on 2006-10-25
10
1,239 Views
Last Modified: 2012-05-05
I need to be able to keep a child form from stealing focus from all my other forms. I've tried to set the enabled property to false and it still doesn't help. I'm updating a caption on this form and every time it's updated/shown it steals focus from all my other forms in my project. I'm using the Visual Component Library for my forms. Any ideas to stop this from happening?

int2strcpy
0
Comment
Question by:int2strcpy
[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
10 Comments
 
LVL 28

Expert Comment

by:TName
ID: 17807952
Hi, I'm not sure if I understand the situation, as setting a caption shouldn't normally pass focus  to another child, but you could try something like this:

procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
fn:String;
begin  
    for i:=0 to MDIChildCount -1 do begin
      if MDIChildren[i].Active then
        fn:=MDIChildren[i].Name;
    end;

  //Do stuff that sets focus on another window

  TForm(Application.FindComponent(fn)).BringToFront;
  TForm(Application.FindComponent(fn)).SetFocus;
end;
0
 

Author Comment

by:int2strcpy
ID: 17807988
My apologies I forget to mention that the child form which is stealing window focus is hidden and this happens when it is shown.

// in hidden state
SomeChild.SomeLabel.Caption := 'Test';
SomeChild.Show;

I execute the above code a lot in the course of an hour and after (x) amount of seconds the form is rehidden again until the next show. Rather than attempt to pass focus onto another child form is there anyway to stop it from ever gaining focus in the first place? This is what I need to do. So maybe some form OnShow hack to stop this?

int2strcpy


0
 
LVL 28

Expert Comment

by:TName
ID: 17808111
How do you hide/show the child window?  Normally, if you do this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowWindow(Form2.handle, SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 ShowWindow(Form2.handle, SW_SHOW);
end;

the child Form2 will only get the focus if it had it before it was hidden. So just pass the focus to another child before hiding Form2:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form3.Setfocus;
  ShowWindow(Form2.handle, SW_HIDE);
end;
0
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!

 
LVL 28

Expert Comment

by:TName
ID: 17808175
>Rather than attempt to pass focus onto another child form is there anyway to stop it from ever gaining focus in the first place? This is what I need to do. So maybe some form OnShow hack to stop this?

Sorry, had missed this bit...
I find it interesting that SomeChild.Show; and SomeChild.Hide; work for you... You ARE talking about MDI children, arent't you?
With ShowWindow(SomeChild.handle, SW_SHOW),   OnShow will probably not have any effect.

Sorry, it's very late here now ;)
0
 

Author Comment

by:int2strcpy
ID: 17808238
No it's not an MDI child. I created a few forms and i used the "add to project" in Delphi's IDE. In my main form I added their .pas name to the uses clause so I can access it like form2.Show; etc.

int2strcpy
0
 
LVL 3

Accepted Solution

by:
cobi100 earned 100 total points
ID: 17808943
maybe something ike this:

// in hidden state
SomeChild.SomeLabel.Caption := 'Test';
SomeChild.Show;
Self.SetFocus;
0
 
LVL 28

Assisted Solution

by:TName
TName earned 100 total points
ID: 17809116
Well, some form will have to have the focus ;)

So either use cobi's code (called in a main form procedure, it will  bring the main form to front), or do what I first said (changed to work with normal forms).
You know best what you need, should the main form always get focus, then you don't need this:


procedure TForm1.Timer1Timer(Sender: TObject);
var
i:Integer;
fn:String;
begin
   if  Form2.Showing then  
      Form2.Hide
   else begin
      for i:=0 to Application.ComponentCount -1 do begin
        if Application.Components[i] is TForm then
          if TForm(Application.Components[i]).Active  then
             fn:=Application.Components[i].Name;
      end;
      Form2.Caption := 'Test';
      Form2.Show;
      TForm(Application.FindComponent(fn)).BringToFront;
      TForm(Application.FindComponent(fn)).SetFocus;
  end;
end;
0
 
LVL 4

Assisted Solution

by:Meldrachaun
Meldrachaun earned 100 total points
ID: 17961643
try this:

var
  control: TWinControl;
begin
  control := Screen.ActiveControl;
  form2.Label1.caption := 'testing';
  Form2.Show;
  if assigned(control) then
    control.SetFocus;
end;
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21133084
Forced accept.

Computer101
EE Admin
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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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