net.vb only numbers and validation message - vb.net

In to my Windows App Form, I have two text boxes and I would like the User to give me only Numbers And if the user gives me letters like for example (hello) when he executes the button I would like to inform the user through a window Validation message " Please enter numerical values ". How can I write it through Basic code?

You may simply use the function IsNumeric() to achieve it.
Look at the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If IsNumeric(TextBox1.Text) And IsNumeric(TextBox2.Text) Then
Else
MsgBox("Please input numbers only!")
End If
End Sub
Output of the form:
TextBox2 verification:
Enjoy!

You can check text box value with help of regex or isnumeric function. you can validate input at leave event of textbox and use error provider control to show warning message.
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
If Not Regex.IsMatch(TextBox1.Text, "\d+") Then
ErrorProvider1.SetError(TextBox1, "only numerics are allowed")
Else
ErrorProvider1.Clear()
End If
End Sub
End Class

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

Visual Studio Button code error

This is my first time using visual studio and I'm running into an error with my tax calulator application.
The problem is that visual studio is saying that the multiplication line using "*" and "+" can not be done due to the variables being text boxes. So my question to the community, is should I change the text box to something else? Or where in my code did I mess up.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale) And IsNumeric(txtSalesTaxRate)) Then
lblTax = txtSale * txtSalesTaxRate
lblTotal = txtSale + lblTax
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
If you need me to give you the full layout of my application, do ask.
I can see multiple problems in your code. I will explain how the "vs" works.The textboxes that you've made are controls.You can't just take their value so simple.They have many property and one of them is .text which allows you to take the value inside them.Another mistake you've made is what you've tried to do with the textboxes.What you type in those textboxes is ..well text. The program can't tell if the value inside is a number or just text. You must convert that value in a number using Cint.So you're code will look like this:
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cint(txtSale.text) * cint(txtSalesTaxRate.text)
lblTotal.text= cint(txtSale.text) + cint(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class
What cint does is to convert every type of data to integer. Also the .text property lets you to set the value of the control (in our case the label caption)
I tried Electric-web's code and ran into a problem with the output. I changed "CInt" to "CDbl" and the tax calculator worked.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
lblTax.text= cdbl(txtSale.text) * cdbl(txtSalesTaxRate.text)
lblTotal.text= cdbl(txtSale.text) + cdbl(lblTax.text)
Else
MsgBox("Please enter valid numbers, thank you!")
End If
End Sub
End Class

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.

Capture keypress / let user pick their own start/stop key

Currently I have the start-key for my vb.net application hardcoded like this:
GetAsyncKeyState(Keys.F2)
Where vb.net sais "F2 As System.Windows.Forms.Keys = 113" on mouse-over
But I want my users to be able to pick their own Key. If I make a drop-down box (combobox) and pre-define some choices in there (Like ESC or F3), all those choices are strings. How can I convert those strings to a System.Windows.Forms.Keys integer?
Also, I'd like it to also be possible to "capture" a single keypress. So they'd click the "capture" button, and the next key they hit will be saved as the start/stop button. But I wouldn't even know where to begin looking for that one.
If txtKeys.Text=="F3" Then
GetAsyncKeyState(Keys.F3)
End If
Try something like this:
Public Class Form1
Dim captureKey As Boolean
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
captureKey = True
End Sub
Private Sub Button1_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If captureKey Then
Label1.Text = e.KeyValue.ToString
captureKey = False
End If
End Sub
End Class
I created a form with a label and a button for an example. e.KeyValue is an integer that I am converting to a string for display purposes. You also have the ability to capture other keydata. See this info on PreviewKeyDownEventArg
As for the first part of your question use a Select Case Statement to convert between your ComboBox Values and KeyData Values.