Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTimePicker

Status
Not open for further replies.

dbsquared

Programmer
Nov 7, 2002
175
US
I searched the foruma and was unable tofind an answer to my problem. When i tryto cahnge themonth of theDatetimepicker it fires the valuechanged code below, then gets stuck in a loop for the valuechanged and keeps bringing up my messagebox. Is there a way to test if only the arrows at thetop are pressed and ignore if that is true?

Code:
Private Sub dtBegin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtBegin.ValueChanged
        Dim Intdayofweek As Integer

        Intdayofweek = DatePart(DateInterval.Weekday, dtBegin.Value)
        If Intdayofweek = 1 Then
            cmdReport.Visible = True
        Else
            MessageBox.Show("You must pick a Sunday for the report to run.  Please try again", "Date Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

To go where no programmer has gone before.
 
Create a class level private variable that stores the "OldValue" of the DatePicker's SelectedDate value. In that sub see if the DatePicker's SelectedDate value is the same as the OldValue, if so, they just changed months, if not, then they selected a new date.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
That really didn't work but here is what I had to do to get it to work and it still didn't work that well because if the day is the same day you are trying to select in a different month then it wouldn't hit the sub correctly. As when you cahnge to a new month the datevalue for the datetimepicker changes to the same day but the month changes to the new one.

so here is what I had to do. firstdate is a global that is initiated on form load

Code:
Private Sub dtBegin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtBegin.ValueChanged
        Dim Intdayofweek As Integer
        Dim tempdate1, tempdate2 As Integer

        tempdate1 = CInt(Format(Firstdate, "dd"))
        tempdate2 = CInt(Format(dtBegin.Value, "dd"))
        Intdayofweek = DatePart(DateInterval.Weekday, dtBegin.Value)
        If Intdayofweek = 1 Then
            cmdReport.Visible = True
        Else
            If tempdate1 <> tempdate2 Then
                MessageBox.Show("You must pick a Sunday for the report to run.  Please try again", "Date Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
        Firstdate = dtBegin.Value
End Sub
[\code]

To go where no programmer has gone before.
 
If I was you, I would think about creating a custom control that only allows Sundays, or a customizeable day of week. It would be nice to have it available from a drop down or give the user some sort of visual clue, so that they do not have to look at a real calender to find the date of a Sunday in order to enter it into your dtpicker.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top