Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Child Form Focus Control

Posted on 2006-10-25
10
Medium Priority
?
1,256 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
9 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
Industry Leaders: 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 400 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 400 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

877 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