2017-02-10 85 views
1

我做如何將上下文菜單添加到richTextBox控件?

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == System.Windows.Forms.MouseButtons.Right) 
    { 
     MessageBox.Show("you got it!"); 
    } 

} 

但我想要的是:

  1. 在做在RichTextBox的線右擊考慮行項目,以便菜單命令纔會生效的特定的行我沒有右鍵點擊。像刪除,粘貼,複製

  2. 如果我選擇粘貼,它會將新文本粘貼到richTextBox的底部(結尾)。但是,如果我點擊複製或刪除它會考慮到特定的行,我做了正確的點擊。

  3. 若要製作一行或一批行的粘貼,並將它們添加爲richTextBox底部(末尾)的行。

這就是我今天將文本作爲行添加到richTextBox。這些線是鏈接。 richTextBox中的每一行都是一個鏈接。而我想粘貼到richTextBox的鏈接不僅僅是文本。因此,我粘貼到richTextBox的每個鏈接都應該像我這樣做:for循環僅用於構造函數的第一次。

for (int i = 0; i < lines.Count; i++) 
      { 
       RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red, 8.25f); 
       richTextBox1.AppendText(lines[i] + (i < lines.Count - 1 ? Environment.NewLine : String.Empty)); 
      } 

      richTextBox1.AppendText(Environment.NewLine); 

      for (int i = 0; i < newList.Count; i++) 
      { 
       RichTextBoxExtensions.AppendText(richTextBox1, "Ready: ", Color.Red, 8.25f); 
       richTextBox1.AppendText(newList[i] + (i < newList.Count - 1 ? Environment.NewLine : String.Empty)); 
      } 

線和newList是列表

這只是如何我添加鏈接到RichTextBox的一個例子。 所以當我粘貼鏈接或鏈接時,他們應該以這種方式添加,就像我正在做的那樣。

這是怎樣的RichTextBox貌似現在例如:

Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101515&ir=true 

所以,如果我現在在做一個鏈接,例如膏:http://microsoft.com 現在RichTextBox的內容將是這樣的:

Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101330&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101345&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101400&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101415&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101430&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101445&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=true 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101500&ir=false 
Ready: http://www.sat24.com/image2.ashx?region=is&time=201702101515&ir=true 
Ready: http://www.microsoft.com 

如果我粘貼多個鏈接,那麼它會將鏈接添加到底部。

我覺得這是從剪貼板文本追加的最快方法:

string newText = Clipboard.GetText(); 
richTextBox1.SelectionStart = richTextBox1.TextLength; 
richTextBox1.SelectionLength = 0; 
richTextBox1.SelectedText = newText; 

但我想它被添加到年底的RichTextBox的底部和格式我對這個做準備好了:

在什麼情況下我應該這樣做?如何在代碼中添加上下文菜單並使用粘貼菜單?

更新

現在我想是這樣的:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == System.Windows.Forms.MouseButtons.Right) 
      { 
       var startIndex = richTextBox1.Text.IndexOf("Ready:") + "Ready:".Length; 
       var length = richTextBox1.Text.IndexOf("Ready:", startIndex) - startIndex; 

       int index = richTextBox1.SelectionStart; 
       int line = richTextBox1.GetLineFromCharIndex(index); 

       var code = richTextBox1.Text.Substring(startIndex + index, length - line - 1); 

       label1.Text = code; 
     } 

我嘗試添加了兩行:

int index = richTextBox1.SelectionStart; 
int line = richTextBox1.GetLineFromCharIndex(index); 

這兩條線我試圖讓鼠標cursour當我點擊一行時的位置。所以它會解析鼠標所在的行文本,就像listView中的項目一樣。

但子串我不正確。

如果我做這樣說:

private void richTextBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == System.Windows.Forms.MouseButtons.Right) 
      { 
       var startIndex = richTextBox1.Text.IndexOf("Ready:") + "Ready:".Length; 
       var length = richTextBox1.Text.IndexOf("Ready:", startIndex) - startIndex; 

       var code = richTextBox1.Text.Substring(startIndex, length - 1); 

       label1.Text = code; 
      } 
     } 

