?
Solved

TWebBrowser focus problem

Posted on 2007-10-02
5
Medium Priority
?
3,136 Views
Last Modified: 2013-11-18
Hello!

I have problem with TWebBrowser ActiveX control. Every time WebBrowser gains focus, form stops receiving OnKeyDown/OnKeyPress/OnKeyUp events. This can be workarounded by Application.OnMessage but there's another problem. I have two controls on form, TWebBrowser and TEdit. When I click on WebBrowser, there is no way Edit can regain focus by code (i can still click on Edit ;). Edit1.SetFocus, Form1.ActiveControl and SetFocusedControl don't work.

This is a handler in ApplicationEvents.OnMessage. It doesn't work, focus remains on WebBrowser (and yes I tried - "Test" message would show)

  if (GetKeyState(VK_CONTROL) < 0) and (Msg.Message = WM_KEYDOWN) and
    (Msg.wParam = VK_HOME) then
  begin
   Edit1.SetFocus;
//  Showmessage('Test');
   Handled:=true;
  end;

Since WebBrowser is TWinControl, I suspected it may behave like TForm, but BringToFront and SetForegroundWindow API call didn't work.

Only workaround I have found by now is to hide and show a form (it is unacceptable). Is it a problem with ActiveX or something else?

I use Delphi 2006, Windows Vista x64 and IE7
0
Comment
Question by:TiborKi
5 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 2000 total points
ID: 20002489
I find that if you focus another control first, then it does work

eg.
  button1.SetFocus;
  edit1.SetFocus;

I think the thing is to make your app really think that the edit control is a "new" focus
0
 
LVL 1

Author Comment

by:TiborKi
ID: 20004131
Thanks, it works.
Only one subquestion: is it possible to somehow simulate keypress so tex would appear in Edit when someone types in TWebBrowser after edit regains focus in Application.OnMessage handler.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 20004680
you can set the Edit1.SelText property (or even the Edit.Text if you want the whole thing)
or you can do a postmessage(edit1.handle, wm_char, ord('A'), 0); for example
might be sendmessage, not sure
0
 
LVL 3

Expert Comment

by:smot
ID: 22965277
or:
    ActiveControl := nil;
    Edit1.SetFocus;
0
 

Expert Comment

by:ejla51
ID: 37761387
>ActiveControl := nil;
> Edit1.SetFocus;

Very good solution! I had bloody troubles /w this issue!!!
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.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 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