Clickable URL in a Winform Message Box?

一世执手 提交于 2019-12-28 01:25:46

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1833747/clickable-url-in-a-winform-message-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!