Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there are two textboxes,can we calculate age without any button just by moving from one textbox to another
Posted

1 solution

Assuming your first textbox contains the date of birth use:

VB
Protected Sub txtDob_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles txtDob.Enter
if isdate(txtBirthDate.Text)
    txtDob.Text = DateDiff(DateInterval.Year, Now, CDate(txtBirthDate.Text)).ToString
End sub


Every time you enter / focus your second textbox, the difference between current time and day of birth will be re-calculated.

This is VB.NET code, but it can easily be translated to C#.
 
Share this answer
 
v2
Comments
Member 11137508 9-Oct-14 3:41am    
Thanks for your quick reply but iam having a doubt which is the id for age textbox and which is for dob ,and where the above code must be placed
Sinisa Hajnal 9-Oct-14 3:59am    
txtDob is the id of your second textbox (ona koja sadrži dob - the one containing "dob"). txtBirthDate is your first textbox (which is only my assumption that it is birth date, but it is whatever your originaly entered date is)

The code has to be placed anywhere within your form code (it is just an event handler - as any other sub)

DateDiff calculates the difference between current date and your entered date...if I misunderstood your question, please try to explain again, I'll change my solution to fit.
Member 11137508 9-Oct-14 4:33am    
no you have correctly understood but when i placed it in vb code it is giving an error near override
Sinisa Hajnal 9-Oct-14 4:45am    
I wrote from memory. Remove the sub, go to txtDob properties, change view to see events and double click on Enter...you should see something very similar to the above with Handles txtDob.Enter at the end.

You can get the same by selecting txtDob in code-view from drop down list of controls on the form and on the right select Enter from event drop down
Sinisa Hajnal 9-Oct-14 4:48am    
I've updated the solution with correct event signature

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900