Loop through empty textbox until the textbox has data - vb.net

I'm sure there's a simple solution on this, but it escapes me. I have a form with three text boxes on it. Before the main code is run I want to make sure each text box has data in it. I've initialized "hasData" to be the variable that will decide if the code can move on or not. I evaluate hasData in a Do While loop but the code discovers there are text boxes without data and set hasData variable to "False". But then I'm in a continuous loop, the message box never goes away to allow you to enter text into the empty text boxes.
Thank you for your help.
Dim hasData As String
hasData = "False"
Do While hasData = "False"
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
btnExecute.Enabled = False
hasData = "False"
MsgBox(" Please fill all text boxes on form ")
' this resulted in an endless loop of the msgBox. It didn't let me add text to the empty fields
Else
btnExecute.Enabled = True
hasData = "True"
End If
Loop
If (hasData = "True") Then
searchDir = txtDirectory.Text
Prefix = txtBxUnique.Text
Dim manualName = txtTechManName.Text

Do you see how you had to list the TextBoxes twice? The list of controls is the same in both cases, so why do double work?
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
' ...
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
' ...
If you ever wanted to add a TextBox to the logic, then you'd have to remember to update the code in two places. You may even have more than two places...
Only list the TextBoxes once in some kind of IEnumerable collection.
Use LINQ to help you with some of the logic.
Use methods to encapsulate the logic, so your program flow is smoother and easier to understand.
' store the TextBoxes in a collection
Private ReadOnly textBoxes As New List(Of TextBox)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add the TextBoxes which should be filled to the collection
textBoxes.AddRange({txtTechManName, txtDirectory, txtBxUnique})
End Sub
Private Function hasData() As Boolean
' ![any TextBox is invalid] == all TextBoxes are valid
Return Not textBoxes.Any(AddressOf textBoxIsInvalid)
End Function
Private Function lastInvalidTextBox() As TextBox
' last TextBox with invalid entry, or default (null)
Return textBoxes.LastOrDefault(AddressOf textBoxIsInvalid)
End Function
' returns true when passed TextBox is invalid
Private Function textBoxIsInvalid(tb As TextBox) As Boolean
' define invalid here, in once place only
Return String.IsNullOrWhiteSpace(tb.Text)
End Function
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
' simply checks the hasData function which hides the logic
If hasData() Then
runProgram()
Else
MessageBox.Show("Please fill all text boxes on form")
' focus the last invalid TextBox, effectively the same as your original logic
' if it is null (no invalid TextBox, see lastInvalidTextBox()), isn't called
lastInvalidTextBox()?.Focus()
End If
End Sub

I moved my evaluating condition to the execute buttons on click event. There I run an if statement and when hasData is true run the function with the rest of the code.
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim hasData As Boolean = False
If txtTechManName.Text.Trim = "" Or txtDirectory.Text.Trim = "" Or txtBxUnique.Text.Trim = "" Then
hasData = False
MsgBox(" Please fill all text boxes on form ")
If txtTechManName.Text.Trim = "" Then
txtTechManName.Focus()
ElseIf txtDirectory.Text.Trim = "" Then
txtDirectory.Focus()
ElseIf txtBxUnique.Text.Trim = "" Then
txtBxUnique.Focus()
End If
Else
hasData = True
runProgram()
End If
End Sub

Related

Creating a confirmation which switches back to previous radio button if user cancels

