Clickable URL in a Winform Message Box?
Asked Answered
M

5

28

I want to display a link to help in a message box. By default the text is displayed as a non-selectable string.

Mariejeanne answered 2/12, 2009 at 15:46 Comment(1)
Sounds like two requests. Use a Form instead of MessageBox.Menefee
N
47

One option is display the url in the message box, along with a message and provide the help button that takes you to that url:

MessageBox.Show(
    "test message",
    "caption",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Information,
    MessageBoxDefaultButton.Button1,
    0, '0 is default otherwise use MessageBoxOptions Enum
    "http://google.com",
    "keyword")

Important to note this code cannot be in the load event of the form, the Help button will not open the link.

Noncooperation answered 2/12, 2009 at 16:2 Comment(3)
Thanks, creative solution that in my case will be sufficient. Really appreciate it. (Upvotes to everyone)Mariejeanne
Note that the "keyword" will be appended after a # to the URL as an anchor reference. I.e. google.com#keywordWordage
Clicking on the help button with this code in C# doesn't do anythingEngird
C
5

You can use the LinkLabel control on your own Form for this. Unfortunately, the MessageBox form cannot be customized in this way, so you would need to create your own Form to mimic the MessageBox for your purposes.

Cenesthesia answered 2/12, 2009 at 15:51 Comment(0)
B
5

MessageBox won't do that. You'll either need to use the TaskDialog (introduced in Vista) or create your own dialog.

--Edit--
There are ways to fake the task dialog on XP. There are a few articles on CodeProject.com that I've used in the past.

Basso answered 2/12, 2009 at 15:52 Comment(0)
M
2

You have to create your own form, instead of the built-in MessageBox, and you can use a LinkLabel on it.

However on the built-in MessageBox a Help button could be displayed among the buttons.

Mide answered 2/12, 2009 at 15:55 Comment(0)
P
1

You could use some custom code with LinkLabel like this:

        if (hyperLinks != null)
        {
            foreach (var link in hyperLinks)
            {
                var linkLabel = new LinkLabel();
                linkLabel.Text = link;
                linkLabel.Width = WhateverParentPanelYouHave.Width;
                linkLabel.Click += LabelClicked;
                WhateverParentPanelYouHave.Controls.Add(linkLabel);
             }
         }

Where hyperLinks is a list of strings for your links.

Then for your LabelClicked handler:

      private async void LabelClicked(object sender, EventArgs e)
      {
        var linkLabel = (LinkLabel) sender;
        var path = linkLabel.Text;
        try
        {
            await Task.Run(() => Process.Start($@"{path}"));
        }
        catch (Exception ex)
        {
            MessageBox.ShowMessage(ex.Message, @"An Error Has Occurred");
        }

      }

Keep in mind, this is your own form with the LinkLabel control added to it. You'll have to inherit from Form and use the ShowDialog() method to display your form with all your controls added to it.

Pamilapammi answered 16/7, 2018 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.