Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a link into the same browser window or tab from Process.Start?

Tags:

c#

.net

winforms

I am trying to open a browser window from a LinkLabel on a Windows Form. When clicked, control passes to the LinkClicked event and the code invokes the default browser using:

System.Diagnostics.Process.Start("http://www.google.com");

I'd like to be able to click the link (i.e. run Start multiple times) but only into the same browser window or tab. Of course, multiple clicks opens a new tab to Google each time. I know how to specify a named window using a link like:

<a href="http://www.google.com" target="googlewin">Click Here!</a>

But how would I do this in the Start command?

ETA: I clicked on Internet Explorer's own linkable link on the About form, and it opens a new window each time, so perhaps not even Microsoft can do this. Hmmm.

like image 652
Cyberherbalist Avatar asked Dec 30 '14 23:12

Cyberherbalist


1 Answers

For Internet Explorer you can do this using SHDocVw assembly.

Instead of using process.start just create an instance of SHDocVw.InternetExplorer and use it to navigate in same browser whenever you want. Here's a simple example.

private SHDocVw.InternetExplorer IE;

private void Form1_Load(object sender, EventArgs e)
  {
     IE = new SHDocVw.InternetExplorer();
     IE.Navigate("http://stackoverflow.com/");
     IE.Visible = true;
  }

private void button1_Click(object sender, EventArgs e)
  {
     IE.Navigate("http://google.com/");
  }

If you specifically wish to use Process.start then for internet explorer you can iterate through SHDocVw.ShellWindows to find the internet explorer you wish to use for navigating.

foreach (SHDocVw.InternetExplorer IE in new SHDocVw.ShellWindows()) {
    if (IE.FullName.ToLower.Contains("iexplore") & IE.LocationURL.ToLower.Contains("someurl")) {
        IE.Navigate("http://google.com/");
    }
}
like image 65
prem Avatar answered Oct 04 '22 21:10

prem