0

I am very interested in C# winforms. I decided to make it open a browser window once I click on a link label with the default browser. I searched on the internet and found the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                VisitLink();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open link that was clicked.");
            }
        }

        private void VisitLink()
        {
            // Change the color of the link text by setting LinkVisited
            // to true.
            linkLabel1.LinkVisited = true;
            //Call the Process.Start method to open the default browser
            //with a URL:
            System.Diagnostics.Process.Start("http://www.microsoft.com");
        }
    }
}

When I tried it in my code, I clicked on the link but nothing shows up. Not even an exception pops up, which made me confused. Can anyone help me? Thanks!

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Your code isn't starting a browser. It's trying to execute a URL as if it were an application document and hoping something in the OS will catch it and start a browser. `I searched on the internet and found the following code` that's a very bad idea. It means you have no idea what the code does or even if it works. So when it fails, you have no idea what went wrong or what to check. Perhaps there's no browser registered – Panagiotis Kanavos Jun 30 '21 at 11:42
  • 1
    Are you running this code on Windows? What is the default web browser? You need to investigate why `Process.Start` doesn't launch the default web browser. First, make sure that this line actually executes (use debugging or add a MessageBox after it). After this, try to launch it through a command-line (`start http://www.microsoft.com`). You might also want to go through alternative answers on this question: https://stackoverflow.com/questions/4580263/how-to-open-in-default-browser-in-c-sharp – default locale Jun 30 '21 at 11:48
  • Have you tried opening a console and pasting that URL? Do you get an error? Are you targeting .NET Core or .NET Old? Using a registered application to open a document (or URL) is a function of the Windows Shell. In .NET Old, the default behavior of `Process.Start` was to use the shell to execute an application or document path. In .NET Core the default is to *not* use the shell because it may not even exist eg on Linux or Mac – Panagiotis Kanavos Jun 30 '21 at 11:50
  • `start http://www.microsoft.com` actually calls the shell and tells it to do whatever is the default action for the argument, in this case the URL. The equivalent in code would be to create a `ProcessStartInfo`, fill it and set `UseShellExecute` to `true` – Panagiotis Kanavos Jun 30 '21 at 11:51
  • 1
    @defaultlocale Using windows. Default web browser chrome. – Chiang Cliff Jun 30 '21 at 11:52
  • 1
    @ChiangCliff but what .NET version are you using? .NET Old? .NET Core? .NET 5 is actually .NET *Core* 5 – Panagiotis Kanavos Jun 30 '21 at 11:52
  • `start https://microsoft.com` succeeds. – Chiang Cliff Jun 30 '21 at 11:52
  • @PanagiotisKanavos I am using .NET Core – Chiang Cliff Jun 30 '21 at 11:53
  • Which means the shell isn't used by default. You'll have to use a `ProcessStartInfo` object instead of calling `Process.Start` – Panagiotis Kanavos Jun 30 '21 at 11:55
  • @ChiangCliff See [this answer](https://stackoverflow.com/questions/4580263/how-to-open-in-default-browser-in-c-sharp#43232486). Things [have changed](https://github.com/dotnet/runtime/issues/17938) in .Net Core and you'll need to call the cmd explicitly like this: `Process.Start(new ProcessStartInfo("cmd", "/c start http://www.microsoft.com") { CreateNoWindow = true })` – default locale Jun 30 '21 at 11:58
  • 1
    @defaultlocale not at all. There never was any reason to start an extra console with `cmd /c` either in .NET Old or .NET Core. You can use `Process.Start("start http://....")`. That's equivalent to `Process.Start(new ProcessStartInfo(someUrl){UseShellExecute=true};`. `start` calls the shell and passes its arguments to it. That's the job of `UseShellExecute=true`. That's what changed between .NET Old and .NET Core – Panagiotis Kanavos Jun 30 '21 at 13:53
  • @PanagiotisKanavos Good to know, thanks! It seems to me you've posted more than enough information in the comments to make a complete answer :) – default locale Jun 30 '21 at 14:00

1 Answers1

1

The link is correctly marked as visited after the click ? If not, the event is probably not even got fired. Check if you correctly add the callback on the click event