So I currently have a form where I have 4 teams, each with 4 teammates. This cannot change.
my form GUI
Initially when the program starts, it automatically selects the "current value" radio button which displays labels with blank data, you can then select "edit values" which can be saved by pressing the big green apply button. This stores the data under a 2d array named stTeamName, which the labels reference whenever you select "current value" again.
There are 4 radio buttons which allow you to switch between the teams.
My problem is, when I change the radio buttons, any data put into the text labels that has not been saved will be deleted.
I solved this problem by adding a message box which will stop the data in the labels from changing unless the user gives confirmation to do so.
However, my only problem is that after I cancel this, the radio button stays at the current selection which does not help.
I solved this issue by using rbTeamName1.Checked = True (rbTeamname 1 to 4 being the radio buttons named team 1-4) to switch back to the previous state.
This of course only goes back to rbTeamname1 no matter what, so I employed an 1d array and select case to help solve this issue.
"""
Dim RadioCounter(1) As Integer
the code below simply gives the default Radio button value on load.`
Sub DeclareRadio(RadioButton)
RadioButton(1) = 1
End Sub
"""
Whenever a radio button is successfully selected, RadioCounter(1) is set to the number 1-4 respectively. Whenever a new radio button is selected, the value of RadioCounter(1) is moved to RadioCounter(0) and the current radio button's value is to RadioCounter(1).
If the user selects no, RadioCounter(0)'s value is referenced and a select case selects the radio button.
(Please note, RadioTeam() is used as a counter to store Team name and Teammate name string variables correctly within the 2d array: stTeamName. I will most likely merge this with RadioCounter as soon as I get this problem fixed)
"""
Private Sub rbTeamName1_CheckedChanged(sender As Object, e As EventArgs) Handles rbTeamName1.CheckedChanged
'note txtTeamName starts at 0, however RadioTeam starts at 1, RadioTeam = 0 has no team designated to it.
'this function is for swapping teams via radio button, each time, a confirmation is given to make sure the user saves the current team, or risk deletion of the inputted data.
RadioCounter(1) = RadioCounter(0)
stTeamname(0, RadioTeam) = txtTeamName.Text
stTeamname(1, RadioTeam) = txtMate1.Text
stTeamname(2, RadioTeam) = txtMate2.Text
stTeamname(3, RadioTeam) = txtMate3.Text
stTeamname(4, RadioTeam) = txtMate4.Text
Dim Dialog As DialogResult
Dialog = MessageBox.Show("Are you sure you want to change team? Any unsaved changes will be lost. If this box shows on starting the program, just press no", "Change team?", MessageBoxButtons.YesNo)
If Dialog = DialogResult.No Then
'need to run function to check previous result of radio button'
Select Case RadioCounter(0)
Case 1
rbTeamName1.Checked = True
Case 2
rbTeamName2.Checked = True
Case 3
rbTeamName3.Checked = True
Case 4
rbTeamName4.Checked = True
End Select
ElseIf Dialog = DialogResult.Yes Then
RadioCounter(1) = 1 'this equals the respective number for each radio button'
RadioTeam = 1
txtTeamName.Text = stTeamname(0, RadioTeam)
txtMate1.Text = stTeamname(1, RadioTeam)
txtMate2.Text = stTeamname(2, RadioTeam)
txtMate3.Text = stTeamname(3, RadioTeam)
txtMate4.Text = stTeamname(4, RadioTeam)
lblTeamName.Text = stTeamname(0, RadioTeam)
lblMate1.Text = stTeamname(1, RadioTeam)
lblMate2.Text = stTeamname(2, RadioTeam)
lblMate3.Text = stTeamname(3, RadioTeam)
lblMate4.Text = stTeamname(4, RadioTeam)
End If
End Sub
"""
....Or it should do that. But it doesnt. Im not sure why, but when I force it to check a specific radio button, by removing the select case and only running rbTeamName4.Checked = True it will work perfectly fine, theres something about the select case that simply doesnt run the code.
Also, despite stTeamname(0, RadioTeam) = txtTeamName.Text etc. being given before the messagebox shows, if I do not press the apply button to save, press another team button, then press no, it will still not save those parameters, which I find extremely weird.
What is the ideal solution to this?
To clarify:
If a user writes team names and teammates and does not press the apply button to save and then changes team by selecting another radio button, a text box should appear asking whether they want to switch team and lose the data (Yes) or stay on the previous radio button to edit and apply the data (No).
Upon pressing no, the program automatically switches the current radio button to the previous selection to make it seem as if no radio button was selected in the first place.
And im not sure if I have to make it obvious but im new to both stackoverflow and VB.net in general. Thank you in advance.
Essentially what you are asking for is dirty form checking. The basic principle is that you need to compare the value to what it was previously.
So for example:
When the TextBox values change, set a form level boolean variable to true
When you go to switch teams or current/edit you would check if that form level boolean variable is true
If the variable is true then you would display the prompt, otherwise just do the action
After the action, reset the global level boolean variable
There are some caveats, for example, when you toggle between current/edit the check changed will fire twice: once for the current radio button and once for the edit. But you just need to work around those edge cases.
Here is a largely untested example for you to go off of:
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public Class Form1
Private _team1 As Team
Private _team2 As Team
Private _team3 As Team
Private _team4 As Team
Private _isFormDirty As Boolean = False
Private _selectedTeamRadioButton = RadioButtonTeam1
Private Sub RadioButtonCurrent_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButtonCurrent.CheckedChanged
If (RadioButtonCurrent.Checked AndAlso _isFormDirty AndAlso MessageBox.Show("By doing this, you will lose all unsaved changes. Are you sure?", "Dirty Form", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No) Then
RadioButtonEdit.Checked = True
Return
End If
If (Not RadioButtonCurrent.Checked) Then
Return
End If
TextBoxTeamName.Enabled = False
TextBoxTeammate1.Enabled = False
TextBoxTeammate2.Enabled = False
TextBoxTeammate3.Enabled = False
TextBoxTeammate4.Enabled = False
ResetTeam()
End Sub
Private Sub RadioButtonEdit_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButtonEdit.CheckedChanged
TextBoxTeamName.Enabled = True
TextBoxTeammate1.Enabled = True
TextBoxTeammate2.Enabled = True
TextBoxTeammate3.Enabled = True
TextBoxTeammate4.Enabled = True
End Sub
Private Sub RadioButtonTeamChanged(sender As Object, e As EventArgs) Handles RadioButtonTeam1.CheckedChanged, RadioButtonTeam2.CheckedChanged, RadioButtonTeam3.CheckedChanged, RadioButtonTeam4.CheckedChanged
If (_selectedTeamRadioButton IsNot DirectCast(sender, RadioButton) AndAlso _isFormDirty AndAlso MessageBox.Show("By doing this, you will lose all unsaved changes. Are you sure?", "Dirty Form", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No) Then
_selectedTeamRadioButton.Checked = True
Return
End If
If (_selectedTeamRadioButton IsNot DirectCast(sender, RadioButton)) Then
Return
End If
_selectedTeamRadioButton = DirectCast(sender, RadioButton)
ResetTeam()
End Sub
Private Sub TextBoxTeamValueChanged(sender As Object, e As EventArgs) Handles TextBoxTeamName.TextChanged, TextBoxTeammate1.TextAlignChanged, TextBoxTeammate2.TextAlignChanged, TextBoxTeammate3.TextAlignChanged, TextBoxTeammate4.TextAlignChanged
_isFormDirty = True
End Sub
Private Sub ButtonApply_Click(sender As Object, e As EventArgs) Handles ButtonApply.Click
Select Case True
Case RadioButtonTeam1.Checked
_team1 = New Team() With {
.Name = TextBoxTeamName.Text,
.Teammate1 = TextBoxTeammate1.Text,
.Teammate2 = TextBoxTeammate2.Text,
.Teammate3 = TextBoxTeammate3.Text,
.Teammate4 = TextBoxTeammate4.Text
}
Case RadioButtonTeam2.Checked
_team2 = New Team() With {
.Name = TextBoxTeamName.Text,
.Teammate1 = TextBoxTeammate1.Text,
.Teammate2 = TextBoxTeammate2.Text,
.Teammate3 = TextBoxTeammate3.Text,
.Teammate4 = TextBoxTeammate4.Text
}
Case RadioButtonTeam3.Checked
_team3 = New Team() With {
.Name = TextBoxTeamName.Text,
.Teammate1 = TextBoxTeammate1.Text,
.Teammate2 = TextBoxTeammate2.Text,
.Teammate3 = TextBoxTeammate3.Text,
.Teammate4 = TextBoxTeammate4.Text
}
Case RadioButtonTeam4.Checked
_team4 = New Team() With {
.Name = TextBoxTeamName.Text,
.Teammate1 = TextBoxTeammate1.Text,
.Teammate2 = TextBoxTeammate2.Text,
.Teammate3 = TextBoxTeammate3.Text,
.Teammate4 = TextBoxTeammate4.Text
}
End Select
_isFormDirty = False
End Sub
Private Sub ButtonDeleteTeam_Click(sender As Object, e As EventArgs) Handles ButtonDeleteTeam.Click
If (MessageBox.Show("Are you sure you want to delete this team?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No) Then
Return
End If
_isFormDirty = False
Select Case True
Case RadioButtonTeam1.Checked
_team1 = New Team()
Case RadioButtonTeam2.Checked
_team2 = New Team()
Case RadioButtonTeam3.Checked
_team3 = New Team()
Case RadioButtonTeam4.Checked
_team4 = New Team()
End Select
ResetTeam()
End Sub
Private Sub ResetTeam()
Dim selectedTeam As Team = Nothing
Select Case True
Case RadioButtonTeam1.Checked
selectedTeam = _team1
Case RadioButtonTeam2.Checked
selectedTeam = _team2
Case RadioButtonTeam3.Checked
selectedTeam = _team3
Case RadioButtonTeam4.Checked
selectedTeam = _team4
End Select
If (selectedTeam IsNot Nothing) Then
TextBoxTeamName.Text = selectedTeam.Name
TextBoxTeammate1.Text = selectedTeam.Teammate1
TextBoxTeammate2.Text = selectedTeam.Teammate2
TextBoxTeammate3.Text = selectedTeam.Teammate3
TextBoxTeammate4.Text = selectedTeam.Teammate4
Else
TextBoxTeamName.Clear()
TextBoxTeammate1.Clear()
TextBoxTeammate2.Clear()
TextBoxTeammate3.Clear()
TextBoxTeammate4.Clear()
End If
End Sub
End Class
Public Class Team
Implements INotifyPropertyChanged
Private _name As String
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
If (_name <> value) Then
_name = value
NotifyPropertyChanged()
End If
End Set
End Property
Private _teammate1 As String
Public Property Teammate1 As String
Get
Return _teammate1
End Get
Set(value As String)
If (_teammate1 <> value) Then
_teammate1 = value
NotifyPropertyChanged()
End If
End Set
End Property
Private _teammate2 As String
Public Property Teammate2 As String
Get
Return _teammate2
End Get
Set(value As String)
If (_teammate2 <> value) Then
_teammate2 = value
NotifyPropertyChanged()
End If
End Set
End Property
Private _teammate3 As String
Public Property Teammate3 As String
Get
Return _teammate3
End Get
Set(value As String)
If (_teammate3 <> value) Then
_teammate3 = value
NotifyPropertyChanged()
End If
End Set
End Property
Private _teammate4 As String
Public Property Teammate4 As String
Get
Return _teammate4
End Get
Set(value As String)
If (_teammate4 <> value) Then
_teammate4 = value
NotifyPropertyChanged()
End If
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class

AutoComplete Textbox to detect if text is typed from user or appended from AutoComplete collection?

I have created an autocomplete textbox with mode of SuggestAndAppend text. I would like to detect if the text in the textbox is newly typed by user, or it is just appended from the Source Collection?
it can be checked when the textbox loose focus, but is there another way to detect immediately as the focus still in the textbox?
any idea?
for the time being, I could write a code to implement the task. right now, this code can detect if the newly typed text is not a part of any items in the collection. but what if the user typed a newly text which can be considered as a part of an entry in the collection? i.e what if the collection contains entries like : BBC, CNN, FOX New and user wants to type only CN ( Cartoon Network)..in this case "CN" will be a part of CNN and then the code will not detect it as a new entry.
Private Sub TextBox1_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim isNew As Boolean = True
For i As Integer = 0 To TextBox1.AutoCompleteCustomSource.Count - 1
If UCase(Trim(TextBox1.AutoCompleteCustomSource(i))) Like UCase(Trim(TextBox1.Text)) & "*" Then
isNew = False
Exit For
End If
Next
If isNew = True Then
MsgBox("Custome")
Else
End If
End Sub
the below code can chick if the text in textbox is new to collection or no, for the time being, it catch it in Leave Event. it should be improved to catch [Enter] key as well
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Dim isNew As Boolean = True
For i As Integer = 0 To TextBox1.AutoCompleteCustomSource.Count - 1
If TextBox1.AutoCompleteCustomSource.Contains(UCase(Trim(TextBox1.Text))) Then
isNew = False
End If
Next
If isNew = True Then
MsgBox("Custome")
Else
End If
End Sub

Deselect combobox selected value

First of all, I apologize to the community because the title may seem a bit 'misleading from what actually is the real problem.
I have made a code that populates two combobox with the same values, with each change that occurs on the combobox I perform a check to see if the selected value is equal to another combobox; if it is equal, then execute a particular code otherwise proceed.
I need to implement a de-selection of value and I did it with:
ComboBox1.SelectedIndex = -1
Combobox2.SelectedIndex = -1
This works fine, but the code that checks if the values are equal interfere with this operation.
In fact, within each combobox I have something like this:
Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
...
where "off ()" is the function that then doesn't continue what you're doing.
How do I solve this problem?
You'll have to disable the event when resetting the combobox. This can be done by removing the event with RemoveHandler/AddHandler and adding it again.
An other option is to use a flag. (This is just an example to show an idea, the flag variable should be properly placed).
Private FreezeEventFlag As Boolean = False ' or True, it depends..
' Declare it toplevel, initialize its value depending on
' the way you're going to initialize your Comboboxes selected values.
' Then set it's value to True each time you want to
' disable the event handling in any code block that resets
' SelectedIndexes to -1 like below :
' ...
FreezeEventFlag = True
ComboBox1.SelectedIndex = -1
Combobox2.SelectedIndex = -1
FreezeEventFlag = False ' Don't forget to set the Flag to false !
Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If FreezeEventFlag Then Exit Sub ' Just ignore this event this time.
If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
End Sub

limit the range of characters the user can put into a textbox vb.net

I have a textbox in a vb form and I want to limit the range of characters that the user can put into the textbox to:" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*().". The textbox is to insert SI Units into a database so i need consistent syntax. If the user types an invalid character into the textbox I would like the textbox to refuse to insert it, or remove it straight away, leaving the cursor in the same position within the textbox. I would also like the textbox to replace "/" with "^(-" and place the cursor before this.
I have found some code elsewhere which I have edited to do this but the code is bad, it activates on text changed within the textbox. This causes the code to fail, when the user inputs a disallowed value the code it activates itself when it tries to changes the text within the textbox.
Here is my code, the textbox starts with the contents "enter SI Units" from the form designer.
Private Sub TxtQuantityTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSIUnit.TextChanged
If txtSIUnit.Text = "Enter SI Units" Then
Exit Sub
End If
Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
Dim Text As String = txtSIUnit.Text
Dim Letter As String
Dim SelectionIndex As Integer = txtSIUnit.SelectionStart
Dim Change As Integer
Letter = txtSIUnit.Text.Substring(SelectionIndex - 1, 1)
If Letter = "/" Then
Text = Text.Replace(Letter, "^(-")
SelectionIndex = SelectionIndex - 1
End If
Letter = txtSIUnit.Text.Substring(SelectionIndex - 1, 1)
If charactersAllowed.Contains(Letter) = False Then
Text = Text.Replace(Letter, String.Empty)
Change = 1
End If
txtSIUnit.Text = Text
txtSIUnit.Select(SelectionIndex - Change, 0)
If txtQuantity.Text <> "Enter Quantity" Then
If cmbStateRateSumRatio.SelectedIndex <> -1 Then
bttAddQUAtoDatabase.Enabled = True
End If
End If
End Sub`
Thanks for you help.
Use the KeyPress event. Set e.Handled to true if you don't like the character. It's a one-liner:
Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As PressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
End Sub
In the textbox's KeyDown event, check e.KeyCode. This lets you prevent certain characters from being handled. There's an example on the KeyDown documentation.

Text box validation

I am using many text boxes in a form.
How do i validate them,
In certain text boxes I have to use only text and in some I have to use only numbers.
Is using ASCII is a right method or is there any easier method to do this. If so please let me know the coding.
Above all other, don’t annoy the user. If I’m typing some text and the application prevents that (regardless of how it does that), I’m rightfully pissed off.
There are multiple values to handle this:
Use a NumericUpDown or a Slider control instead of a text box for numeric values (in other words: use the correct control instead of a general-purpose control).
Allow (more or less) arbitrary input and try to parse the user input in a meaningful way. For example, entering “+33 (0) 6 12-34-56” is an entirely meaningful format for a phone number in France. An application should allow that, and try to parse it correctly.
Granted, this is the hardest way, but it provides the best user experience.
Use the Validating event to validate input. This is automatically triggered whenever the user leaves the input control, i.e. when they have finished their input, and a validation will not annoy the user.
The MSDN documentation of the event gives an example of how this event is used correctly.
But do not use the KeyPress or TextChanged events to do validation. The first will disturb the users when entering text. The second will also annoy them when they try to paste text from somewhere else. Imagine the following: I am trying to copy an number from a website. Unfortunately, the text I have copied includes something else, too, e.g. “eggs: 14.33 EUR” instead of just “14.33”.
Now, the application must give me the chance to paste and correct the text. If I am not allowed to do that, the application is a UX failure. If the application uses the TextChanged event to prevent my pasting this text, I don’t get the chance to delete the offending text.
Text only limited to 40 characters:
<asp:RegularExpressionValidator ID="regexpText" runat="server"
ErrorMessage="Text only!"
ControlToValidate="txtName"
ValidationExpression="^[a-zA-Z]{1,40}$" />
Only Numbers:
<asp:RegularExpressionValidator ID="regexpNumber" runat="server"
ErrorMessage="Numbers only!"
ControlToValidate="txtName"
ValidationExpression="^[0-9]$" />
Wow, this can be a very broad topic...
For numeric textboxes, you should probably restrict input during KeyPress event:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If
End Sub
NOTE: This code sample is assuming WinForms, different approach must be used for web...
Regardless of the plat form, you should look into the validation controls offered by the framework, and that will allow you to validate that there is indeed input, values are in a specified range, and also using regex write more complicated validation rules.
The fastest way for validation is using regular expressions. They are harder to understand but offer better performance.
But you could do it also using string functions. Which is easier if you don't know regex but is less performant. This could be a viable option, depending on how hard the validation is.
Here and here are some posts that will help you with code samples.
Agreed that Regular Expressions might be faster, but ... well, here's how I've done it. Basically, this code is for a UserControl which contains a label, a text box, and an error provider. It also has various other properties, but here's the bit which deals with validation.
I do use this on the TextChanged event, because I don't want the user to continue typing if it's an invalid character; the rule checking "eats" the invalid character.
Public Enum CheckType
ctString = 0
ctReal = 1
ctDecimal = 2
ctInteger = 3
ctByte = 4
End Enum
Private mAllowNegative As Boolean = True
Private mAllowNull As Boolean = True
Private mCheckType As CheckType = CheckType.ctString
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
RuleCheckMe()
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub RuleCheckMe()
'// Rule Checking
If Me.TextBox1.TextLength = 0 Then
If mAllowNull = False Then
Me.epMain.SetError(Me.TextBox1, "You are required to provide this value.")
Me.Valid = False
Else
Me.epMain.Clear()
Me.Valid = True
End If
Else
Select Case mCheckType
Case CheckType.ctString
If mInputMask.Length > 0 Then
'TODO: Figure out how to cope with input masks!
Me.Valid = True
Else
Me.Valid = True
End If
Case Else '// right now we're only testing for numbers...
If Not IsNumeric(Me.TextBox1.Text) And Me.TextBox1.Text <> "." And Me.TextBox1.Text <> "-" Then
If Not String.IsNullOrEmpty(Me.TextBox1.Text) Then
Me.TextBox1.Text = Me.TextBox1.Text.Remove(Me.TextBox1.Text.Length - 1, 1)
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
End If
Me.epMain.SetError(Me.TextBox1, "This field does not accept non-numeric values.")
Me.Valid = False
ElseIf mAllowNegative = False And Me.TextBox1.Text.StartsWith("-") Then
Me.TextBox1.Text = Me.TextBox1.Text.Remove(Me.TextBox1.Text.Length - 1, 1)
Me.epMain.SetError(Me.TextBox1, "This field does not accept negative values.")
Me.Valid = False
ElseIf mCheckType = CheckType.ctByte And CType(Me.TextBox1.Text, Integer) > 255 Then
Me.epMain.SetError(Me.TextBox1, "This field does not accept values greater than 255.")
Me.Valid = False
Else
Me.epMain.Clear()
Me.Valid = True
End If
End Select
End If
End Sub
<System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always), _
System.ComponentModel.Category("Data")> _
Public Property AllowNegative() As Boolean
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mAllowNegative
End Get
<System.Diagnostics.DebuggerStepThrough()> _
Set(ByVal value As Boolean)
mAllowNegative = value
End Set
End Property
<System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always), _
System.ComponentModel.Category("Data")> _
Public Property AllowNull() As Boolean
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mAllowNull
End Get
<System.Diagnostics.DebuggerStepThrough()> _
Set(ByVal value As Boolean)
mAllowNull = value
End Set
End Property
<System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always), _
System.ComponentModel.Category("Data")> _
Public Property DataTypeCheck() As CheckType
<System.Diagnostics.DebuggerStepThrough()> _
Get
Return mCheckType
End Get
<System.Diagnostics.DebuggerStepThrough()> _
Set(ByVal value As CheckType)
mCheckType = value
End Set
End Property
i would like to share my text box validator..
Dim errProvider As New ErrorProvider
' Verify that this field is not blank.
Private Sub txtValidating(sender As Object,
e As System.ComponentModel.CancelEventArgs) Handles _
txtName.Validating, txtStreet.Validating, txtCity.Validating,
txtState.Validating, txtZip.Validating
' Convert sender into a TextBox.
Dim txt As TextBox = DirectCast(sender, TextBox)
' See if it’s blank.
If (txt.Text.Length > 0) Then
' It’s not blank. Clear any error.
errProvider.SetError(txt, “”)
Else
' It’s blank. Show an error.
errProvider.SetError(txt, “This field is required.”)
End If
End Sub
' See if any field is blank.
Private Sub Form1_FormClosing(sender As Object,
e As FormClosingEventArgs) Handles Me.FormClosing
If (txtName.Text.Length = 0) Then e.Cancel = True
If (txtStreet.Text.Length = 0) Then e.Cancel = True
If (txtCity.Text.Length = 0) Then e.Cancel = True
If (txtState.Text.Length = 0) Then e.Cancel = True
If (txtZip.Text.Length = 0) Then e.Cancel = True
End Sub
Private Sub TxtEmployeenumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtEmployeenumber.KeyPress
Dim c As Char
c = e.KeyChar
If Not (Char.IsDigit(c) Or c = "." Or Char.IsControl(c)) Then
e.Handled = True
MsgBox("numeric texts only")
End If
End Sub
just go to the keyup event of text box and enter the following code
100% it will work
if(Not Char.IsNumber(Chrw(e.Keycode))) Then
Messagebox.show ("only numeric values ")
textbox1.text=""
end if