Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Child Form Focus Control

Posted on 2006-10-25
10
Medium Priority
?
1,253 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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 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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

722 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