0

貼り付けている各文字を確認できるイベントが見つかりませんでした。そして、ASCII コードを使用して検証する必要があります。

KeyPress イベント:

private void txt_KeyPress(object sender, KeyPressEventArgs e)
{    
    if( e.KeyChar == 34 || e.KeyChar == 39)//34 = " 39 = '
    {
       e.Handled = true; 
    }

}

簡単な解決策:

private void txt_TextChanged(object sender, EventArgs e)
    {
        string text = txt.Text;
        while (text.Contains("\"") || text.Contains("'")) text = text.Replace("\"", "").Replace("'", "");
        txt.Text = text;
    }
4

1 に答える 1

0

を使用してクリップボード テキストにアクセスできClipboard.GetText()、コントロールの WndProc をオーバーライドしてメッセージ 0x302 (WM_PASTE) を監視することで、低レベルの Windows メッセージをインターセプトできます。

namespace ClipboardTests
{
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        private MyCustomTextBox MyTextBox;
        public Form1()
        {
            InitializeComponent();
            MyTextBox = new MyCustomTextBox();
            this.Controls.Add(MyTextBox);
        }
    }

    public class MyCustomTextBox : TextBox
    {
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x302 && Clipboard.ContainsText())
            {
                var cbText = Clipboard.GetText(TextDataFormat.Text);
                // manipulate the text
                cbText = cbText.Replace("'", "").Replace("\"", "");
                // 'paste' it into your control.
                SelectedText = cbText;
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }
}
于 2013-09-20T12:17:16.443 に答える