RichTextBox without Image

One of my friend asked me if there is a way that could prevent Images being pasted in the RichTextBox control, so I did a little code that allows RichTextBox control not to include Images.
The solution to this problem is very simple just override the OnKeyDown event and check if the clipboard contains data of Bitmap Type.

public class RichTextBoxWithoutImage:RichTextBox

{

protected override void OnKeyDown(KeyEventArgs e)

{

base.OnKeyDown(e);

/*check for Ctrl+V*/

if (e.Control && e.KeyCode == Keys.V)

{

IDataObject obj = Clipboard.GetDataObject();

if (obj.GetDataPresent(typeof(Bitmap)))

{

e.Handled = true;

}

}

}

}

Advertisement

One Response

  1. Useful stuff brilliantly written. Thanx , it save my time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.