How to send the datetimepicker value to textbox, only when clicking the date, specific days or year of the month - vb.net

Everytime I double click the datetimepicker, it automatically send its values to the textbox. Now, I want to send the value of DateTimePicker in the Textbox, only when I click the date, specific days or year of the month?
Here's my vb.net code:
Private Sub DateTimePicker1_DOB_CloseUp(sender As Object, e As EventArgs) Handles DateTimePicker1_DOB.CloseUp
TextBox1_Date.Text = DateTimePicker1_DOB.Text
End Sub

Related

Days in a certain month

I want to calculate how many days there are in a month. Obviously if there is a leap year then February would have 29 days.
I have some code that works out if it a leap year or not, but I do not know if it is useful.
I tried to do the current year and to count the days of the month that is entered; however there's an error and I'm not sure what to do.
Sub daysInMonth()
Console.WriteLine("Please Enter month you will like to calculate number of days: ")
Dim inputMonth As DateTime = Console.ReadLine()
Dim newMonth = DateAndTime.Month(inputMonth)
Dim current = Now()
Dim currentYear = (Year(current))
Dim febuaryLeapYear = System.DateTime.DaysInMonth(currentYear, newMonth)
End Sub
Solved
I have this function which returns the number of days in any month, any year. Hope helps
Private Function Dates(ByVal Year As Integer, Month As Integer) As Integer
Return DateTime.DaysInMonth(Year, Month)
End Function
I think the problem in this case is that DaysInMonth requires the month to be expressed as an integer. So if you change inputMonth to Integer, you shouldn't receive an error.
Integer.TryParse(string, integerVariable) will check the string to see if it can be converted to an Integer. It will return True or False so it can be used in an If statemnent. In addition it fills the integerVariable with the Integer representation of the string.
The AndAlso parts of the If will never execute if the previous condition is false. This is called short circuiting. Your code will not blow up comparing numbers in the AndAlso conditions because if the first part is false it never executes.
Sub CalculateDaysInMonth() 'It is a bad idea to name your Sub the same as a .net method
Do 'The method will loop until the user enters correct data
'A more explicit message will help user to enter correct data
Console.WriteLine("Please enter a month by entering a number between 1 and 12")
Dim inputMonth As Integer
If Integer.TryParse(Console.ReadLine, inputMonth) AndAlso inputMonth > 0 AndAlso inputMonth < 13 Then
Dim currentYear As Integer = Now.Year
Dim numberOfDays As Integer = DateTime.DaysInMonth(currentYear, inputMonth)
'This is an interpolated string. If your version doesn't support this you can use String.Format
'String.Format("There are {0} in month {1} this year", numberOfDays, inputMonth)
Console.WriteLine($"There are {numberOfDays} in month {inputMonth} this year")
Return
End If
Loop
End Sub

vb.net Refresh DatePicker value after everyday

I have this code under a Timer's tick event to refresh the value of a DateTimePicker after everyday/at 7:19 (tTmNow)
`Private Sub tmrNewDay_Tick(sender As Object, e As EventArgs) Handles tmrNewDay.Tick
If tTmNow.Text.Contains("7:19:") Then 'at this time pick yesterday's date
Dim ystd As String
ystd = Today.AddDays(-1) 'yesterday
dtPicker1.Value = ystd 'put yesterday's date into the date time picker
End If
End Sub`
Unfortunately, at the first run of the program, I achieve my aim successfully. But the second day (The program is to run everyday and pick yesterday's date), when picking the date, it adds the time; giving 19-04-2017 00:00:00, instead of 19-04-2017. Is there a way to refresh the contents of the DateTimePicker or is there an entirely new way of picking yesterday's date, day after day.

how to detect number of days of the succeeding month in vb.net?

I have two datetimepickers and one textbox. One is for effectivitydate and the other one is for expirationdate. Textbox is for the result number of days of two datetimepickers. If the textbox is 5 days beyond the succeeding month then the messagebox will popup with the message "The document was submitted beyond 5 days of the succeeding month!"
You could use:
Dim maxDate = New Date(Now.Year, Now.Month + 1, 5)

Determine the day someone turns 21

I need to determine the day of week that someone turns 21. I have tried so many different ways to do this, I can get the age, the day of week a person was born but not the day they turned 21, this is my current code. Yes this is homework but I have worked a few hours on this and can't figure it out,
Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click
Dim dob As Date = txtDay.Text
Dim age, days As Double
days = DateDiff(DateInterval.Day, dob, Today) / 365
age = Fix(days)
txtWeek.Text = age.DayOfWeek.ToString()
End Sub
First of all, think of how you would solve this problem in real life, then try to write a program that would go about it in the same manner.
So, first of all, I would find out what day is the subject's 21st birthday by simply adding 21 years to his/her date of birth. This is really simple in Visual Basic because of the AddYears function.
Dim _21BirthDay = dob.AddYears(21)
Then, I would look at a calendar and find out what day of the week that was.
Dim WeekDayOf_21BirthDay = _21BirthDay.DayOfWeek
Then, I could use the WeekDayOf_21BirthDay variable to show the user.

excel auto fill date based on current date

I need to auto fill a specific column with a date. If the date occurs between Saturday through Tuesday, I need to auto fill the date column with the previous Friday. If the date occurs between Wednesday and Friday I need to put in the coming Friday date for the auto fill.
For example: If I run the spreadsheet on Tuesday 10/23 I need the auto fill date to be Friday 10/19. If I run the spreadsheet on Wednesday 10/24 I need the auto fill date to be Friday 10/26.
Here is the formula I have so far, so I need to invoke this formula via macro when saving the spreadsheet or by clicking a custom button. Any assistance would be greatly appreciated.
=IF(ISBLANK($A2),"",IF(WEEKDAY(TODAY())<4,(TODAY()+(-1-WEEKDAY(TODAY()))),(TODAY()+(6-WEEKDAY(TODAY())))))
I'd suggest using VBA for what you want, something like this which would go in the ThisWorkbook module and run prior to the workbook saving:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Check if A2 is blank
If Cells(2, 1) = "" Then Exit Sub
'Find the date and use it
Dim x As Long
Dim dateToUse As Date
x = Weekday(Date, vbSaturday)
If x <= 4 Then
dateToUse = Date - x
Else
dateToUse = Date + (7 - x)
End If
'Change Cells(1, 1) to the actual target you want to have the date,
'which could be determined based upon the contents of your workbook/sheet.
Cells(1, 1) = dateToUse
End Sub