Restricting entry in textbox

  • Thread starter Thread starter Altramagnus
  • Start date Start date
A

Altramagnus

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.
 
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
 
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.
 
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.
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top