Enable copy, cut, past window in a rich text box

前端 未结 7 1367
礼貌的吻别
礼貌的吻别 2021-01-31 09:30

I have a rich text box(richTextBox1) in my program as shown bellow. But when I right click on it, it doesn\'t pop up a “copy, cut, past” window. Can you please tell

7条回答
  •  离开以前
    2021-01-31 09:31

    I just want to add to Thilina H's answer (the one that was marked as the correct answer by the poster) Here is my copy and paste functions, They are a bit more notepad like.

    void CopyAction(object sender, EventArgs e)
    {
        if (richTextBox1.SelectedText != null && richTextBox1.SelectedText != "")
        {
            Clipboard.SetText(richTextBox1.SelectedText);
        }
    }
    
    void PasteAction(object sender, EventArgs e)
    {
        if (Clipboard.ContainsText())
        {
            int selstart = richTextBox1.SelectionStart;
            if (richTextBox1.SelectedText != null && richTextBox1.SelectedText != "")
            {
                richTextBox1.Text = richTextBox1.Text.Remove(selstart, richTextBox1.SelectionLength);
            }
    
            string clip = Clipboard.GetText(TextDataFormat.Text).ToString();
            richTextBox1.Text = richTextBox1.Text.Insert(selstart, clip);
            richTextBox1.SelectionStart = selstart + clip.Length;
        }
    }
    

    I hope it helps someone ;

提交回复
热议问题