简体   繁体   English

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

[英]How can i add a context menu to a richTextBox control?

I did我做了

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

 }

But what i want is:但我想要的是:

  1. When doing a right click on a line in the richTextBox consider the line as item so the menu commands will take effect only for the specific line i did right click on.在 RichTextBox 中右键单击一行时,将该行视为项目,以便菜单命令仅对我右键单击的特定行生效。 Like delete,paste,copy像删除,粘贴,复制

  2. If i select paste it will paste the new text to the bottom(end) of the richTextBox.如果我选择粘贴,它会将新文本粘贴到 RichTextBox 的底部(末尾)。 But if i click on copy or delete it will consider it to the specific line i did the right click on.但是,如果我单击复制或删除,它会将其视为我右键单击的特定行。

  3. To make paste for one line or for batch of lines and add them as lines in the bottom(end) of the richTextBox.为一行或一批行进行粘贴,并将它们作为行添加到 RichTextBox 的底部(结束)中。

This is how i'm adding the text as lines today to the richTextBox.这就是我今天将文本作为行添加到richTextBox 的方式。 The lines are links.线条是链接。 Each line in the richTextBox is a link. richTextBox 中的每一行都是一个链接。 And what i want to paste to the richTextBox is only links not just text.我想粘贴到richTextBox 的只是链接,而不仅仅是文本。 So each link i paste to the richTextBox should be added like i'm doing it: The for loops is just for the constructor first time.所以我粘贴到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));
            }

lines and newList are list行和新列表是列表

This was just an example how i'm adding the links to the richTextBox.这只是我如何将链接添加到richTextBox 的示例。 So when i make paste of a link or link they should be added in this way like how i'm doing it.因此,当我粘贴链接或链接时,应该像我这样做一样以这种方式添加它们。

This is how the richTextBox looks like now for example:例如,这是 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

So if i'm doing now a paste of a link for example: http://microsoft.com Now the richTextBox content will look like:因此,如果我现在正在粘贴链接,例如: 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

And if i paste multiple links then it will add the link to the bottom.如果我粘贴多个链接,那么它会将链接添加到底部。

I think this is the fastest way to append text from clipboard:我认为这是从剪贴板附加文本的最快方法:

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

But i want it to be added to the end the bottom of the richTextBox and in the format i'm doing it with the Ready:但我希望将它添加到 RichTextBox 底部的末尾,并采用我正在使用 Ready 的格式:

And in what event should i do it ?在什么情况下我应该这样做? How do i add a context menu in code and using the paste menu ?如何在代码中添加上下文菜单并使用粘贴菜单?

Update更新

I tried something like this now:我现在尝试了这样的事情:

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

I tried to add the two lines:我尝试添加两行:

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

This two lines i'm trying to get the mouse cursour position when i click on a line.当我点击一条线时,这两条线我试图获得鼠标光标位置。 So it will parse the line text the mouse is on like item in a listView.所以它会像listView中的项目一样解析鼠标所在的行文本。

But the substring i not correct.但是子串我不正确。

If i'm doing it this way:如果我这样做:

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

It will give me in label1 always the first line link.它将始终在 label1 中给我第一行链接。 And not the line where the mouse cursour position is clicked on.而不是点击鼠标光标位置的那条线。 If i click on line 7 then i want to see in label1 the whole text of line 7. If i clicked on line 65 then in label1 to see the whole text of line 65.如果我点击第 7 行,那么我想在 label1 中看到第 7 行的整个文本。如果我点击第 65 行,然后在 label1 中查看第 65 行的整个文本。

Same idea as in listView if i click on item.如果我单击项目,则与 listView 中的想法相同。

Your question is pretty straight forward, but its followed by a lot of unrelated stuff.你的问题很直接,但后面有很多不相关的东西。 I just went ahead and tried to answer the opening question which asks how to add a context menu to a 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;
    }

Been toying with your other requirements and the following could get you going.一直在考虑您的其他要求,以下内容可以帮助您前进。 It needs some love, but the premise is there and works:它需要一些爱,但前提是存在并且有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM