Force Custom Event Handler First - vb.net

When I create custom handlers like:
Public Class MyCustomClass
Public Sub AddHandlers()
AddHandler Form1.MouseMove, AddressOf MoveMouse
End Sub
Private Sub MoveMouse(sender As Object, e As MouseEventArgs)
MsgBox("Needs to happen first.")
End Sub
End Class
I need MoveMouse in this class to fire before any other event when the user moves their mouse over Form1.
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MsgBox("Needs to happen second.")
End Sub
While writing this, I realized I could create yet another custom event handler in Form1's class, but is there any other way to ensure that MoveMouse (regardless of what class it is in) happens before Form1_MouseMove?
Thanks-
~Nic

Events are fired in the order in which they are declared:
So if you want your custom class to raise MouseMove on Form1 before Form1 raises the event you need to make your custom class add the handler first:
Public Class CustomClass
Public Sub OnMouseMoved(sender As Object, e As MouseEventArgs)
Console.WriteLine("Custom mouse moved")
End Sub
End Class
Public Class Form1
Public Custom As CustomClass
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Custom = New CustomClass
AddHandler MouseMove, AddressOf Custom.OnMouseMoved
AddHandler MouseMove, AddressOf OnMouseMoved
End Sub
Private Sub OnMouseMoved(sender As Object, e As MouseEventArgs)
Console.WriteLine("Form1 mouse moved")
End Sub
End Class

Related

Switching forms in vb.net

I am a volunteer for the National Park Service trying to convert an interactive display originally created 20 years ago in a language called ToolBook into Visual Basic. The program consists of several projects under a single solution. The starting project, called "MainMenu", can be thought of as a library, with buttons that bring up “books.” The project called Geology is an example “book” and GeologyMenu can be thought of as the index of a book. The buttons on GeologyMenu connect to “chapters” that explain and show examples of geologic processes in the park. The “chapters” are within the project “Geology” and work fine within the project. All forms used in the program have timers that allow the program to re-set itself to MainMenu when not in use.
In a previous post, with the help of Idle Mind (thank you again), the following code for works fine for going from MainMenu to GeologyMenu and in the reverse direction as long as no button is pushed on GeologyMenu. However, if I go to a “chapter” I can no longer get back to the MainMenu from the GeologyMenu. Here is the relevant code:
MainMenu
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
TimerMain.Stop()
formNew.Show()
Me.Hide()
End Sub
Private Sub formNew_FormClosed(Sender As Object, e As FormClosedEventArgs)
lblTime.Text = 8
TimerMain.Start()
Me.Show()
End Sub
GeologyMenu
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblTime.Text = 6
TimerGeologyMenu.Enabled = True
Me.Show()
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
The code below for takes the viewer to the Erosion “chapter”
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
Erosion “Chapter” . This is the code for the button on every form in Erosion that takes the program back to GeologyMenu
Public Class frmErosionP02
Private Sub BtnGeologyMenu_Click(sender As Object, e As EventArgs) Handles btnGeologyMenu.Click
My.Computer.Audio.Stop()
frmGeologyMenu.lblTime.Text = 10
frmGeologyMenu.TimerGeologyMenu.Enabled = True
frmGeologyMenu.Show()
Me.Close()
End Sub
The code for forms within Erosion takes me back to GeologyMenu, but then MainMenu won’t show when I close GeologyMenu and I don’t understand why or how to fix it. Thank you in advance for your help!
Simply, pass the previous menu/Form to the new one in a parameterized constructor and keep it in a class variable, then handle the Form.Closed event of the new menu to show the previous one.
Example for the relevant code:
Public Class frmMainMenu
Inherits Form
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New frmGeologyMenu(Me)
Me.Hide()
formNew.Show()
End Sub
End Class
Public Class frmGeologyMenu
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
Dim frmErosion As New frmErosionP02(Me)
Me.Hide()
frmErosion.Show()
End Sub
End Class
Public Class frmErosionP02
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Public Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
End Class

How to check if a Form has focus?

