2012-03-24 7 views
17

私はrichtextboxesが(http://www.yahoo.comのような)リンクを検出できることを知っていますが、テキストのように見えるリンクを追加する方法はありますか?あなたはリンクのラベルをどこで選ぶことができるのか?ウィンドウを使用して忘れてしまった、イムはリッチテキストボックス内のリンク?

編集を形成:たとえばの代わりに、それはClick here to go to yahoo

編集として表示されますhttp://www.yahoo.comとして、それが表示される何か(フォーマットに簡単にのように)使用することをお勧めthatsのはありますか?

+0

自動です。 「www。」と入力するだけです。または "http://"とShazam!それはリンクです。コードでも動作します。 LinkClickedイベントを使用して、クリックを検出します。 –

+0

"http://として表示されているのではなく、ここをクリックしてください..." – Oztaco

+0

まあ、それはShazamだった!コメント。 EM_SETPARAFORMATは不幸に見えます。 –

答えて

5

それはあなたのコントロールにいくつかのWIN32の機能を呼び出すことによって可能ですが、あなたには、いくつかの標準的な方法を探しているなら、これはアウトポストチェック: Create hyperlink in TextBox control

を統合するさまざまな方法についていくつかの議論があります。

挨拶

アップデート1:リッチテキストボックスコントロールは、 "DetectUrls" にいくつかの機能を提供するため、 http://msdn.microsoft.com/en-us/library/f591a55w.aspx

: は、最善のことは、この方法に従うことです。そして、あなたは非常に簡単にクリックしたリンクを扱うことができます。

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

を、あなたは、単純な基本クラスを拡張することで、独自のリッチテキストボックスcontorlを作成することができます - そこにはDetectUrlsたとえば、あなたが必要なメソッドをオーバーライドすることができます。

0

標準のRichTextBoxコントロール(Windowsフォームを使用していると仮定しています)ではかなり限られた機能が公開されていますので、残念ながらSendMessage()、CFM_LINK、EM_SETCHARFORMATなどの行に沿ってWin32 interopを実行する必要があります。 )。

this answerでこれを行う方法の詳細はこちらをご覧ください。もちろん

6

ここにあなたがに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; 

そして、ここでは、ハンドラです:

private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
    { 
     System.Diagnostics.Process.Start(e.Link.LinkData.ToString()); 
    } 
0

私はほとんどないかもしれない方法を見つけましたエレガントですが、それはほんの数行のコードであり、仕事をしています。すなわち、フォントの変更によってハイパーリンクの外観をシミュレートし、マウスポインタが何であるかを検出することによってハイパーリンクの動作をシミュレートすることです。

コード:

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をサブクラス化して、上のコードにある機能をカプセル化することができます。

関連する問題