Hi,
I'm programming a windows form application, I'm having a problem in implementing a code to my user controls,
let's say, all my forms inherit from my base class and in my base class I have the code below,
Public Class BaseForm
Inherits System.Windows.Forms.Form
Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
MyBase.OnVisibleChanged(e)
If Me.Visible Then
For Each myControl As Control In Me.Controls
If myControl.GetType Is GetType(Button) Then
AddHandler myControl.Click, AddressOf TestModul.ClickHandler
End If
Next
Else
For Each myControl As Control In Me.Controls
If myControl.GetType Is GetType(Button) Then
RemoveHandler myControl.Click, AddressOf TestModul.ClickHandler
End If
Next
End If
End Sub
End Class
The test module is as follows:
Module TestModul
Public Sub ClickHandler(sender As Object, e As System.EventArgs)
Dim mySender As Control = sender
MessageBox.Show(mySender.Name + vbCrLf + mySender.Parent.Name)
If (mySender.Parent.GetType().ToString().Contains("UserControl")) Then
MessageBox.Show("Its a user control")
End If
End Sub
End Module
So far everything is working properly, but the problem is with implementing the code to my usercontrols which all inherit from my baseusercontrol which is written below:
Public Class BaseUserControl
Inherits System.Windows.Forms.UserControl
Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
MyBase.OnVisibleChanged(e)
If Me.Visible Then
For Each myControl As Control In Me.Controls
If myControl.GetType Is GetType(Button) Then
AddHandler myControl.Click, AddressOf TestModul.ClickHandler
End If
Next
Else
For Each myControl As Control In Me.Controls
If myControl.GetType Is GetType(Button) Then
RemoveHandler myControl.Click, AddressOf TestModul.ClickHandler
End If
Next
End If
End Sub
End Class
this procedure must do as follows, anytime a button is pressed it must show me a messagebox and display the name of that button and the form which contains the button.
What I have tried:
as is traced it it does not enter this condition,
If myControl.GetType Is GetType(Button) Then
and see's it as a false condition. I removed the If statement and saw that again it doesn't read the line of code
AddHandler myControl.Click, AddressOf TestModul.ClickHandler
so again the change will not be done on the buttons.
Well in on of my forms it's working, because I opened it and manually removed the line of inheritence after the name of those classes, I just removed the line ' Inherits baseusercontrol' and rewrote it. then it worked.
But I shouldn't do this for all other classes. What would be the automated way to that?
In advance thank you for your help.