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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

PROBLEM: Lost a focus

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
DanDaemon
Asked:
DanDaemon
  • 4
  • 4
  • 3
  • +2
1 Solution
 
esoftbgCommented:
Set by ObjectInspector:

  edChatMess.TabStop := True;

for all other controls:

  ContolName.TabStop := False;

0
 
esoftbgCommented:
procedure TForm1.WebBrowserEnter(Sender: TObject);
begin
  edChatMess.SetFocus;
end;
0
 
DanDaemonAuthor Commented:
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!

 
esoftbgCommented:
Use
  TSpeedButton  // does not get the focus
instead of TButton and TBitButton
0
 
esoftbgCommented:
Sorry,
I just try to help ....
0
 
DanDaemonAuthor Commented:
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
 
moorhouselondonCommented:
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
 
moorhouselondonCommented:
I see what you mean... error messages.
0
 
DanDaemonAuthor Commented:
Dear moorhouselondon,

Do you have sample code?

Thanks,
Dan
0
 
BlackTigerXCommented:
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
 
DanDaemonAuthor Commented:
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
 
moorhouselondonCommented:
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
 
pritaeasSoftware EngineerCommented:
Can't you use the WinAPI function SetFocus(hWnd) ? Seems to me that is what you want. See MSDN for details.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now