Cyberherbalist
Cyberherbalist

Reputation: 12319

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

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.

Upvotes: 6

Views: 5322

Answers (3)

prem
prem

Reputation: 3538

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/");
    }
}

Upvotes: 1

Justin Yanke
Justin Yanke

Reputation: 21

I feel like you are approaching this the wrong way.

When clicked, control passes to the LinkClicked event and the code invokes the default >browser using:

Actually, it does not invoke the default browser, but rather creates a new instance of your default browser. Yes, you could obtain the handle of the newly-created process and use a rather elaborate way of adding more tabs, but such practice would be re-inventing the wheel. Your best bet is to create your own WebBrowser Control. Process.Start is a way to create a new process and should not be used as a means of changing a process that is already running.

Upvotes: 0

codekaizen
codekaizen

Reputation: 27419

Ditch Process.Start: it most likely can't be done. The executable would need to support command line options which allow you to select a tab and perform the navigation. I don't see any such support in any browser: Chrome; IE or Firefox

The only reasonable alternative I can think of is to use the published interop mechanisms to work with a browser window. Once you have a browser window, you can script it to set the window location. With this approach you'd need to use the WebBrowser control or something like Awesomium.

Upvotes: 0

Related Questions