Reputation: 8630
I'm new to Custom Controls and I'm looking for some help.
I want to know if it is possible to add validation on an event such as a "Key_Press" within my Custom Class rather than through an Event in my form code. I aim to block the use of the Return & Enter keys for the control.
I have created a custom RichTextBox, code below :-
public class CustomRTB : RichTextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == (Keys.Control | Keys.V)))
{
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
string contents = Clipboard.GetText().Replace("\r\n", " ");
Clipboard.SetData(DataFormats.Text, contents);
this.Paste();
}
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
Upvotes: 0
Views: 1601
Reputation: 941337
Block the Enter key by simply overriding the OnKeyDown() method. An example of a plain KeyDown event that works for any RTB:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.Enter) e.Handled = e.SuppressKeyPress = true;
}
Upvotes: 2