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

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).

Related

Two items maintaining focus

I am needing to find away to have two different items to maintain focus to fire off the textbox.KeyPress event. I have a textbox that when gains focus it pops up a form for a numeric keypad that fires off sendkey commands that I need to get read by the textbox.KeyPress event to fill the textbox
This is on a popup form that has buttons to click
Private Sub Number1_Click(sender As System.Object, e As System.EventArgs) Handles Number1.Click
SendKeys.Send("1")
End Sub
This is on a different form that I need the sendkeys.send to populate
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
'Code goes here
End Sub
Before calling SendKeys, set the focus on the textbox that will get the key. You will need an instance variable of the form that the textbox is on. This variable must be in scope of the form that you are sending the key from. If the other form is launched from the same form that the SendKeys statement is executed, then a class member-level variable can be used. Otherwise, use a global variable declared in a module.
'Declaration
Public frm2 as Form2
'Show Form2
frm2 = new Form2()
frm2.Show()
'Click handler
Private Sub Number1_Click(sender As System.Object, e As System.EventArgs) Handles Number1.Click
frm2.TextBox.Focus()
Application.DoEvents() 'Sometimes needed
SendKeys.Send("1")
End Sub

How to utilize the button click event from all buttons to call a function?

Let me explain further. Let's say I have 20 buttons on a form, and in the all of the button's click event I want to call a specific function, instead of placing the calling to that function in each click event, is there a way to call it from any of the click events without having to place the code in each click event?
Hope that makes sense.
Yes, just add the click event for each button to the end of the Handles clause.
Private Sub My_Sub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
' do stuff
End Sub
If you have a lot of buttons or a dynamic number of buttons, you could use a recursive method to add the same event handler for each. This will take care of all buttons, even those inside group boxes or other containers.
First, create the method you want each button click to call.
Private Sub bt_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'
' Your code goes here
'
End Sub
Second, create a method to recursively find all the button controls and add the event handler.
Private Sub AddEventHandler(RootControl As Control)
For Each c As Control In RootControl
If c.HasChildren Then
AddEventHandler(c)
End If
If TypeOf c Is Button Then
AddHandler c.Click, AddressOf bt_ButtonClick
End If
Next
End Sub
Lastly, in your form load event, add this line:
AddEventHandler(Me)
Wire the one sub to the click event for every button.
Private Sub AnyButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
End Sub

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

dateTimePicker ValueChange

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

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