winforms - C# UserControl, Add Event Handler Manually -
i trying add new mouse-click event handler manually web browser control, since web browser not have mouse click event in designer properties.
i need run code whenever user clicks inside web browser control. workaround adds in handler web browser not have through existing documentcompleted event handler.
when run code below, returns null exception. wondering if possible done way.
edit: have added while loop ensure webbrowser1 completes loading. however, gets stuck in while loop. mean webpage loading never complete?
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { try { while (webbrowser1.readystate != webbrowserreadystate.complete) { application.doevents(); } webbrowser1.document.body.mousedown += new htmlelementeventhandler(body_mousedown); } catch (exception ex) { debug.writeline(ex.stacktrace); debug.writeline(ex.message); } } private void body_mousedown(object sender, htmlelementeventargs e) { if( e.mousebuttonspressed == mousebuttons.left) { messagebox.show("file in web browser selected"); } }
your code must like:
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { if (webbrowser1.readystate == webbrowserreadystate.complete) { webbrowser1.document.body.mousedown += new htmlelementeventhandler(body_mousedown); } }
Comments
Post a Comment