dateTimePicker ValueChange - vb.net

I have a DateTimePicker and every time I want the event dtpJournalDate.ValueChanged is being called.
My problem is that, when I click the dropdown arrow, the event dtpJournalDate.ValueChange is already executing even if I have not chosen a new date yet and after I have chosen a date, it would again execute the ValueChange event. I only want the event ValueChange execute after I have chosen a date?
Private Sub dtpJournalDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpParametersDate.ValueChanged
'Some Codes to do when changing the date
End Sub

Use CloseUp event handler as #Damith mentioned
Private Sub dtpJournalDate_CloseUp(sender As Object, e As EventArgs) Handles dtpParametersDate.CloseUp
'Some Codes to do when changing the date
End Sub
But this event will not firing if user input date information manually(writing straight to DateTimePicker's textbox.
Then you can add KeyPress event handler and run your code after user press Enter-button:
Private Sub dtpJournalDate_KeyPress(sender As Object, e As KeyPressEventArgs) Handles dtpParametersDate.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
'Run your code with new date information
End If
End Sub

Or you can change event handler in onCloseUp event
Private Sub MyDatetimePicker_ValueChanged(sender As Object, e As EventArgs) Handles Me.ValueChanged
Me.OnCloseUp(e)
'Do what ever change you want, when instance is created.
End Sub

Related

Is there a Form event like "Shown" but runs the code every time it is displayed instead of the just first time?

Just noticed in one of my programs that the form event Shown only runs the code when the form is first displayed. Every other time after that it does not run the code. Is there a form event that does what shown does, but every time it is displayed instead of just the first? Or is there a way to get around it? This is the exact same for the Load event too.
Thanks heaps, appreciate any response.
I just tested with this in Form1:
Private f2 As New Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
f2.Show()
End Sub
and this in Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Form2_Load")
End Sub
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Visible Then
MessageBox.Show("Form2_VisibleChanged")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
End Sub
and it worked exactly as expected. Every time I clicked Button1 in Form1, Form2 was displayed. The first time I saw messages for "Form2_Load"and "Form2_VisibleChanged" and on subsequent occasions on for "Form2_VisibleChanged".

Use timer to trigger an event in program at specific time

I have my timer code displaying the time correctly on my program, and I want to trigger an event to happen at a 6:31. How do I get the program to preform a button click or trigger the event that the button handles. The program works when I run and press the button, but I want it to happen automatically at 6.31 without me pressing it. Thanks!
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Date.Now.ToString("hh:mm:ss")
End Sub
In your Tick event:
Dim now As DateTime = DateTime.Now
If (18 = now.Hour) AndAlso (31 = now.Minute) Then
' Change 18 to 6 is you want AM instead of PM
' Do something here
End If
This is the naive version. Your question is lacking in details so I did not elaborate on the code.
At a minimum you may need to ensure that once the "event" happens it can't happen again.

Button.Click event doesn't fire if datagridview is in editmode

I have a datagridView (unbound for several reasons).
When the user changes something I am saving the new value in a tempTable
Private Sub fgFields_AfterEdit(sender As Object, e As C1.Win.C1FlexGrid.RowColEventArgs) Handles fgFields.AfterEdit
Cursor.Current = Cursors.WaitCursor
SaveField(e.Row)
Cursor.Current = Cursors.Default
End Sub
I have also a button (btnSave) that saves the values from datagrid , among with other values to a database:
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
SaveFileDef("SAVE")
End Sub
My problem is that if I start editing the cell I need to click somewhere else to fire the after_edit. If the user click on btnSave the event After_Edit is fired but not the button.Click!!!!
So the code in the event btnSave.Click is not running.
Any ideas how to overcome this strange behaviour?
So .. you use ComponentOne Flexgrid ..
try this way on Event RowValidating
e.Cancel = True

Fire Validated Event on WinForms ComboBox

I have some logic being handled in the Validated event on a combobox, but would like it to trigger as soon as a selection is made. I can handle the SelectedIndexChanged event separately in order to fire the event, but I'm not sure how to invoke it.
Private Sub ComboSelectedIndexChanged(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
'Fire Validated Event
End Sub
Private Sub ComboValidated(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.Validated
'Do Something Here
End Sub
I'd rather not call into the method handling the validated event directly,
ComboValidated(Nothing, Nothing)
as it's not precisely what I'm trying to do with the code and it will run a second time when in fact the validated event actually does fire upon losing focus.
What code can I execute to fire the validated event (either by fire the event directly or by causing the control to lose focus)?
Why don't you make it as Sub ..
Private Sub ComboValidated(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.Validated
InCBValidated()
End Sub
Sub InCBValidated()
'Do Something Here
End Sub
So you just call InCBValidated()
I solved this by making the control lose focus which then fired all the neccessary events. The control happened to be in a groupbox so the easiest way to remove focus was just to give that control focus so the same place on the page was still selected
Private Sub ComboSelectedIndexChanged(sender As System.Object, e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Focused Then GroupBox1.Focus()
End Sub

VB.Net Mouse.Enter and Mouse.Leave both called in same event?

In my VB.Net forms application, I have a function:
Private Sub pnlMain_MouseLeave(sender As Object, e As System.EventArgs) Handles pnlMain.MouseLeave
...
End Sub
and another function:
Private Sub pnlMain_MouseEnter(sender As Object, e As System.EventArgs) Handles pnlMain.MouseEnter
...
End Sub
When the mouse enters or leaves both are executed - first Enter and then Leave. Why is this happening?
It doesn't or it shouldn't
but if you have a breakpoint on the enter event then it will fire the mouseleave event as you hit it. (because you've triggered the leave event).