它會給我LABEL1總是第一線鏈接。 而不是鼠標光標位置點擊的那一行。 如果我在第7行單擊然後我想在第7行 的label1的全部文本,看看我就行65點擊然後在label1的看線的全部文本65

相同的思路ListView中如果我點擊物品。

+1

嘗試GetCharFromPosition提供鼠標的位置,然後GetLineFromCharIndex從該位置。 – LarsTech

回答

1

你的問題很簡單,但其後跟很多不相關的東西。我只是繼續嘗試回答開放問題,詢問如何將上下文菜單添加到richtextbox。

private void txtbx_text1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button != MouseButtons.Right) 
     { 
      return; 
     } 
     ContextMenu cm = new ContextMenu();//make a context menu instance 
     MenuItem item = new MenuItem();//make a menuitem instance 
     item.Text = "remove all";//give the item a header 
     item.Click += DoNothing;//give the item a click event handler 
     cm.MenuItems.Add(item);//add the item to the context menu 
     item = new MenuItem();//recycle the menu item 
     item.Text = "load from file";//give the item a header 
     item.Click += DoNothing;//give the item a click event handler 
     cm.MenuItems.Add(item);//add the item to the context menu 
     item = new MenuItem();//recycle item into a new menuitem 
     item.Text = "save list";//give the item a header 
     item.Click += DoNothing;//give the item a click event handler 
     cm.MenuItems.Add(item);//add the item to the context menu 
     ((RichTextBox)sender).ContextMenu = cm;//add the context menu to the sender 
     cm.Show(txtbx_text1, e.Location);//show the context menu 
    } 


    private void DoNothing(object sender, EventArgs e) 
    { 
     //doing nothing 
     return; 
    } 

一直在追求與您的其他需求,以下可能會讓你去。它需要一些愛,但前提是存在的,工作原理:利用

private void txtbx_text1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button != MouseButtons.Right) 
     { 
      return; 
     } 
     ContextMenu cm = new ContextMenu();//make a context menu instance 
     MenuItem item = new MenuItem();//make a menuitem instance 
     item.Text = "remove line";//give the item a header 
     item.Click += (sendingelement, eventargs) => RemoveLine(item, e);//give the item a click event handler 
     cm.MenuItems.Add(item);//add the item to the context menu 
     ((RichTextBox)sender).ContextMenu = cm;//add the context menu to the sender 
     cm.Show(txtbx_text1, e.Location);//show the context menu 
    } 

    private void RemoveLine(object sender, MouseEventArgs e) 
    { 
     if (txtbx_text1.Text.Length == 0) 
     { 
      return; 
     } 
     int charNextToCursor = txtbx_text1.GetCharIndexFromPosition(e.Location); 
     int lineNumFromChar = txtbx_text1.GetLineFromCharIndex(charNextToCursor); 
     int firstCharOfLine = txtbx_text1.GetFirstCharIndexFromLine(lineNumFromChar); 
     int lineLength = txtbx_text1.Lines[lineNumFromChar].Length; 
     string firstchar = txtbx_text1.Text.Substring(firstCharOfLine, 1); 
     //txtbx_text1.Text = txtbx_text1.Text.Remove(firstCharOfLine, lineLength); 
     if (lineNumFromChar == 0) 
     { 
      if (txtbx_text1.Lines.Length > 1) 
      { 
       txtbx_text1.Text = txtbx_text1.Text.Remove(firstCharOfLine, lineLength + 1); 
      } 
      else 
      { 
       txtbx_text1.Text = txtbx_text1.Text.Remove(firstCharOfLine, lineLength); 
      } 

     } 
     else 
     { 
      txtbx_text1.Text = txtbx_text1.Text.Remove(firstCharOfLine - 1, lineLength + 1); 
     } 

     ((MenuItem)sender).Parent.Dispose(); 

     return; 
    } 
+0

不知道這是做到這一點的正確方法,但我的答案的第二部分完成了你所要求的。它刪除創建上下文菜單的行,而不考慮插入位置。 –

+0

非常感謝。 –

相關問題