-
Dec 29th, 2017, 10:28 AM
#1
Thread Starter
Junior Member
Checking person age
Hello, I'm working on the form where I want the user to enter their date of birth and if is under 18 they will see the message box. The form consist of 2 textboxes: Day, Year and one combobox with list of Months. At the moment no matter what year I enter everything is passing through.
Code:
Code:
Private Sub btnContinue_Click(sender As Object, e As EventArgs) Handles btnContinue.Click
Dim currentYear As Integer = Today.Year
Dim CURRENTmONTH As Integer = Today.Month
Dim CurrentDay As Integer = Today.Day
Dim birthYear As Integer = Integer.TryParse(txtYear.Text, birthYear)
Dim birthMonth As Integer = Integer.TryParse(cboMonths.SelectedIndex - 1, birthMonth)
Dim birthDay As Integer = Integer.TryParse(txtDay.Text, birthDay)
Dim Age As Integer = currentYear - birthYear
If CURRENTmONTH = birthMonth Then
If CurrentDay < birthDay Then
Age -= 1
End If
ElseIf CURRENTmONTH < birthMonth Then
Age -= 1
ElseIf Age < 18 Then
MessageBox.Show("You're not old enough to register. Use guest Account")
Else
MessageBox.Show("You're good to go")
End If
End Sub
Last edited by Shaggy Hiker; Dec 29th, 2017 at 10:31 AM.
Reason: Added CODE tags.
-
Dec 29th, 2017, 10:36 AM
#2
Re: Checking person age
Welcome to the forums. I edited your post to add [CODE][/CODE] tags which you can do by pressing the # button and pasting the code between the resulting tags.
As for the question, you'd be better off using a DateTimePicker to get the birthday than using a trio of textboxes, but that's up to you. In any case, TryParse doesn't return an integer, it returns a Boolean, so for the code to even compile, you must have Option Strict OFF. If you turn Option Strict ON, you'll probably find several problems, and they are all worth fixing.
If you stick with the integers, you might as well just turn them into a date:
Code:
dim birthDate = new Date(birthYear,birthMonth,birthDay)
at which point you can do something like this:
Code:
If (date.Today-birthDate).TotalYears<18 Then
However, if you want to go the way you are, then fix the problem with TryParse, then put a breakpoint on the first If statement and step through the code to see what is happening. This is an excellent place to learn breakpoints and code stepping. If the code isn't doing what you expect, don't guess at why not. With a breakpoint, you can pause the code at any line and see what all the variables hold, then step forwards line by line (F11) and watch the variables change, see which branches are taken at any If statement, and so on.
My usual boring signature: Nothing
-
Dec 29th, 2017, 10:56 AM
#3
Re: Checking person age
Hi,
use Dtp like Shaggy said, or I would just use a Textbox
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim Birth As Date = TextBox4.Text
If Date.TryParse(Birth, TextBox4.Text) = False Then
MessageBox.Show("wrong Date !")
End If
Dim year, month, day As Integer
While year <= (Now.Year - Birth.Year)
While month <= (Now.Month - Birth.Month)
While day <= (Now.Day - Birth.Day)
day += 1
End While
month += 1
End While
year += 1
End While
year -= 1 : month -= 1 : day -= 1
If year < 18 Then
MessageBox.Show("You're not old enough to register. Use guest Account")
End If
Label1.Text = (year & " year(s) " & month & " month(s) " & day & " day(s)")
End Sub
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Dec 29th, 2017, 11:32 AM
#4
Thread Starter
Junior Member
Re: Checking person age
Hi Shaggy and ChrisE, thanks for helping me out. I have tried use breakpoint, and I notice that since that line the value is set to -1 for some reason and is using -1 for all calculations. I wonder why is increasing year to 2018 instead of 2016, since is suppose to calculate 2017-1? Is it because of 2017-(-1)=2018? Thanks. PS: Iam not quick at learning. Thank you for patience.
Code:
Code:
Dim birthYear As Integer = Integer.TryParse(txtYear.Text, birthYear)
Dim birthMonth As Integer = Integer.TryParse(cboMonths.SelectedIndex - 1, birthMonth)
Dim birthDay As Integer = Integer.TryParse(txtDay.Text, birthDay)
-
Dec 29th, 2017, 11:49 AM
#5
Re: Checking person age
You're using TryParse wrong... TryParse returns a BOOLEAN VALUE ... indicating if the conversion was successful or not. The numerical conversion is returned through the second parameter. So it returns the value through the output parameter, but then you immediately overwrite it by stuffing the true/false return value into the integer value. The code should look likle this:
Code:
Dim birthYear As Integer = 0
Dim birthMonth As Integer = 0
Dim birthDay As Integer = 0
If Integer.TryParse(txtYear.Text, birthYear) andalso Integer.TryParse(cboMonths.SelectedIndex - 1, birthMonth) andalso Integer.TryParse(txtDay.Text, birthDay) Then
... work with the values here
else
.. one or more of the conversions was bad
end if
-tg
-
Dec 29th, 2017, 12:08 PM
#6
Thread Starter
Junior Member
Re: Checking person age
Is working now, thank you, guys. I really appreciate you took your time to help me. Thanks again
-
Dec 29th, 2017, 02:16 PM
#7
Re: Checking person age
As I said earlier, if you turn Option Strict ON, it would have shown this problem. Option Strict is off by default, but that allows you to do unsafe conversions, so it's best to set Option Strict ON in either the project or for ALL projects using Options (Under Projects and Solutions on the VB Defaults tab).
My usual boring signature: Nothing
-
Dec 29th, 2017, 02:31 PM
#8
Thread Starter
Junior Member
Re: Checking person age
Thanks for the tip I'll enable from now on.Is there any way to compare two strings from two textboxes.
Textbox1.Text = TextBox2.Text doesn't work.
-
Dec 29th, 2017, 03:33 PM
#9
Re: Checking person age
In what way does it not work, cause that should work...fairly well. There are ways it can fail if you do it that way. For one thing, the capitalization has to be the same. If you don't want to care about that you can use String.Equals, which has several overloads. One of the overloads takes a third argument, which you would set to CurrentCultureIgnoreCase (or whatever option like that Intellisense gives you).
That won't take care of hidden spaces, though. A leading or trailing space would be invisible, and darn hard to see unless you are using a font that has all characters taking up the same width (TrueType fonts, I think). You can use String.Trim() to get rid of any leading or trailing spaces, but that still leaves the case where somebody put two spaces between words. There are two spaces between two of the words in the last sentence, but you may not see which two. Similarly, that can be hard to see in a textbox, and I don't know any good way to catch that.
For strings to be equal, they really have to be equal, including in their hidden characters. Trim takes care of leading and trailing characters...beyond that you're a bit hosed.
My usual boring signature: Nothing
-
Dec 29th, 2017, 04:38 PM
#10
Thread Starter
Junior Member
Re: Checking person age
Well I make mistake now I know why wasn't working. I have came up with something like this? Is working well. My programming level is just beginner do you think I could improve som
Do you think is appropriate to use textchanged event for txtConfPass? Thanks
Code:
Code:
Private Sub txtPassword_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPassword.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) And txtPassword.Text.Length < 7 or e.KeyChar= ControlsChars.Back Then
e.Handled = False
Else
e.Handled = True
lblErrPass.Visible = True
lblErrPass.Text = "Password can consit of letters, numbers and up to 7 characters"
End If
End Sub
Private Sub txtConfPass_TextChanged(sender As Object, e As EventArgs) Handles txtConfPass.TextChanged
If (txtConfPass.Text <> txtPassword.Text) Then
lblPassCheck.Visible = True
lblPassCheck.Text = "Password don't match."
ElseIf (txtConfPass.Text = txtPassword.Text) Then
lblPassCheck.Visible = True
lblPassCheck.Text = "Correct."
End If
End Sub
Last edited by BloodySandwich; Dec 29th, 2017 at 04:43 PM.
Reason: code editing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|