Solved

Child Form Focus Control

Posted on 2006-10-25
10
1,245 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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