c# textbox clipboard paste

C#verifica los caracteres en el portapapeles cuando los pega en el cuadro de texto



textbox clipboard (4)

Agregue la regla en el cambio de texto del cuadro de texto para aceptar el número solo como sigue:

private string value; private void textBox1_TextChanged(object sender, EventArgs e) { // at this moment value is still old var oldValue = value; if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]")) { MessageBox.Show("Please enter numbers only."); textBox1.Text = oldvalue; } else{ value = ((TextBox)sender).Text; } }

¿Hay alguna forma de verificar el carácter en el solo dígito del portapapeles antes de pegarlo en el cuadro de texto C # (Ctrl + V y clic derecho -> Pegar), que no está usando MarkedTextbox.


Creo ~ quieres un TextBox que solo puede aceptar dígitos?

Si es así, configure el estilo ES_NUMBER en el TextBox a través de SetWindowLong ():

public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load += Form2_Load; } private void Form1_Load(object sender, EventArgs e) { SetNumbersOnlyTextBox(this.textBox1); } public const int GWL_STYLE = (-16); public const int ES_NUMBER = 0x2000; [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern int GetWindowLong(IntPtr hWnd, int nIndex); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); public void SetNumbersOnlyTextBox(TextBox TB) { SetWindowLong(TB.Handle, GWL_STYLE, GetWindowLong(TB.Handle, GWL_STYLE) | ES_NUMBER); } }

Alternativamente, puede Heredar de TextBox y configurar ES_NUMBER en CreateParams ():

public class IntegerTextBox : TextBox { private const int ES_NUMBER = 0x2000; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style = cp.Style | ES_NUMBER; return cp; } } }


Dudo que de todos modos haya que verificar antes de pegar en TextBox, sugeriría suscribirse a los KeyDown de KeyDown y MouseClick y escribir su propia lógica.

protected override void OnKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) { // Your logic to read clipboard content and check length.; } } private void Form1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { // You logic goes here. } }

Puede obtener ayuda de MSDN sobre cómo leer / escribir el contenido del portapapeles.


Si, literalmente, quiere permitir únicamente las pasadas que solo tengan dígitos, hágalo de TextBox y atrape WM_PASTE, y suprima el mensaje cuando lo desee:

public class DigitsOnlyOnPasteTextBox : TextBox { private const int WM_PASTE = 0x302; protected override void WndProc(ref Message m) { if (m.Msg == WM_PASTE && Clipboard.ContainsText()) { int i; string txt = Clipboard.GetText(); foreach(char c in txt) { if (!char.IsNumber(c)) { return;// suppress default behavior } } } base.WndProc(ref m); // allow normal processing of the message } }