views:

793

answers:

2

Hello,

Let's say I have internet explorer embedded in a windows form and I then navigate to a random page on the web. How can I determine when a textbox (or any other control that can accept text input for that matter) becomes the item in focus? Basically, every time the mouse is clicked I can check to see if the item in focus is a textbox or some other control for text input and then act appropriately.

Thanks in advance,

Bob

+1  A: 

You still haven't explained the roll of the WebBrowser but the problem seems to be tracking the Input Focus. I don't know of a Form level event but you can hook an eventhandler to the Enter or GotFocus event of all relevant Controls.

// in Form_Load
foreach (var control in this.Controls)  control.Enter += OnEnterControl;


private void OnEnterControl(object sender, EventArgs e)
{
  focusControl = (sender as Control);
}
Henk Holterman
I'm getting a handle to the activex IE browser control that is embedded in a windows form, then using this handle to send input to the browser control such as to click on a particular location programmatically. Your solution might work for me. I'll try it out then check it as the answer if it does
Beaker
This will only work for winform controls. This will not work for the MSHTML controls inside the webbrowser, which I think is what the OP is asking for.
jeffamaphone
@jeffamaphone: see the comments under the OP.
Henk Holterman
You are right according to the question I asked. I re-asked this question with the parameters of the solution I need made more clear. I need an answer that involves using a Windows handle to the IE control.
Beaker
A: 

I believe what you want to do is sink the HTMLTextContainerEvents2 dispinterface and respond to onFocus (and potentially onBlur).

You're right that you'll have to do the interop via pinvoke yourself. I assume you can get IHTMLElement pointers to all of the objects you want to track (by using getElementsByTagName or some such thing). Once you have that,

  1. Query the IHTMLElemnt for IConnectionPointContainer.
  2. Call IConnectionPointContainer::FindConnectionPoint(DIID_DHTMLTextContainerEvents2)
  3. Call IConnectionPoint::Advise() on the IConnectionPoint obtained in step 2.
  4. And of course you need to implement IDispatch::Invoke() which will be your callback.
  5. Handle DISPID_HTMLELEMENTEVENTS2_ONFOCUS, etc.
jeffamaphone
And don't forget to disconnect when you're done.
jeffamaphone
So far I've been using SendInput and SendMessage to achieve a lot of functionality. I would prefer an answer that involves using a handle to the IE control. I'm just looking to operate on the windows controls in the IE control so I don't think any special IE objects would be required.
Beaker
Well, those aren't windows controls in the IE control. MSHTML renders all its own controls (except combobox on IE6). So you can't just get handles to those controls because they don't have handles. You can use spy++ to verify.
jeffamaphone