Solved

Child Form Focus Control

Posted on 2006-10-25
10
1,211 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
10 Comments
 
LVL 28

Expert Comment

by:TName
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 28

Expert Comment

by:TName
Comment Utility
>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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:int2strcpy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Forced accept.

Computer101
EE Admin
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

15 Experts available now in Live!

Get 1:1 Help Now