Link to home
Start Free TrialLog in
Avatar of SweetingA
SweetingA

asked on

Problems with vb express date picker control

I have 2 date picker controls on a form.

If i pick a date in the current month, no problems.

If i try to hit the left or right arrows using the mouse click to continue searching its very unstable and usally just moves the 1st day of the previous month.

Any ideas most welcome....
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi SweetingA;

To your statement, "If i try to hit the left or right arrows using the mouse click to continue searching its very unstable and usally just moves the 1st day of the previous month."; What do you mean byits very unstable?

If I remember correctly if you click on the previous month button it will move to the previous month highlighting the first of the month and the same is true if you click on next month button.
Avatar of SweetingA
SweetingA

ASKER

Hi Fernando,

Most of teh time it does not leave the calendar open, it just puts the first day of the last month in and closes the calendar.  I have to do it several times to try and get a chosen date in an earlier month.

Regards
Hi SweetingA;

Clicking on a date will close the calendar but clicking the left and right arrows should move the calendar to the next/previous month without closing unless you click outside of the calendar. So to choose a date in a earlier month click on the left arrow multiple times until you get to the month you want and then click on the date in that month, clicking on the date will close the calendar. Another way to do that would be to open the calendar click between the month name and one of the left/right arrows which will display all 12 months name click on the month you want to go to and it will display that month and then select you date. The calendar should not close while you do one of the above ways.
I can navigate round clicking the centre date to change months and years, that way works fine but when i click the rarows its a bit flakey and most of the time picks the first day in the month and closes the calendar view.

Its not the end of the world but it is very annoying, i just though it may be a bug taht someone might know how to work around.

Thanks anyway.
I am unable to reproduce that issue. If you can build a test app where this is happening I will have a look at it. If you do create the test app please zip the complete project with all files and post to the web where I can download it a web site such as Microsoft OneDrive.
OK Fernando thanks, i'll take a look but it might take me an hour or so as the wife is giving me earache for being on the PC all the time.

Thanks
Hello Fernando,

Link is below

https://onedrive.live.com/redir?resid=C91A0203A9EBBE32!111&authkey=!AI7fOb9YbxYNZcc&ithint=file%2czip

As i said sometimes it works sometimes not but you will see what i mean very quickly.

Select "User Defined" in the combo box then use the date pickers.

Thanks Alan
Hi SweetingA;

A couple of issues.

1.  You have the following two lines of code in a couple of places. This is configuration of the DateTimePicker control and should only be done once like in the form load event. Leaving it set in this way will cause the text box of the control to NOT display anything even when the date has been set.

datStart.CustomFormat = " "
datStart.Format = DateTimePickerFormat.Custom

2.  When wireing up controls you need to be careful because they may fire multiple times. An example of this is the datStart_ValueChanged event. This event will fire on all clicks within the opened calendar window, for example left/right arrows, white space between month name and an arrow and the date itself. So whatever code is placed in it will be executed multiple time.

3.  The issue causing the control to be unstable is that you had configuration commands in the datStart_ValueChanged and this is causing the control to reset the calendar drop down. Some events that can be used that execute once is Enter, Leave, Validating, Validated and some others.

To see it work like it should, do the following two things, 1 in the DTGraph_Load event give a format value to datStart.CustomFormat and 2 comment out the line in datStart_ValueChanged event.
Hello Fernando,

As ususal you are spot on.  However thsi only solves the problem at date start and not date end.  I need to do some manipulation after date end and it seems any code in the ValueChanged event destabilies the date picker.  Any further ideas?  Revised code it below - i have emade some changes to the datEnd and deleted the datStart.

What i initially wanted to do is execute the SQL as long as there were dates in the 2 date boxes and anytime either of the boxes were updated.

