Solved

PROBLEM: Lost a focus

Posted on 2004-09-30
13
420 Views
Last Modified: 2010-04-04
Hello All,

I have small problem...

I have a form with two elements, TWebBrowser and TEdit. When I show some information in TWebBrowser TEdit losts focus in any ways... I did so to fix this problem:
---
    if(edChatMess.CanFocus)then begin
      Self.ActiveControl:=edChatMess;
      edChatMess.SetFocus;
    end;
---

And sometimes I get same trouble...

Can somebody suggest me way to fix this trouble: I need that TEdit always has focus in any cases!

Thanks,
Dan
0
Comment
Question by:DanDaemon
[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
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12191941
Set by ObjectInspector:

  edChatMess.TabStop := True;

for all other controls:

  ContolName.TabStop := False;

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12191972
procedure TForm1.WebBrowserEnter(Sender: TObject);
begin
  edChatMess.SetFocus;
end;
0
 

Author Comment

by:DanDaemon
ID: 12191988
Hello All,

Please give me a constructive solutions.

2esoftbg, sorry but you gave me 'low skills' solutions.
Which will not solve my problem.

Thanks,
Dan
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 12

Expert Comment

by:esoftbg
ID: 12192002
Use
  TSpeedButton  // does not get the focus
instead of TButton and TBitButton
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12192033
Sorry,
I just try to help ....
0
 

Author Comment

by:DanDaemon
ID: 12192170
2esoftbg,

NP... In example I described a very simple form, just for example... but in real application,
my form has many components and this components can have focus also, but when some
information is shown in TWebBrowser it means that TEdit should get focus...

Thanks,
Dan
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12194667
The hammer/egg approach would surely be to have coding in all of the relevant twebbrowser events which brings focus back to the tedit.

Where there is code already present in the onwhatever event then put the setfocus at the end of the custom coding.
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 12194844
I see what you mean... error messages.
0
 

Author Comment

by:DanDaemon
ID: 12197982
Dear moorhouselondon,

Do you have sample code?

Thanks,
Dan
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12198236
can you show the code on how you are "loading information in TWebBrowser"???

it might be as simple as:
try
  //load info into WebBrowser, whatever you are doing here
finally
  edChatMess.SetFocus;
end

if you want to get the focus back to WHATEVER control was focused before loading the data, you would do something like:

var
  aControl:TWinControl;
begin
  aControl:=ActiveControl; //grab the control that has the focus
  try
    WebBrowser1.Navigate('www.experts-exchange.com');
  finally
    ActiveControl:=aControl //return the focus to whatever control had the focus
  end
end;
0
 

Author Comment

by:DanDaemon
ID: 12198276
2BlackTigerX,

I was using same code, but it did not work properly when user clicks on TWebBrowser body.
After this click focus to TEdit does not work.

Thanks,
Dan

P.S. I want to know that somebody knows low-level WinAPI functions to set focus. Need small example.
0
 
LVL 31

Accepted Solution

by:
moorhouselondon earned 250 total points
ID: 12198418
Exactly same idea as esoftbg's second post, just extending to the onnavigatecomplete2 event, but though an initial navigate works fine (you can load up a web page and edit receives focus), if you click further then (I've got rid of the errors) the web control retains focus.  Figuring that the browser was waiting for a click on a link I used a webbrowser stop call, followed by edit1.setfocus but that didn't work.

(The code is straight from the Delphi Help, with a couple of additions).

var
  CurDispatch: IDispatch; {save the interface globally }

procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);

begin
  if CurDispatch = nil then
    CurDispatch := pDisp; { save for comparison }
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
  if (pDisp = CurDispatch) then
  begin
    Beep; {the document is loaded, not just a frame }
    CurDispatch := nil; {clear the global variable }
    webbrowser1.Stop;
    edit1.SetFocus;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
webbrowser1.Navigate('www.ebay.co.uk');
end;


0
 
LVL 6

Expert Comment

by:pritaeas
ID: 12198797
Can't you use the WinAPI function SetFocus(hWnd) ? Seems to me that is what you want. See MSDN for details.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 110
Convert GUI app into console app for Win32 Env 5 125
Installshield for Embarcadero EX 10.1 Berlin 4 72
Browsing a TTreeView in Delphi 5 31
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

740 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