469,638 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,638 developers. It's quick & easy.

Restricting entry in textbox

I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.

The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.

How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.

Thanks.

Nov 17 '05 #1
6 3517
Use Javascript at Client side
use onblur property of text box
"Altramagnus" <al*********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.

The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.

How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.

Thanks.

Nov 17 '05 #2
You could check for the correct entry in the .leave event.

I made a custom textbox and added things like text only, text and numeric,
integer and decimal.

Regards,
Brian

"Altramagnus" <al*********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.

The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.

How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.

Thanks.

Nov 17 '05 #3
Hi Altramagnus,

The first case is easy

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
e.Handled = true;
}

For the second part, use a storage variable to keep track of the last content of the TextBox

string tb = "1234";
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach(Char c in textBox1.Text)
if(!Char.IsDigit(c))
{
// calculate the cursor position
// change the Text
// set the cursor position
int i = textBox1.SelectionStart -
(textBox1.TextLength - tb.Length);
textBox1.Text = tb;
textBox1.SelectionStart = i;
break;
}
tb = textBox1.Text;
}

A neater way would be to simply call textBox1.Undo(), but if the user then tried again, it would undo the last operation by pasting the previous illegal text and a nice loop and stack overflow would result.

On Mon, 03 Oct 2005 12:10:47 +0200, Altramagnus <al*********@hotmail.com> wrote:
I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.

The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.

How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.

Thanks.


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4
Thanks.

However, in normal environment it works, however, in an environment
which is slow, or constantly heavy CPU usage, i could the text
flicker back to the original when pasting illegal text.

Is there any way to detect what is the text pasted before the text is
changed,
rather than allow the text changed, and then revert later if illegal.

Thanks.

Nov 17 '05 #5
A way to prevent Paste (or verify it before allowing it) is to
override ProcessCmdKey and check for Shift+Ins or Ctrl+V
(standard paste shortcuts)

Like this:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
' We need to handle the paste command to prevent illegal characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control
Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject()
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String = CStr(data.GetData(DataFormats.StringFormat,
True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
' TODO: Verify that the text only contains valid characters
' and return True if it doesn't. Otherwise call the base
class
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function

I don't see a need to limit cut and copy since those commands doesn't put
anything into the textbox.
For normal keyboard typing, KeyPress is the way to go.

/claes

"Altramagnus" <al*********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
I have searched throught the newsgroups
on how to restrict entry in textboxes, for example,
I only want the textbox to only accept numberic.

The standard answer is to use the KeyPress event.
However, this do not address the paste event
User is still able to copy and paste any characters.

How do I create a textbox so that regardless what I do ( including cut
and paste ),
the result must only contain certain prefdefined chars.

Thanks.

Nov 17 '05 #6
Thanks.

I have got almost the same solution.
Difference is instead of ProcessCmdKey, I overrode the WndProc and
check for
WM_PASTE message. If WM_PASTE, then I did exactly the same as what you
have suggested,
checking the clipboard. And also, instead of handling KeyPress event, I
checked
for WM_CHAR message. My guess is WndProc will be called first b4
KeyPress event.

Regards,
Altramagnus

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by mdb | last post: by
1 post views Thread by Arne | last post: by
2 posts views Thread by =?Utf-8?B?R3JlZw==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.