Any further ideas?


    Private Sub datEnd_ValueChanged(sender As System.Object, e As System.EventArgs) Handles datEnd.ValueChanged
        'Date Range OEE
        Try
            datEnd.Format = DateTimePickerFormat.Long
            Dim sd As DateTime = datStart.Value.ToString("yyyy/MM/dd")
            Dim ed As DateTime = datEnd.Value.ToString("yyyy/MM/dd")
            Dim Result As DialogResult
            Result = MsgBox("Are you sure the dates are correct?", vbQuestion + vbYesNo, "Message Alert")
            If Result = System.Windows.Forms.DialogResult.Yes Then
                Dim mind As DateTime
                Dim maxd As DateTime
                SQL.RunQuery("SELECT Max(Date) As MaxDate, Min(Date) As MinDate  FROM qry_OEE_DateRange")
                For Each i As Object In SQL.SQLDataSet.Tables(0).Rows
                    maxd = i.Item("MaxDate")
                    mind = i.Item("MinDate")
                Next
                Dim pmind As DateTime
                Dim pmaxd As DateTime
                pmind = sd
                pmaxd = ed
                If ed > maxd Then pmaxd = maxd
                If sd < mind Then pmind = mind
                SQL.RunQuery("SELECT OEE As OEE, Date As Date FROM qry_OEE_DateRange WHERE Date >= '" & pmind.ToString("yyyy/MM/dd") & "' And Date <= '" & pmaxd.ToString("yyyy/MM/dd") & "' ")
                If SQL.recordcount = 0 Then
                    MsgBox("No records available for this selection, please reselect", vbExclamation + vbOKOnly, "Message Alert")
                    Chart1.Series(0).Points.Clear()
                    Exit Sub
                End If
                SQL.SQLDA.Fill(SQL.SQLDataSet, "qry_OEE_DateRange")
                Chart1.DataSource = SQL.SQLDataSet.Tables("qry_OEE_DateRange")
                Dim Series1 As Series = Chart1.Series(0)
                Series1.Name = "Date Range OEE"
                Chart1.Series(Series1.Name).XValueMember = "Date"
                Chart1.Series(Series1.Name).YValueMembers = "OEE"
                Chart1.Series(0).CustomProperties = "DrawingStyle = Cylinder ,PixelPointWidth = 15"
                Chart1.ChartAreas(0).AxisX.Title = "Date"
                Chart1.ChartAreas(0).AxisY.Title = "% OEE"
                'Chart1.ChartAreas(0).AxisX.Maximum =
                'Chart1.ChartAreas(0).AxisX.Minimum =
                Chart1.ChartAreas(0).AxisY.Minimum = 0
                Chart1.ChartAreas(0).AxisY.Maximum = 120
                'Chart1.ChartAreas(0).AxisX.Interval =
                Chart1.ChartAreas(0).AxisY.Interval = 20
                Chart1.ChartAreas(0).AxisX.TitleFont = New Font("Arial", 10, FontStyle.Bold)
                Chart1.ChartAreas(0).AxisY.TitleFont = New Font("Arial", 10, FontStyle.Bold)
                Chart1.ChartAreas(0).AxisX.LabelStyle.Font = New Font("Arial", 6)
                Chart1.ChartAreas(0).AxisY.LabelStyle.Font = New Font("Arial", 6)
                Chart1.ChartAreas(0).AxisX.MajorGrid.LineDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
                Chart1.ChartAreas(0).AxisY.MajorGrid.LineDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
                Chart1.ChartAreas(0).AxisX.LabelStyle.Angle = 0
                Chart1.Series(0).XValueType = ChartValueType.DateTime
                Exit Sub
            Else
                MsgBox("Please make another selection", vbExclamation + vbOKOnly, "Message")
                datStart.Visible = False
                datEnd.Visible = False
                labDateFilter.Visible = False
                labTo.Visible = False
                SQL.SQLDataSet.Clear()
                Chart1.Series(0).Points.Clear()
                cboPeriodSelector.SelectedIndex = -1
                cboPeriodSelector.Focus()
                Exit Sub
            End If
        Catch ex As SqlException
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Thanks for the help so far.
Hi SweetingA;

I have looked at your new code. My first suggestion would be to move the code in datEnd_ValueChanged to the datEnd_Leave event handler. The reason for this as I stated in my last post is that the ValueChanged event handler is called multiple times and will cause multiple calls to the SQL server and sometimes with invalid date range values. Doing it in the Leave event will give better results seeming it gets called once. The only thing with this is that the Leave event gets called once the user moves focus away from the datEnd control.

Another thing I would suggest to you doing is moving the line of code at the top of the handler, datEnd.Format = DateTimePickerFormat.Long, to the datEnd_Enter. This way it is set up long before the user starts to modify the value of the DateTimePicker.
Hi Fernando,

I can't find any LeaveEvent handler?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Fernando,

I tried it and the date picker works better in the datEnd but is still not perfect, problem is that the graph does not refresh as i don't really leave the combobox - thats why the valuechanged was a better option
Hi Fernando,

I found this code which works a treat.....

Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DateTimePicker1.ValueChanged
  Me.Text = DateTimePicker1.Value.ToString
End Sub

Private Sub DateTimePicker1_DropDown(ByVal sender As Object, ByVal e As EventArgs) Handles DateTimePicker1.DropDown
  RemoveHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
End Sub

Private Sub DateTimePicker1_CloseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.CloseUp
  AddHandler DateTimePicker1.ValueChanged, AddressOf DateTimePicker1_ValueChanged
  Call DateTimePicker1_ValueChanged(sender, EventArgs.Empty)
End Sub

Regards
Alan
I've requested that this question be closed as follows:

Accepted answer: 0 points for SweetingA's comment #a40239605

for the following reason:

The solution is signifcantly better than my original code and gave me a good understanding of the restrictions surrounding the use of the date picker.
The solution is signifcantly better than my original code and gave me a good understanding of the restrictions surrounding the use of the date picker.