trying to validate user input when exiting textbox in VB - vb.net

I would think this problem is so simple and basic , I just can't get anything to work. I just want to validate user input and I can never get the validation rules to fire only when I want . I've tried using got focus , lost focus , leave , and validating . Some of those fire when the forms loaded some trigger when other fields look up . There have to be an easy way I hope you can understand and can help. thanks

Please try the following txtName.TextChanged as the event, this will run each time the value is changed in the text box.
Private Sub txtName_TextChanged(sender As Object, e As EventArgs) Handles txtName.TextChanged
End Sub

You can use the .net ErrorProvider and the Validating event.
Private err As New ErrorProvider()
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "" Then
e.Cancel = True
err.SetError(TextBox1, "This text box cannot be blank.")
Else
err.Clear()
End If
End Sub

Related

how to set minimum limit of a textbox vb.net

I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(
Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same codeFrom what I can understand, something like this should help get you on your way. Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click of if the textbox is clicked off.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
checkPassword(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
checkPassword(TextBox1.Text)
End Sub
Private Sub checkPassword(password As String)
If Not password.Length > 5 Then
Label1.Text = "The password must be more than 5 charcharacters"
TextBox1.Clear()
Else
Label1.Text = "Password accepted"
End If
End Sub

vb.net masked textbox and datetime now string not working together?

I have a masked textbox set to date.short I would like to automate the cuurentdate to be filled in when the masked textbox is clicked I though the following would work but I get the error InvalidCastExpectation was ungandeld "Application is in break mode"
Private Sub MaskedTextBox1_Click(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.Click
MaskedTextBox1.Text = DateTime.Now.ToString("dd/MM/yyyy")
End Sub
I also thought about changing ("dd/MM/yyyy") to ("dd-MM-yyyy") but this also dosnt fix it?
The Click event does not use the MaskInputRejectedEventArgs parameter:
Private Sub MaskedTextBox1_Click(sender As Object, e As EventArgs)
Handles MaskedTextBox1.Click

How to enable overwrite text in a textbox?

I'm making a program which requires a login system. What I want to do is have a textbox which contains the word 'Username' and when the user clicks it, the 'Username' text is overwritten. For example, Spotify use it on their login screen:
My question is, how do I do this?
Set the Text property of the TextBox that you are using for the username to the string "Username". Then, in the TextBox's Click event, change it to a blank string. Like so:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
TextBox1.Text = ""
End Sub
Edit:
As #LarsTech mentioned, this does not address if the user tabs into the TextBox. If you wanted to account for that too, use the TextBox's Enter event instead:
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
TextBox1.Text = ""
End Sub
I agree, using Textbox1.Enter is the easiest solution. On top you can also catch the case of no text entered via TextBox1.Leave like that
Private Sub TextBoxLeaveHandle() Handles TextBox1.Leave
If TextBox1.Text = "" Then
TextBox1.Text = "Username"
End If
End Sub
Sometimes it can also be useful to use the TextBox.SelectAll() function as it not immediately remove the entire text but (obviously from the name) select the entire text so you can overwrite it with your first keypress.

How to automate TextBox control for Enter key

I have to do the following for each textbox control.
If e.KeyCode = Keys.Enter Then
Me.DateDateTimePicker.Focus()
End If
Should there be a For Next loop option, pls mention under which Sub Routine I should code the loop.
From what you're saying, I think this should be placed within the KeyPressed event within the text box you are trying to run this piece of code from. You can also integrate multiple events for many controls into a single method.
Hope this helps!
You can but your code inside a 'KeyUp' event and then alter the 'KeyUp' event of one textbox to handle more than one textbox keyups event, but only one code can be executed for all of them,
check this:
Private Sub ***TextBox1_KeyUp***(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyUp, ***TextBox2.KeyUp***, ***TextBox3.KeyUp***
'*************
'Write your code Here
'*************
End Sub
As others said, you can use only one method to handle multiple events.
Moreover, you can execute sender-customized code:
Sub MyEventHandler(sender as Object, e as Event) Handles obj.ev, obj2.ev, obj3.ev
'Use sender property (properly casted, if necessary)
'to run sender specific code
DirectCast(sender, TextBox).Text = "foo"
End Sub

Ok button in VB.net

I have a form that contain a combobox, I'm inserting an error check to check if the user hit the ok button before selecting a valid value from the combobox it mask the error and keep the form on focus, till the user select a proper value, but in my case after raise the error the application still close after i press Ok, would you help me identify my mistake
here is my code
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Try
If SymbolComboBox.SelectedValue Is Nothing Then
Throw New Exception()
Else
ErrorProvider1.SetError(SymbolComboBox, String.Empty)
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End If
Catch ex As Exception
ErrorProvider1.SetError(SymbolComboBox, "Error")
End Try
End Sub
Thank you in advance
Jp
Is your OK button the default accept button for the form ? If so, I beleive it will always return System.Windows.Forms.DialogResult.OK no matter the outcome, unless you specifically cancel the action. I do not remember how to cancel the action though, but if it's the case, I sugesst you remove that AcceptButton property of your form.
OR,
Is it possible that SymbolComboBox.SelectedValue is never 'Nothing' even when nothing is selected (like an empty string) ? I would use selectedIndex instead and check if it's equal to -1.
Hope that helps.
I'm not sure, but I do know that using exceptions like this to control program flow is a bad technique, and in this case unnecessary:
Private Sub OK_Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles OK_Button.Click
If SymbolComboBox.SelectedValue Is Not Nothing Then
ErrorProvider1.SetError(SymbolComboBox, String.Empty)
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
Else
ErrorProvider1.SetError(SymbolComboBox, "Error")
End If
End Sub
Why not look at using a required field validator to do this? Are you doing Web forms or Win Forms development? Also what is the value of SymbolComboBox.SelectedValue when you get this issue?