Clickable URL in a Winform Message Box?

后端 未结 5 2092
借酒劲吻你
借酒劲吻你 2020-11-29 10:50

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

相关标签:
5条回答
  • 2020-11-29 11:24

    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.

    0 讨论(0)
  • 2020-11-29 11:36

    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.

    0 讨论(0)
  • 2020-11-29 11:41

    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.

    0 讨论(0)
  • 2020-11-29 11:43

    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.

    0 讨论(0)
  • 2020-11-29 11:44

    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.

    0 讨论(0)
提交回复
热议问题