0

i have following problem:

I'm using webbrowser control in c# winforms, to navigate to specific url which contains textboxes. Now i would like to act after user enters text inside these textboxes. I've tried the Webbrowser.Document.Body.keydown, keyup and keypress events. Only keypress gets fired, which would be fine but when it fires the content of textbox is still unchanged (as i received the event before it got to specific textbox).

So i tried little workaround, to start a new thread with small delay that would do the magic, but in the created thread I cannot access Webbrowser.Document property because :

Specified cast is not valid

Is there some suitable way to solve the problem I have? Thanks for your help

Edit1: After suggestion of blur and change events, i found that event oninput should do what I want. However, it has some flaw: After i click inside the textbox for the first time, the first key press works fine, however the other are not executed. But when I focus out of the element and click inside it again, it works fine, but when i start to write from the start of the field it ignores the event in some cases..

HtmlElement element = webbrowser.Document.GetElementFromPoint(e.ClientMousePosition);
element.AttachEventHandler("oninput", func);
Pins
  • 217
  • 1
  • 12
  • Add an EventListener to the Submit Button's `click` event and register your [ObjectForScripting](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser.objectforscripting) to receive the notification -- You could also handle (probably, it depends on what you're doing) the `Navigating` event, which also allows to cancel the navigation (if, e.g., some element doesn't contain what is expected). Or maybe both. – Jimi May 23 '22 at 10:36
  • Hello, thanks for your input. Basically I dont have the possibility to use the said button(or anny button). I want to do following: user enters for example key 'X' in textbox and after that my event will get launched with the changed textbox content(so basically it will react to the user entering or removing text from the textbox). So there are no buttons. About the Navigating event, i'm not sure if thats what I need in this situation (after i described it) – Pins May 23 '22 at 10:40
  • Then add an event listener to the `blur` event of the input textbox (or `change`, if you want to handle each key press). – Jimi May 23 '22 at 11:05
  • I´ve tried what u suggested and have some update on the situation, please read the edit in my answer. – Pins May 23 '22 at 12:54
  • I wouldn't add a handler to the `input` event. It's even weirder than `change`. As mentioned, you can use `blur` or, why not, `keyup` (or both, so you have feedback when either a key is pressed or the focus is moved). So you have, e.g., `element = [Document].GetElementById("The ID"); element.AttachEventHandler("onkeyup", (o, a)=> { Console.WriteLine(element.GetAttribute("value"))});` with a WebBrowser Control or `element.addEventListener("keyup", function(e) {[Some Function]);` with WebView2 (I strongly suggest you replace the WebBRowser Control with WebView2) – Jimi May 23 '22 at 16:51
  • Note that you have removed the context in which your code is run. Never do that, especially in an event-driven platform as WinForms. You should store the HtmlElement(s) you're using for this as Field(s) and remember to call `DetachEventHandler()`, then set them to `null` when the WebBrowser Control navigates somewhere else or it's disposed (the Form closes etc.). -- You should add the event listener when `DocumentCompleted` is raised (also note that this event can be raised multiple times - once for each IFrame or *late* JavaScript changes to the DOM) – Jimi May 23 '22 at 16:51
  • Sadly for some reason the onkeyup event doesnt fire for me. My first thinking was to get keypress event (the only key event im receiving actually), set delay to like 0.5sec(so user can fast write more chars) and then do my action. However, i have to do delay on different thread, and whenever i use different thread, the html document is not reacting anymore. – Pins May 23 '22 at 21:14
  • It's not possible that you don't get the `keyup` event (set as `onkeyup` in IE, case sensitive). You may not get an `input` event in case Internet Explorer is in Quirks mode (i.e., the page doesn't have the `` meta tag). If you can't control this, you have to explicitly set the Emulation Mode of IE, using the `HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION`. -- Don't use any kind of *delays*, it's useless. You need to make the WebBrowser Control behave appropriately. – Jimi May 24 '22 at 00:53
  • If you don't know how to set (programmatically) the Emulation Mode to IE11 Edge (`11001`) , see here: [How can I get the WebBrowser control to show modern contents?](https://stackoverflow.com/a/38514446/7444103) -- Note that the `FEATURE_BROWSER_EMULATION` Key may not exist, so you have to check this first and create it if it's missing. – Jimi May 24 '22 at 00:53
  • See also: [Tips and tricks to manage Internet Explorer compatibility](https://learn.microsoft.com/en-us/internet-explorer/ie11-deploy-guide/tips-and-tricks-to-manage-ie-compatibility) and read the `Important Note` at the top of that document (one of the reasons why I *strongly suggest* to move to the WebView2 Control). – Jimi May 24 '22 at 01:00

0 Answers0