Okay so for a client i'm working on a compact framework application.
Early on, I realised that trying to render user-created content in datagrids just wasn't going to work.
The main reason for this was that long text wouldn't wrap vertically and expand the rows, like you'd expect it to (at least from a web background).
Further to this, when you had a layout such as
Label Value
Label Value
If the value was long, you could set AutoGrow and it would grow horizontally but not vertically.
I tried to get around this with a horrible horrible iterative function which looped over the controls on the form and tried to resize them to hold their content and move other controls down the form like it should.
None of this worked.
In a moment of desperation, I dropped a webbrowser control on the form and found I could set documentText of it to HTML. voila! Layout problems solved..
This had a couple of challenges though, which is the reason for this post.
1) Navigation. In order to capture input, ie links, submit buttons etc.. you typically need a server side page. Plain html will not form-submit to itself, well not that I could find and you certainly can't href unless you use # anchor tags.
I used a blank html document (just html and body tags) to facilitate linking. Links navigate to this blank html document ?id=xx and then I parse the querystring.
2) Focus.
This is the real reason for this post.
Focusing web browsers is poorly implemented at best.
Because the webbrowser control grabs focus to allow document navigation, things dont work too well. I discovered that when the webbrowser takes up the whole form, this isn't an issue but if you have a smaller webbrowser and say, a search box, then its very difficult to control the focus.
If you are using the activated event on a form to add data etc, on the second visit to the form, the focus is still on the webbrowser.
Textbox1.focus() does nothing.
WebBrowser1.focused = false
The solution to this problem is WebBrowser1.Enabled = false.
Disable the web browser. Disabled controls cant have focus.
Just remember to renable it when you want the user to be able to swich back to it.
WebBrowser1.Enabled is not in intellisense though, but it does work, at least on the emulator. I have to try the actual devices yet.
If you combine this with Form1.Keypreview = true and a Form1_keydown handler, you can also use a button on the phones keypad to jump the focus back out of the webbrowser by disabling it.
I hope this helps someone else, I searched for days to find a solution to this focusing stuff.