0

I didnt find an event that I can verify each char that I'm pasting. And I need verify using ASCII code 'cause I want to handle ' and "

KeyPress Event:

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

}

Simple Solution:

private void txt_TextChanged(object sender, EventArgs e)
    {
        string text = txt.Text;
        while (text.Contains("\"") || text.Contains("'")) text = text.Replace("\"", "").Replace("'", "");
        txt.Text = text;
    }
  • What is your imput control? Most likely you need: `textBox.TextChanged += eventhandler;` – MrFox Sep 20 '13 at 11:57
  • @hanspassant It isn't solve my problem, 'cause I'm trying handle this char ' so, I can't use string or char to replace, I need to use ascii code. And what event I'll use? – Felipe Moura Sep 20 '13 at 12:09
  • What makes you think you paste a *character* from the clipboard? You paste a string. Do with it whatever you want. – Hans Passant Sep 20 '13 at 12:23
  • If I paste this example: 'hello world' ..I want to replace: hello world without ' – Felipe Moura Sep 20 '13 at 12:35

1 Answers1

0

You can access clipboard text using Clipboard.GetText() and you can intercept the low-level Windows message by overriding the control's WndProc and watching for the message 0x302 (WM_PASTE).

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);
            }
        }
    }
}
Henrique Miranda
  • 1,030
  • 1
  • 13
  • 31
  • I've tried use, but it didn't work: string text = txt.Text; if (text.Contains(Convert.ToChar(34))) text.Replace(Convert.ToChar(34), ' '); if (text.Contains(Convert.ToChar(39))) text.Replace(Convert.ToChar(39), ' '); – Felipe Moura Sep 20 '13 at 12:38
  • Oh, you want to remove single and double quotes from the clipboard text. please see my edited answer. – Henrique Miranda Sep 20 '13 at 12:48
  • I didn't get it what is to do..private void TEXTBOX1_TextChanged(object sender, EventArgs e) { WndProc(???) } protected override void WndProc(ref Message m) { if (m.Msg == 0x302 && Clipboard.ContainsText()) { var cbText = Clipboard.GetText(TextDataFormat.Text); cbText = cbText.Replace("'", "").Replace("\"", ""); //this.SelectedText = cbText; <- this = form1?? TEXTBOX1.SelectedText = cbText; <- TEXTBOX1 just for test. } base.WndProc(ref m); } – Felipe Moura Sep 20 '13 at 13:16
  • You must create a custom control that inherits from the ``TextBox`` control. I'll update my answer with a more complete example. – Henrique Miranda Sep 20 '13 at 13:44
  • Assuming my answer solved your problem, can you please accept it? – Henrique Miranda Oct 04 '13 at 18:02