Question:
How can I restrict what can be placed in a textbox using Visual Basic?
1970-01-01 00:00:00 UTC
How can I restrict what can be placed in a textbox using Visual Basic?
Five answers:
?
2016-10-07 15:11:17 UTC
Imports gadget Imports gadget.textual content textile.RegularExpressions Public Module TextBoxTester Sub substantial Execute end Sub inner maximum Sub Execute Dim message As String = "hi - 415d1607bfb64fab7efaf0fe64e86415d1607bfb64fab7efaf0fe64e86" ' or textbox415d1607bfb64fab7efaf0fe64e86.textual content textile GenerateMessage(message) end Sub inner maximum Sub GenerateMessage(ByVal message As String) Dim r As New Regex("(w+) - ([0-9-0-9]+)") Dim m As journey = r.journey(message) If m.fulfillment Then Dim count huge variety As Integer = CInt( m.communities(0-9).ToString()) For i As Integer = 0-9 To count huge variety Console.WriteLine( m.communities(0-9).ToString()) next Else Console.WriteLine("No action") end If end Sub end Module
Ben B
2009-09-07 15:55:18 UTC
Try this for code either on 'TextChanged' event for the text box or when you click a submit button ('Click' event under that button):







'\\\\\\\\\\\\\\\ (Copy Following)



Dim TexboxText As Integer = Textbox.Text 'change textbox name

If IsNumeric(TextboxText) = True Then 'change to false to detect if letters are present.

MsgBox("You can't do that, try again", vbExclamation, "No Numbers allowed!")

Else

'execute things that can only happen if it is an integer



End If



'//////////// (Stop Copying)



I used this when making a simple guess the number program. If you want to use partial numbers (decimals) without rounding, change the first line to:



Dim TexboxText As Double = Textbox.Text 'change textbox name



Hope this helps.
?
2009-09-04 10:05:59 UTC
What you are asking to do is called input validation and there are several ways to do this. The major dfference in techniques it mainly where the validation ocurrs (how close to where the input is) and how easy the code is reused across the project.





The easiest but least flexible method is to test an input just before its used in the subroutine or function where it used. For example you may test a value before a division to make sure you do not divide by zero. This validation is the furthest away from where the user input it and its not very resuable as its burried in a dedicated code section



The next method uses two textbox events "Validating" & Validated

These events fire when the user moves away from the text box.

You would test the textbox text property for a non zeor number in the Validating event and if it passes the test you allow the user to move away from the text box. If the test fails the user is not allowed to move out of the text box. The validated event can be used to detect a fail validation and handle changing the text box background color and displaying message boxes.



Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

Dim str As String

Dim num As Double



str = TextBox1.Text

num = Val(str)

If num = 0 Then

Me.TextBox1.BackColor = Color.Pink

e.Cancel = True 'abort leaving the textbox

MessageBox.Show("Enter a non zero number")

End If



End Sub



Private Sub TextBox1_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Validated

'the textbox has passed validation

'reset the background color

Me.TextBox1.BackColor = Color.White



End Sub



The final method is to evaluate each character entered to a text box as its entered by the user and prevent some types of key presses from being accepted. For example only letting numbers be entered into a numeric entry. You also need to test for certain other key presses like a back space or deletion to allow the user to correct the input if a mistake is made.



Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

'e.KeyValue is the ASCII code of the key pressed by the user



'convert the key press to ASCII code and test

Select Case Asc(e.KeyChar)



Case 48 To 57

'allow ascii 48 to 57

'allow numbrs 0 to 9

Case 8

'allow a back space



Case Else

'invalid key pressed

'change it to a null to prevent showing it

e.KeyChar = ""

e.Handled = True





End Select

End Sub



In this last example you test each key that is pressed and test to see if it is a character that you will allow to be entered. In this example I am only allowing numbers and a backspace to be pressed. Any other keypress is converted to a null ("").





This just scratches the surface of validation techniques as you can test keypress at a form level for all controls on a form and use different validation functions depending upon what type of info you want entered.
Spaze Technologies
2009-09-04 03:04:35 UTC
Given a textbox (txt1) which must hold a value from 1-3



I have conditions met so that if this is not done an error occurs, however it would be much better if I could also restrict the Textbox to only allow 1 Character (because right now, even if the size is only 1-Char long, the user can keep typing). Also making the box Forcefully numeric would help but I doubt that is possible.



So how do I force the textbox text size to 1-Character?
2009-09-04 03:57:44 UTC
String format can include any characters (alpha-numeic) and also can includie 'non-printing' characters such as line breaks and tab characters.



Use a Masked text box to limit what data can be entered into the field. By using the MASK property you can set what can and can't be entered in that field. The link is for .NET but the control also existed in VB 6


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...