如何在 RichTextBox 中创建超链接? [英] How can I make a hyperlink work in a RichTextBox?

查看:69
本文介绍了如何在 RichTextBox 中创建超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 www.stackoverflow.com 添加到我的 RichTextBox 并运行该程序时,它以蓝色显示并作为超链接显示,但是当我单击它时什么也没有发生.我该如何解决这个问题?

When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?

推荐答案

  1. 确保文本属性包含有效的 url.例如.http://www.stackoverflow.com/

DetectUrls 属性设置为 true

LinkClicked 事件编写一个事件处理程序.

Write an event handler for the LinkClicked event.

就我个人而言,我不会像 Microsoft 建议的那样将 "IExplore.exe" 作为参数传递给 Process.Start 调用,因为这假定它已安装,并且是用户的首选浏览器.如果您只是将 url 传递给进程启动(如下所示),那么 Windows 将做正确的事情并使用适当的 url 启动用户的首选浏览器.

Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.

private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
    System.Diagnostics.Process.Start(e.LinkText);
}

这篇关于如何在 RichTextBox 中创建超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