Tutorial :How come my code works when I use a MessageBox.Show() in between, and does not work without it?



Question:

I embedded .NET's WebBrowser into a form that has some other buttons.

The application that I'm developing does not use a keyboard, but uses a touchscreen. So it actually uses the mouse only. To scroll the webbrowser down, I made two custom buttons. When I click those buttons, it should send a "PageDown" key stroke to the browser, and the browser should scroll down.

So the code on the click event is as follows:

        theForm.Activate();          Application.DoEvents();          theBrowser.Focus();          Application.DoEvents();          SendKeys.Send(key);  

Where 'theForm' is the form and 'theBrowser' is the browser instance.

So when I click the button, nothing happens. When I first click on the browser, and then click the button, it DOES work. But im giving it focus right? And i gave the application enough space to set the focus?

But now what I think is strange. When I put a MessageBox.Show("HELLO"); in the code, like this:

    theForm.Activate();      Application.DoEvents();      theBrowser.Focus();      MessageBox.Show("HELLO");      Application.DoEvents();      SendKeys.Send(key);  

...it does work immediatly when I click the button.

&nbsp

So the question is: Why does this code not work when i leave the MessageBox.Show() out, but does when I do use the MessageBox.Show()?

Hope you can help, thanks in advance...


Solution:1

To me this looks like the wrong way to be going about this.

Try something along the lines of this C++ sample or this C# sample.

At least be explicit about the HWND you are sending the message to if you are going to be simulating key presses.


Solution:2

Without getting into whether your approach is correct. (and I suspect it is not).

I would gamble that You have a race condition. Which mean that the browser control needs more time to load and respond to events than you are letting it.

So when you do Message.Show, suddenly the thread that you send the events is blocked and it is letting the browser control complete intializing or something else.

It is hard to know from your question whether you are running on Mobile, or regular desktop because there are better approaches to deal with touch. Look at Window7 Api for multi Touch or WPF 4.0 (which is shared by Surface Touch SDK aswell).

Hope that helps. Ariel


Solution:3

It may be that the browser control is still loading a document and the message box gives it enough time to finish. If so, wait until the browser's IsBusy property is false or use the DocumentCompleted event.


Solution:4

I do not know, but in general in my experience it is a bad idea to try to change the focus programmatically like this, you always end up on a bug hunt. There are lots of ways to get windows to scroll though, so maybe you should try something else? For example JavaScript in the web browser can scroll the window effectively.


Solution:5

I suspect that you have a race here what gets done first. Focusing the control might not be instantaneous and the keystroke you send went elsewhere, whereas after closing the message box the browser control is already focused and gets its keystroke.

Also, to quote a note from MSDN about Control.Focus():

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

So using one of those might be better than using Focus().


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »