How to paste only valid value in textbox.
-
Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.
Handle the TextChanged event. Even better, use a NumericUpDown control.
Cheers, Vıkram.
"You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.
-
Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.
override WndProc, trap WM_PASTE and process there. Check out pinvoke.net.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
override WndProc, trap WM_PASTE and process there. Check out pinvoke.net.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.
If that's a web application or windows application . If it's a web application, then you can go about by Javascripts.
-
OK. This example creates a customized TextBox so add this class, build then drop onto a form.
public class RestrictedTextBox : TextBox { private const int WM\_PASTE = 0x0302; protected override void WndProc(ref Message m) { if (m.Msg == WM\_PASTE) { if (!IsPasteValid()) { m.Result = IntPtr.Zero; return; } } base.WndProc(ref m); } private bool IsPasteValid() { bool rtn = false; IDataObject obj = Clipboard.GetDataObject(); string pasteString = (string)obj.GetData(typeof(string)); // Do checks on pasteString here and set rtn to true if OK. return rtn; } }
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
If that's a web application or windows application . If it's a web application, then you can go about by Javascripts.
-
OK. This example creates a customized TextBox so add this class, build then drop onto a form.
public class RestrictedTextBox : TextBox { private const int WM\_PASTE = 0x0302; protected override void WndProc(ref Message m) { if (m.Msg == WM\_PASTE) { if (!IsPasteValid()) { m.Result = IntPtr.Zero; return; } } base.WndProc(ref m); } private bool IsPasteValid() { bool rtn = false; IDataObject obj = Clipboard.GetDataObject(); string pasteString = (string)obj.GetData(typeof(string)); // Do checks on pasteString here and set rtn to true if OK. return rtn; } }
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
I think, this piece of code will help you out...
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ( Convert.ToInt16(e.KeyChar) >= 48 && Convert.ToInt16(e.KeyChar) <=57)
{
e.KeyChar = e.KeyChar;
}
else
{
e.KeyChar = Convert.ToChar(0);
}
}private void textBox1\_TextChanged(object sender, EventArgs e) { char\[\] strSample = new char\[((System.Windows.Forms.TextBox)(sender)).Text.ToString().Length\]; strSample = (((System.Windows.Forms.TextBox)(sender)).Text).ToCharArray(); for (int i=0; i<strSample.Length;i++) { if (Convert.ToInt16(strSample\[i\]) >= 48 && Convert.ToInt16(strSample\[i\]) <= 57) { continue; } else { ((System.Windows.Forms.TextBox)(sender)).Text = ""; break; } } }
Try this and let me know if you have any concerns. Regards, Vengat P
-
I think, this piece of code will help you out...
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ( Convert.ToInt16(e.KeyChar) >= 48 && Convert.ToInt16(e.KeyChar) <=57)
{
e.KeyChar = e.KeyChar;
}
else
{
e.KeyChar = Convert.ToChar(0);
}
}private void textBox1\_TextChanged(object sender, EventArgs e) { char\[\] strSample = new char\[((System.Windows.Forms.TextBox)(sender)).Text.ToString().Length\]; strSample = (((System.Windows.Forms.TextBox)(sender)).Text).ToCharArray(); for (int i=0; i<strSample.Length;i++) { if (Convert.ToInt16(strSample\[i\]) >= 48 && Convert.ToInt16(strSample\[i\]) <= 57) { continue; } else { ((System.Windows.Forms.TextBox)(sender)).Text = ""; break; } } }
Try this and let me know if you have any concerns. Regards, Vengat P
-
Thanks Vengat, Everything is worked fine but if we paste characters then textbox goes blank. In that case we want current value of textbox. Then how to do that.
I am also new to windows application, but i'm sure that, you can bind the value of textbox again with the help of a global variable(that will be updated for each key press event and text change event). Sample: private string strTestVal = string.Empty; on keypress event: in if condition strTestVal = textBox1.Text.ToString(); on text change event: textBox1.Text = strTestVal;
Regards, Vengat P