Try to search your question here, if you can't find : Ask Any Question Now ?

PreviewKeyDown with System.Diagnostics.Process.Start(some url) opens two browsers

HomeCategory: stackoverflowPreviewKeyDown with System.Diagnostics.Process.Start(some url) opens two browsers
Avatarcraig asked 5 months ago

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.

1 Answers
Best Answer
Avatarnaveen answered 5 months ago
Your Answer

12 + 2 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com