... new solution ...
To both prevent the user entering those characters at run-time, and catch the 'Paste event and remove those characters, you can use a UserControl:
using System.Text;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class NoQuoteTextBox : TextBox
{
private const int WM_PASTE = 0x0302;
private StringBuilder sb;
public NoQuoteTextBox()
{
InitializeComponent();
}
protected override void WndProc(ref Message msg)
{
if (msg.Msg != WM_PASTE)
{
base.WndProc(ref msg);
}
else
{
sb = new StringBuilder(Clipboard.GetText());
if (sb.Length > 0)
{
sb.Replace("\'", string.Empty);
sb.Replace("\"", string.Empty);
if (sb.Length > 0)
{
Clipboard.SetText(sb.ToString());
base.WndProc(ref msg);
}
}
}
}
private void NoQuoteTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = e.KeyChar == '\'' || e.KeyChar == '\"';
}
}
}
... previous solution ...
Why not prevent those characters from being entered ?
private void SomeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\'' || e.KeyChar == '\"')
{
e.Handled = true;
}
}