3

I am programming in c#. and I've a richTextBox on it.

At runtime, I insert some Bitmap images into richTextbox by coding. But I want to prevent user that drag my inserted Images or paste some other images in richTextBox.

How can I implement that?

thanks in advance!

Ali.M
  • 311
  • 6
  • 24
  • You can chane the richtextbox property to "readonly" but, you don't want to disable "writing/copy-paste features" for texts. Am I right? – Lost_In_Library Mar 01 '12 at 07:25
  • 2
    You should add your answer as an answer to your question and accept it. It will add closure to it. – Mark Hall Mar 01 '12 at 17:06
  • Please can you remove the answer from the question and add it as a new answer, then you can accept it. This will improve the usefulness of the question for other visitors – musefan Jan 29 '14 at 10:08
  • This doesn't look a duplicate to me. TheJonz answered something which I could use, not found in the "already answered" link. I guess both questions are a bit fuzzy in that the don't tell what should be done with casual text data. – Andreas Vergison Sep 12 '16 at 10:26

3 Answers3

2

If you want to just allow plain text pasted from the clipboard then you can do the following... (warning this replaces the clipboard with plain text)

private void textbox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.V)
    {
        Clipboard.SetText((string)Clipboard.GetData("Text"), TextDataFormat.Text);
    }
}

By doing this your RichTextBox will still allow undo and will work when it has some text highlight (neither one is supported in the answer from Zarathos located here: How to Make RichTextBox Text Only?)

Community
  • 1
  • 1
TheJonz
  • 394
  • 2
  • 11
1

Have you tried setting the AllowDrop Property to False to disable dropping. As far as preventing your content from being copy and pasted. You can do something like this SO question suggests by using ShortcutsEnabled to disable Shortcuts or just preventing pasting by making the control ReadOnly

Community
  • 1
  • 1
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Hi, Thanks for reply.updated question with a solution. I setted AllowDrop Property to false and it works.but about paste, I want to user allowed to paste texts. however I found out the solution. thanks. – Ali.M Mar 01 '12 at 08:26
0

I used this code and it works perfectly:

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        //if ((Control.ModifierKeys & Keys.Control) == Keys.Control && (e.KeyChar == 'V' || e.KeyChar == 'v'))
        if (((int)e.KeyChar) == 22)
        {
            if(hasImage(Clipboard.GetDataObject()));
            {
                e.Handled = true;
                richTextBox1.Undo();
                MessageBox.Show("can't paste image here!!");
            }


        }
    }

    private Boolean hasImage(object obj)
    {
        System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();
        Object data = Clipboard.GetDataObject();
        Clipboard.SetDataObject(data);
        richTextBox.Paste();
        Clipboard.SetDataObject(data);
        int offset = richTextBox.Rtf.IndexOf(@"\f0\fs17") + 8; // offset = 118;
        int len = richTextBox.Rtf.LastIndexOf(@"\par") - offset;
        return richTextBox.Rtf.Substring(offset, len).Trim().Contains(@"{\pict\");
    }

And disabled dragdrop in richTextBox.
thanks

Ali.M
  • 311
  • 6
  • 24
  • Why did you use `offset` and `len`? Why not using a simple `richTextBox.Rtf.Contains(@"{\pict\")` ? – 8oris Dec 13 '21 at 15:30