2017-02-10 13 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. 1行または1行の行のペーストを作成し、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が

リストされているこれは私がリッチテキストボックスへのリンクを追加している方法を一例に過ぎでした。 リンクやリンクの貼り付けをするときは、どうやってやっているかのように、この方法で追加する必要があります。

これは、リッチテキストボックスは、例えば、今どのように見えるかです:

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 を今すぐリッチテキストボックスの内容は次のようになります
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; 

しかし、私はそれが最後に追加されるように、リッチテキストボックスの下部たいとフォーマットに私はそれをやっていますレディ:

そして、どうすればいいですか?コード内にコンテキストメニューを追加し、貼り付けメニューを使用するにはどうしたらいいですか?

更新

は、私は今、このような何かを試してみました:

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

この二行私は、マウスのcursourを取得しようとしている:

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

私は2つの行を追加しようとしました私が線をクリックしたときの位置。したがって、マウスが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行目をクリックすると は、私が65

同じ考えListViewの場合のように行のテキスト全体を見て、その後LABEL1に65行をクリックした場合はライン7 のLABEL1テキスト全体で見てみたいです私は項目をクリックします。

+1

は、その位置からGetLineFromCharIndex続いマウスの位置を供給GetCharFromPositionを使用してみてください。 – LarsTech

答えて

1

あなたの質問は非常にまっすぐですが、それに続く無関係なものがたくさんあります。私はちょうど先に進み、リッチテキストボックスにコンテキストメニューを追加する方法を尋ねる最初の質問に答えようとしました。

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

それは正しい方法であるかどうか分かりませんが、私の答えの2番目の部分は、あなたが尋ねたことを行います。キャレットの位置に関係なく、コンテキストメニューが作成された行が削除されます。 –

+0

よろしくお願いいたします。 –

関連する問題