I have two WinForms. Let's say MainForm and ChildForm.
What I'm trying to make is when the MainForm is activated the ChildForm should always be visible, and when the MainForm loses focus the ChildForm should be hidden except if it's the ChildForm which was activated.
Here is my code :
AddHandler Me.MainForm.Activated, Sub()
Me.ChildForm.Show()
End Sub
AddHandler Me.MainForm.Deactivate, Sub()
If Not Me.ChildForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
AddHandler Me.ChildForm.Deactivate, Sub()
If Not MainForm.Focused Then
Me.ChildForm.Hide()
End If
End Sub
The code doesn't work. Basically when I click on a certain form (on the child form for example), the property Me.ChildForm.Focused is not correct and then ChildForm is hidden while it should be visible.
Can anyone know how to achieve that please ?
Not sure if this is a good one, but the idea is using flags for MainForm and ChildForm active status. These flags are set when MainForm and ChildForm's Activated and Deactive events are fired. For simplicity, I'll use a module to store the flags and the instance of ChildForm. Then call SetChildVisible() method in MainForm, Form1 and Form2 Activated event. Set startup object to MainForm.
Module1
Module Module1
Public FChild As ChildForm
Public FMainActive As Boolean
Public FChildActive As Boolean
Public Sub SetChildVisible()
FChild.Visible = Not FChildActive And FMainActive
End Sub
End Module
MainForm
Public Class MainForm
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim f1 = New Form1
Dim f2 = New Form2
Dim child = New ChildForm
Module1.FChild = child
child.Show()
f1.Show()
f2.Show()
End Sub
Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FMainActive = True
Module1.SetChildVisible()
End Sub
Private Sub MainForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FMainActive = False
End Sub
End Class
ChildForm
Public Class ChildForm
Private Sub ChildForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.FChildActive = True
End Sub
Private Sub ChildForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
Module1.FChildActive = False
End Sub
End Class
Form1
Public Class Form1
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class
Form2
Public Class Form2
Private Sub Form2_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Module1.SetChildVisible()
End Sub
End Class

How to use array when declaring sub VB

I was wondering is there was a way to use an array when declaring a sub.
Example:
Public Class Form1
Private PictureBox(2) as PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox(0) = PictureBox1
PictureBox(1) = PictureBox2
PictureBox(2) = PictureBox3
Private Sub PictureBox_mouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox(1).MouseDown
'do stuff here
end sub
This is the method Plutonix suggested in the comments applied to your code. Note that no array is needed.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is PictureBox Then
AddHandler DirectCast(ctrl, PictureBox).MouseDown, AddressOf PictureBox_MouseDown
End If
Next
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
'Code here will apply to every PictureBox in your form
End Sub
End Class
Also note that instead of DirectCast(ctrl, PictureBox).MouseDown, you could just use ctrl.MouseDown. Using DirectCast simply helps you avoid making mistakes by cluing IntelliSense and the compiler in on what you're doing.

VB.NET UserControl does not receive parent form's events

I have created a usercontrol and added it to a form.
I would like to receive the form's event using Private WithEvents _Parent As Form.
But none of the events is being received.
The entire code of my usercontrol is attached.
Does anybody see what I am doing wrong?
Public Class UserControl1
Private WithEvents _Parent As Form
Public Sub New()
InitializeComponent()
_Parent = Me.Parent
End Sub
Private Sub _Parent_Activated(sender As Object, e As EventArgs) Handles _Parent.Activated
MsgBox("activated")
End Sub
Private Sub _Parent_Resize(sender As Object, e As EventArgs) Handles _Parent.Resize
MsgBox("resize")
End Sub
End Class
When the constructor is called, there is no parent yet (Windows Forms controls are added to their parents after the class is created). Me.Parent returns Nothing at this point.
Handle Me.ParentChanged to initialize _Parent:
Private Sub UserControl1_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
_Parent = Me.Parent
End Sub

My event handler is never called because the original event is raised in another event handler?

The event handlers in my parent class are never called though the events are raised in the child class.
The Code:
Public Class childForm
Public Event checkboxchangedEvent(ByVal checkbox1 As Boolean, ByVal checkbox2 As Boolean)
Private Sub checkboxchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
RaiseEvent checkboxchangedEvent(CheckBox1.Checked, CheckBox2.Checked)
End Sub
End Class
Public Class ParentForm
Friend WithEvents cf As childform = New childform
Private Sub cf_checkboxchanged(ByVal checkbox1 As Boolean, ByVal checkbox2 As Boolean) Handles cf.checkboxchangedEvent
My.Settings.checkbox1 = checkbox1
My.Settings.checkbox2 = checkbox2
End Sub
End Class
You could try it like this as an alternative method of setting up the event handler,
Public Sub ShowChildForm()
Dim frm as New childform
AddHandler frm.checkboxchangedevent, AddressOf cf_checkboxchanged
frm.ShowDialog()
RemoveHandler frm.checkboxchangedevent, AddressOf cf_checkboxchanged
End Sub
I think your issue is timing. Try explicitly creating the form and adding the handlers in ParentForm's Load event handler. (This is all typed from memory and untested so event names/signatures for the Load event may be incorrect.)
Public Class ParentForm
Friend WithEvents cf As childform
Private Sub cf_checkboxchanged(ByVal checkbox1 As Boolean, ByVal checkbox2 As Boolean)
My.Settings.checkbox1 = checkbox1
My.Settings.checkbox2 = checkbox2
End Sub
Private Sub ParentForm_Load(ByVal sender as object, ByVal e as EventArgs) Handles Me.Load
cf = new childForm()
AddHandler cf.checkboxchanged, AddressOf cf_checkboxchanged
cf.Show()
End Sub
End Class