首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >富文本框中的链接?

富文本框中的链接?
EN

Stack Overflow用户
提问于 2012-03-25 04:25:24
回答 4查看 30.1K关注 0票数 25

我知道richtextboxes可以检测链接(比如http://www.yahoo.com),但是有没有办法给它添加看起来像文本但实际上是链接的链接呢?比如你可以在哪里选择链接的标签?例如,它不是显示为http://www.yahoo.com,而是显示为Click here to go to yahoo

编辑:忘记,即时消息使用windows窗体

编辑:有没有更好用的东西(比如更容易格式化的)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-25 04:51:42

当然,也可以通过在控件中调用一些WIN32功能来实现,但是如果您正在寻找一些标准的方法,请查看这篇文章:Create hyperlink in TextBox control

有一些关于集成的不同方式的讨论。

问候

更新1:最好的方法是:http://msdn.microsoft.com/en-us/library/f591a55w.aspx

因为RichText框控件为"DetectUrls“提供了一些功能。然后你就可以很容易地处理被点击的链接了:

代码语言:javascript
复制
this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);

您可以通过扩展基类来简单地创建自己的RichTextBox控件-在基类中,您可以覆盖所需的方法,例如DetectUrls。

票数 10
EN

Stack Overflow用户

发布于 2014-11-06 23:43:23

你可以在这里找到一个通过linkLabel在富文本框中添加链接的例子:

代码语言:javascript
复制
    LinkLabel link = new LinkLabel();
    link.Text = "something";
    link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
    LinkLabel.Link data = new LinkLabel.Link();
    data.LinkData = @"C:\";
    link.Links.Add(data);
    link.AutoSize = true;
    link.Location =
        this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
    this.richTextBox1.Controls.Add(link);
    this.richTextBox1.AppendText(link.Text + "   ");
    this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;

下面是处理程序:

代码语言:javascript
复制
    private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
    }
票数 9
EN

Stack Overflow用户

发布于 2015-08-25 05:54:43

我找到了一种可能不是最优雅的方法,但它只需要几行代码就可以完成工作。也就是说,其思想是通过更改字体来模拟超链接外观,并通过检测鼠标指针所在位置来模拟超链接行为。

代码:

代码语言:javascript
复制
public partial class Form1 : Form
{
    private Cursor defaultRichTextBoxCursor = Cursors.Default;
    private const string HOT_TEXT = "click here";
    private bool mouseOnHotText = false;

    // ... Lines skipped (constructor, etc.)

    private void Form1_Load(object sender, EventArgs e)
    {
        // save the right cursor for later
        this.defaultRichTextBoxCursor = richTextBox1.Cursor;

        // Output some sample text, some of which contains
        // the trigger string (HOT_TEXT)
        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Underline);
        richTextBox1.SelectionColor = Color.Blue;
        // output "click here" with blue underlined font
        richTextBox1.SelectedText = HOT_TEXT + "\n";

        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Regular);
        richTextBox1.SelectionColor = Color.Black;
        richTextBox1.SelectedText = "Some regular text";
    }

    private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int mousePointerCharIndex = richTextBox1.GetCharIndexFromPosition(e.Location);
        int mousePointerLine = richTextBox1.GetLineFromCharIndex(mousePointerCharIndex);
        int firstCharIndexInMousePointerLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine);
        int firstCharIndexInNextLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine + 1);
        if (firstCharIndexInNextLine < 0)
        {
            firstCharIndexInNextLine = richTextBox1.Text.Length;
        }

        // See where the hyperlink starts, as long as it's on the same line
        // over which the mouse is
        int hotTextStartIndex = richTextBox1.Find(
            HOT_TEXT, firstCharIndexInMousePointerLine, firstCharIndexInNextLine, RichTextBoxFinds.NoHighlight);

        if (hotTextStartIndex >= 0 && 
            mousePointerCharIndex >= hotTextStartIndex && mousePointerCharIndex < hotTextStartIndex + HOT_TEXT.Length)
        {
            // Simulate hyperlink behavior
            richTextBox1.Cursor = Cursors.Hand;
            mouseOnHotText = true;
        }
        else
        {
            richTextBox1.Cursor = defaultRichTextBoxCursor;
            mouseOnHotText = false;
        }
        toolStripStatusLabel1.Text = mousePointerCharIndex.ToString();
    }

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && mouseOnHotText)
        {
            // Insert your own URL here, to navigate to when "hot text" is clicked
            Process.Start("http://www.google.com");
        }
    }
}

为了改进代码,可以创建一种优雅的方法来将多个“热门文本”字符串映射到它们自己的链接URL(可能是一个Dictionary<K, V> )。另一个改进是将RichTextBox子类封装在上面的代码中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9855292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档