0

In this question (and accepted answer) a simple WPF program sets up a PreviewKeyDown handler to call Process.Start on a directory name to open that folder in Windows File Explorer.

If I change the handler definition to open up a URL as follows:

data_grid.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.O && data_grid.SelectedItem is DirectoryInfo info)
        System.Diagnostics.Process.Start("https://www.github.com");
};

it opens up two browser tabs when I press the o key.

The original version did not open two File Explorer windows.

Why does the URL version open two browser windows? What's a good way to get it to only open one?

To make this concrete and more explicit, here's an entire program which demonstrates the issue.

MainWindow.xaml:

<Window x:Class="WpfUrlsDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUrlsDataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfUrlsDataGrid
{
    public class Address { public string Url { get; set; } }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var data_grid = new DataGrid()
            {
                IsReadOnly = true,
                AutoGenerateColumns = true,
                ItemsSource = new[] 
                {
                    new Address() { Url = "https://www.google.com" },
                    new Address() { Url = "https://www.github.com" },
                    new Address() { Url = "https://www.stackoverflow.com" }
                }
            };

            data_grid.PreviewKeyDown += (s, e) =>
            {
                if (e.Key == Key.O && data_grid.SelectedItem is Address address)
                    System.Diagnostics.Process.Start(address.Url);
            };

            var dock_panel = new DockPanel();

            dock_panel.Children.Add(data_grid);

            Content = dock_panel;
        }
    }
}

Here's what the example program looks like:

enter image description here


UPDATE in response to comments below

If I add a breakpoint at the following line:

System.Diagnostics.Process.Start(address.Url);

Only one browser tab opens! Very strange that the behaviour is different when debugging.


UPDATE in response to comments below

If I change the browser that is used to Microsoft Edge as follows:

System.Diagnostics.Process.Start(String.Format("microsoft-edge:{0}", address.Url));

it still opens up two tabs.


UPDATE

The issue seems to be intermittent. Most of the time, it opens two browser tabs. However, occasionally it will open only one tab.

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 1
    Use the debugger and see if your function is called twice by setting a breakpoint there – Denis Schaf Apr 11 '19 at 05:17
  • 1
    Also check if this changes when you change your standard browser to a different browser just to make sure its not the browser trying to open a start-page or something like that – Denis Schaf Apr 11 '19 at 05:27
  • Hi @DenisSchaf! Thanks for taking a look. I've added some updates to the question in response to your comments. – dharmatech Apr 11 '19 at 05:57
  • try using the `KeyDown` event instead of `PreviewKeyDown` – Denis Schaf Apr 11 '19 at 06:02
  • I tested and it works for me . it opens only one window. Is it something wrong with keyboard ? May be sending many 2 keystrokes instead of 1? Try using some other key instead of o maybe? i once had this problem with printscreen key. Just one keypress sent 5 keystrokes. So try to change the keyboard? – na th Apr 11 '19 at 09:03
  • @nath The issue occurs both with the external keyboard as well as the laptop keyboard. – dharmatech Apr 11 '19 at 16:57
  • 1
    https://stackoverflow.com/a/43211503/17034 – Hans Passant Apr 11 '19 at 18:02
  • @HansPassant Thanks for the reference. It looks like in your answer there, you're using a `BeginInvoke` that's part of Windows Forms. The demo app above is in WPF. Is there a good an equivalent of `BeginInvoke` for WPF? – dharmatech Apr 12 '19 at 01:42

2 Answers2

1
data_grid.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.O && data_grid.SelectedItem is DirectoryInfo info)
        System.Diagnostics.Process.Start("https://www.github.com");
    **e.Handled = true;**
};
  • Hmm... So, with this approach, it does seem to eliminate the double browser tab issue! However, the 'up' and 'down' arrow keys no longer change the current selected row. – dharmatech Apr 11 '19 at 17:02
  • A slight modification of this approach seems to work! I've added an answer based on this approach. – dharmatech Apr 11 '19 at 17:08
0

This approach seems to resolve the issue:

data_grid.PreviewKeyDown += (s, e) => 
{
    if (e.Key == Key.O && data_grid.SelectedItem is Link link)
    {
        System.Diagnostics.Process.Start((data_grid.SelectedItem as Link).Url);

        e.Handled = true;
    }
};

Note that it's a slight modification of the answer by @user11344985. It moves the e.Handled = true; line into the if body.

dharmatech
  • 8,979
  • 8
  • 42
  • 88