I want to implement a LinkLabel so that when clicked on it by middle mouse button was performed opening the link in a browser, and then a form with LinkLabel back automatically become activated.
For this was written the code below. But it does not work. After clicking the middle mouse button on the link link opens, but the form is not active. Why? And how to fix it?
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.linkLabel1.Text = "https://www.google.com.ua/";
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
if (!this.IsDisposed && !this.Disposing)
{
this.Deactivate += new EventHandler(Form1_Deactivate);
}
}
System.Diagnostics.Process.Start(this.linkLabel1.Text);
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (!this.IsDisposed && !this.Disposing)
{
this.Deactivate -= new EventHandler(Form1_Deactivate);
this.Activate();
}
}
}
}
Edited: After answering @King King, I found that this problem occurs only in the browser Opera. On Firefox and Google Chrome his solution (with sleeping thread on 500 msec) and my solution (which code above) works fine, if Firefox/Google Chrome is not running or not minimized. If the Firefox/Google Chrome minimized and click on LinkLabel on my form, the browser is unfolds, but after that the form is not activated.
To sum up: Unfortunately, cross-browser solutions yet to be achieved... Firefox and Google Chrome do not work if they are minimized. And Opera is in general resists as he can, the interception of the active plan.
I know that the solution to this problem exists. For example in IM client QIP implemented that I want to implement. And there, after clicking on the link window focus is restored independently of the browser.