472,800 Members | 1,101 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

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 3656
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mdb | last post by:
I'm trying to restrict the characters that are valid in a textBox, so I derived from System.Windows.Forms.TextBox, and overrode WndProc, catching the key up/down messages... Everything works...
3
by: volume | last post by:
Restricting a windows textbox (edit item) to digits only. Is there a windows option, using .NET C#, to only allow a user to enter digits ONLY? If so, what is the flag or setting? If no, what is...
2
by: W. Adam | last post by:
I have a form which displays info. On top of it there is a text box for input box which lets you put number from 1 thu 112.I need to verify that the number entered by the user is within the range...
1
by: Arne | last post by:
Hello ! I want to create entry widgets dynamically. var = i=0 for x in var: textbox = "t_", x textbox = entry(frame) textbox.grid(row=4+i, column=0) i = i + 1
2
by: hollykatong | last post by:
hello, is there a way to restricting a textbox input wherein it will only accept two integer with decimal places? for example if i enter 90 or 90.1234 it will allow me and when i try to type 890...
0
by: lagu2653 | last post by:
I've made a custom textbox with a certain background. In order to the textbox higher than normal I set the property multiline to true. The row above and below are showed since the textbox is higher...
0
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can...
2
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can...
2
by: =?Utf-8?B?R3JlZw==?= | last post by:
I'm from an Access background and I'm working with VB.Net. In MS Access I can restrict data-entry into my fields on a form using the Format property. Is there an equivilant in VB.Net. I want to...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.