I have a WebBrowser object in a WPF Page and I'm trying to do something whenever the user interacts with the page. I have intially tried to use the events associated with the WebBrowser object but they don't seem to be firing. Below is a simplified example of what my code is trying to do:
webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown);
with the event handler as:
void webBrowser_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.MessageBox.Show("Pressed");
}
However when I run the page and click inside the WebBrowser no message box is displayed.
Apologies, originally I had mentioned that it was a System.Controls WebBrowser rather than a Forms browser.
Mouse events are not supported by the WebBrowser control, according to the documentation. You need to hook up handlers to the DOM events provided by the document being displayed in the control, using the WebBrowser.Document property. This post has an example of how to do this.
Add the ms html com library
Once the WebBrowser.LoadCompleted event fires try this:
mshtml.HTMLDocumentEvents2_Event doc = ((mshtml.HTMLDocumentEvents2_Event)Browser.Document);
doc.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(doc_onmouseover);
or use some other event.
Hope this helps someone.
Related
I have a problem in my wpf project. I have a webbrowser control. What I want is to know whether anybody has clicked on the webrowser control. I used the mousedown event. But to my surprise it is not firing the event. In the webbrowser control I find that only Navigated and Navigating events are fired. Please let me know how can I get mousedown event? Thanks for your time.
Mouse events are not supported by the WebBrowser control. Please refer to the following (duplicate) question for more information about how you could solve this.
WPF WebBrowser Mouse Events not working as expected
MACMAN, add event handler manually, like this:
public MainWindow()
{
InitializeComponent();
AddHandler(FrameworkElement.MouseDownEvent, new MouseButtonEventHandler(WebBrowser_MouseDown), true);
ps: See this topic: https://social.msdn.microsoft.com/Forums/en-US/61807025-d4c4-41e0-b648-b11183065009/mousedown-event-not-working-wpf?forum=wpf
I got the solution by injecting javascript that adds a html mousedown event to the website. The javascirpt in turn invokes a wpf function that is written into a class using ComVisible[true].
http://sekhartechblog.blogspot.in/2012/04/webbrowser-javascript-communication-in.html
i am using System.Windows.Controls.Webbrowser class in my C# app.
I want to recognize if the document title changes.
Therefore i listen on the LoadComplete Event and getting the title
public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = browser.Document;
setTitle(doc.Title);
}
The problem now is, that the title could change during loading. In this case the LoadCompleted event would not fire.
Next problem is. I have a html application inside which navigates through javascript functions in the application. In case of using these javascript links, the event would not fire, too.
I am looking for an event like OnTitleChange from CHtmlView (c++)
Is there a possibilty to solve my issue?
Thanks for help.
Not supported by the WPF webbrowser.
You can use windows forms interop to show a windows forms webbrowser control then subscribe to the DocumentTitleChanged event.
This is the working code for me:
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigate(URL);
browser.DocumentTitleChanged += new EventHandler(browser_DocumentTitleChanged);
WindowsFormsHost winFormsHost = new WindowsFormsHost();
winFormsHost.Child = browser;
AddChild(winFormsHost);
public void browser_DocumentTitleChanged(object sender, EventArgs e)
{
setTitle(browser.DocumentTitle);
}
I've created an application that grabs data from a web page and saves them to a database. This pages sends some __postbacks. After the postback the page loads some data that I need to save. I do not know how to detect this event so I've created a button which grabs the data when pressed by the user.
How can I automate the process so that the user won't have to check for the postback himself?
That is how can I detect the postback event in my webbrowser control?
The webbrowser control has a DocumentCompleted event. You can use the WebBrowser.Document property to look for something specific in this event.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Document.Body.InnerHtml.Contains("some way that I know I am ready"))
}
This may come across as incredibly stupid, but I cannot figure out if:
I am an idiot
I have misunderstood something
The MS Web Browser control is bugged
I prefer to think that it is the latter.
I have a Web Browser control in a WinForms user control. It has been added to the control at design time, and in theory, in the Load event of the control it should navigate to Google.
Seems straightforward.
However.
public partial class TVHost : UserControl
{
public TVHost()
{
InitializeComponent();
}
private void TVHost_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.co.uk");
}
}
This doesn't work. No error, just nothing. Inserting a breakpoint/debug lines shows me that the Load event doesn't even get called.
I decided at this point to check that the Load event is being set correctly in the Designer.cs file.
this.Load += new System.EventHandler(this.TVHost_Load);
Seems legit.
If I remove the web browser control from the form, the load event fires.
I don't understand this one bit, how can a control prevent a method which uses it from firing in the first place?
Moving on, I found this:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d6e427b2-9cc9-4318-bb05-11363025e3f7/
TL;DR for the link is as follows: "Load won't work if you have a webbrowser on the form which is set to Visible = true"
So sure as hell, if I change the default visibility of the webbrowser to false, the load event of the control fires. I can work around the problem by setting the visibility of the browser in the load event.
private void TVHost_Load(object sender, EventArgs e)
{
webBrowser1.Visible = true;
webBrowser1.Navigate("http://google.co.uk");
}
Very odd.
Whilst this "fix" works, I find it incredibly hacky and was wondering if anybody has any explaination for this behaviour?
Amazingly I have found this bug in MS Connect, left over from 2005 - http://connect.microsoft.com/VisualStudio/feedback/details/116535/when-adding-a-webbrowser-control-to-a-user-control-the-load-will-not-fire#
From the discussion in the Connect bug you linked to:
For now, if you want to get the Load event to fire, you can set the URL property of the WebBrowser control in the property grid. The URL can be anything you want, even about:blank if you don't want it to start with a page loaded.
So if you go into the designer and set the WebBrowser's Url property to the string about:blank (which tells the WebBrowser to load an empty page), then your user control should start getting its Load event again.
I have a custom Outlook 2010 Ribbon tab that has the type Microsoft.Outlook.Appointment.
In that tab, I have several buttons that change the current appointment item, and call its Save method. However, that method always changes the focus to the first tab of the inspector. I want the focus to remain on my custom tab.
Here is my current code:
private void ButtonSaveAppointment(object sender, RibbonControlEventArgs e)
{
Outlook.Inspector inspector = (Outlook.Inspector)this.Context;
Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)inspector.CurrentItem;
appointment.Save();
this.RibbonUI.ActivateTab(this.Tabs[0].ControlId.ToString());
}
This does not work for me. Can anyone tell me what am i doing wrong?
Thank you.
This is what i use in my load event:
ThisRibbonCollection ribbonCollection = Globals.Ribbons[_inspector];
ribbonCollection.RibbonSMS.RibbonUI.ActivateTab("the_name_of_the_ribbon_tab");
And that would correspond to what i see in your code:
this.RibbonUI.ActivateTab(this.Tabs[0].ControlId.ToString());
That code fires well in the load event, but i tested it and it does not work when you are calling it from a function. Please try and move it to the load event and you will see that the code works. My guess is that it does not work properly from a method but just form the load event.
Good luck