如何将上下文菜单添加到richTextBox控件?

时间:2017-02-10 15:53:50

标签: c# .net winforms

我做了

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底部(末尾)的行。

  4. 这就是我今天将文本作为行添加到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的底部,并且格式为我正在使用Ready:

    我应该在什么情况下这样做?如何在代码中添加上下文菜单并使用粘贴菜单?

    更新

    我现在尝试过这样的事情:

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

    当我点击一条线时,这两行我正试图获得鼠标的光标位置。因此,它将像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行,那么我想在label1中看到第7行的全文。 如果我点击第65行,那么在label1中查看第65行的全文。

    如果我点击项目,就像在listView中一样。

1 个答案:

答案 0 :(得分: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;
    }