click on a link event webbrowser c#? - c#

I want to open a link in IE browser from the web browser control in my winform ... the webbrowser control navigate to a page when the user click on a link it opens in the internet explorer browser.

Basically handle the WebBrowser.Navigating Event, cancel the navigation and open up the url in explorer like this:
private void WebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
Process.Start("iexplorer.exe", e.Url);
}

Related

tabPage1_Click event not triggering

I am using a tab in my program to switch between two forms. I put the code required to switch between forms within the tabPage1_Click event, but it doesn't trigger when the tab is clicked.
I attached the code and properties of the tab. Please let me know if any other information is required to know the problem. Thanks.
private void tabPage1_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.ShowDialog();
this.Close();
}
There are 2 things involved here. Tab control and Tab pages. Tab Control is the parent object which has multiple Tab pages in it.
You have event handler for Tab Page which is tabpage1_Click and not for Tab Control.
tabpage1_Click will be triggered when you click on tab page 1(not on the tab page header).
If you need to capture an event when you click on tab page header use Tab Control click event, something like below.
private void tabControl1_Click(object sender, EventArgs e)
{
//Your code goes here
}
To access the properties of the tab page use tabControl1.SelectedTab

Open new page in standard browser when link is clicked

I can open the link in my standard browser with this code:
public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel the current event
e.Cancel = true;
//this opens the URL in the user's default browser
Process.Start(e.Url.ToString());
}
But the problem is that IE only should be opened when a link on the webbrowser is clicked. When using this code IE also opens when I change the documenttext.
My suggestion would be to take a different approach. At the point in time immediately after the initial page has loaded in the WebBrowser control (Navigated event), you can use the webBrowser1.Document property to retrieve an HtmlDocument instance.
From this you should be able to find your link by using, for example,
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx
Then you can add an event handler to detect when this link is clicked, and in this handler, run your code to start the IE process.

How to automatically accept the "Open/Save Dialog" in a WebBrowser Control?

I am working on a c# Windows utility, which is opening a file using webbrowser control.
webBrowser1.Navigate("URL");
URL is actually an attachment, which is showing as "Open" "Save" dialog
private void WebBrowser1_FileDownload(Object sender, EventArgs e)
{
textbox1.text ="filedownload event fired!!"
}
can I do somthing so that i cancel the file open/save dialog in the background.
As I wanted to automate the process and don't want user to get popped up message.

Open YouTube Embedded Player link in default browser

I'm hosting YouTube Embedded Player in C# WebBrowser control. When I click button "Watch on YouTube" IE opens. Is there any way to open the link in default web browser, for instance, Chrome?
Like this for example:
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
Explanation: change the navigating handler to process the URL and fire the link in the diagnostics (which will open the URL in the user’s default browser).

Capture the mouse right click event of a web browser control

I would like to select all when a user right clicks on my web browser control.
I am developing a win forms app, and use web browsers to display my information, because i can use html to style the words.
The right click context menu is not working for me. The options on it are all irrelevant to my app.
But the context menu after a select has been made i want to keep, the copy, cut, paste options.
I can already select all:
getCurrentBrowser().Document.ExecCommand("SelectAll", true, null);
I would just like to do it in the right click event of the web browser?
Handle MouseDown event:
webBrowser.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
and make sure user pressed Right button, then select all:
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
if(e.MouseButtonsPressed == MouseButtons.Right)
{
webBrowser.Document.ExecCommand("SelectAll", true, null);
}
}
This article shows how you can replace the context menu of the Web Browser with your own.
Alternatively, if you execute the following Javascript from within the Web Browser, it will disable the default right-click context menu:
document.oncontextmenu=new Function("return false")
If you are using WinForms rather than WPF, you can set the IsWebBrowserContextMenuEnabled to false to prevent the IE context menu, in which case it will use the ContextMenu you supply on the WebBrowser control.
WPF doesn't have the same property exposed on the Web Browser, so it may not be so easy. In this case you may have to use WindowsFormsHost to host the WinForms Web Browser in WPF.
This works :)
When the context menu shows select all is running pushing out the contextmenu i want, with the copy, paste, cut and so on.
private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webCompareSQL.Document != null)
{
webCompareSQL.Document.ContextMenuShowing += DocMouseClick;
}
}
void DocMouseClick(object sender, HtmlElementEventArgs e)
{
webCompareSQL.Document.ExecCommand("SelectAll", true, null);
}
You need to be sure, that the WebBrowser.Document property is already loaded. Then you can register the Mouse event.
`browser.DocumentCompleted += (s, e) => {
browser.Document.MouseMove += (sM, eM) +=> {
Debug.WriteLine(eM.ClientMousePosition.X);
};
};
`

Categories