1

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.

Anatolii Humennyi
  • 1,807
  • 3
  • 26
  • 37

2 Answers2

0

Agreed...using SetForegroundWindow() in the Deactivate() worked on my system:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Middle)
        {
            this.Deactivate += new EventHandler(Form1_Deactivate);
            System.Diagnostics.Process.Start(this.linkLabel1.Text);
        }
    }

    void Form1_Deactivate(object sender, EventArgs e)
    {
        this.Deactivate -= new EventHandler(Form1_Deactivate);
        SetForegroundWindow(this.Handle);
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • This doesn't work, in fact `Activate()` works the same as `SetForegroundWindow()` – King King Jul 03 '13 at 15:23
  • Both worked on my system...don't know what to tell ya. I'm running Win 8 Pro. – Idle_Mind Jul 03 '13 at 15:26
  • I'm using Windows 7, what is the default browser on your system? Mine is Google Chrome, strangely the `Process` returned by `Start()` is null? – King King Jul 03 '13 at 15:28
  • I'm using Google Chrome (Version 27.0.1453.116 m) as well. I know there are OS settings that prevent focus from being "stolen" by other applications. In those cases the app will blink in the taskbar instead of getting focus. – Idle_Mind Jul 03 '13 at 15:32
  • How long is the page loaded on your system? My Internet connection is not so good that it has to take about 4 seconds to load Google.com completely. – King King Jul 03 '13 at 15:36
  • It pops up almost immediately on mine. – Idle_Mind Jul 03 '13 at 15:38
  • I am the author of this question, I confirm that your solution does not work on my computer too... My OS Windows XP SP3. My browser Opera. Page of Google with my internet connection is loaded in about 1 second – Anatolii Humennyi Jul 05 '13 at 12:04
0

I don't know why the Idle_Mind's solution can't work for me. However if the current thread is slept for a certain time after the call to System.Diagnostics.Process.Start(this.linkLabel1.Text); it will work. I tested this and you even don't need any kind of subscribing Deactivate event:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Middle)
    {
        System.Diagnostics.Process.Start(this.linkLabel1.Text);
        System.Threading.Thread.Sleep(500);
        Activate();
    }
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • This does not working on my computer too... My OS Windows XP SP3. My browser Opera – Anatolii Humennyi Jul 05 '13 at 11:58
  • @AnatoliiGumennyi you can try increasing the value `500` to higher, If even doing so doesn't work, I can't understand what the problem is here, because the solution works very OK in my system (with Google Chrome however). – King King Jul 05 '13 at 15:05
  • I installed Google Chrome, and really with it your solution works perfectly. I also tested it on Mozilla Firefox. And there, too, everything is OK. It turns out that this is somehow a special Opera opens external links. I think she tries to keep herself out of focus... I have edited my question. I would like to still find a cross-browser solution... – Anatolii Humennyi Jul 08 '13 at 11:56