Links inside rich textbox?

2020-02-26 05:01发布

I know that richtextboxes can detect links (like http://www.yahoo.com) but is there a way for me to add links to it that looks like text but its a link? Like where you can choose the label of the link? For example instead of it appearing as http://www.yahoo.com it appears as Click here to go to yahoo

edit: forgot, im using windows forms

edit: is there something thats better to use (as in easier to format)?

4条回答
▲ chillily
2楼-- · 2020-02-26 05:15

Here you can find an example of adding a link in rich Textbox by linkLabel:

    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;

And here is the handler:

    private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
    }
查看更多
该账号已被封号
3楼-- · 2020-02-26 05:23

The standard RichTextBox control (assuming you are using Windows Forms) exposes a rather limited set of features, so unfortunately you will need to do some Win32 interop to achieve that (along the lines of SendMessage(), CFM_LINK, EM_SETCHARFORMAT etc.).

You can find more information on how to do that in this answer here on SO.

查看更多
姐就是有狂的资本
4楼-- · 2020-02-26 05:24

Of course it is possible by invoking some WIN32 functionality into your control, but if you are looking for some standard ways, check this post out: Create hyperlink in TextBox control

There are some discussions about different ways of integration.

greetings

Update 1: The best thing is to follow this method: http://msdn.microsoft.com/en-us/library/f591a55w.aspx

because the RichText box controls provides some functionality to "DetectUrls". Then you can handle the clicked links very easy:

this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);

and you can simple create your own RichTextBox contorl by extending the base class - there you can override the methods you need, for example the DetectUrls.

查看更多
我只想做你的唯一
5楼-- · 2020-02-26 05:25

I found a way which may not be the most elegant, but it's just a few lines of code and does the job. Namely, the idea is to simulate hyperlink appearance by means of font changes, and simulate hyperlink behavior by detecting what the mouse pointer is on.

The code:

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");
        }
    }
}

To improve on the code, one could create an elegant way to map multiple "hot text" strings to their own linked URLs (a Dictionary<K, V> maybe). An additional improvement would be to subclass RichTextBox to encapsulate the functionality that's in the code above.

查看更多
登录 后发表回答