Inheritance works for first descendant but not next. Why? - vb.net

Form1
Public Class Form1
Private Sub But_Bell_Click(sender As System.Object, e As System.EventArgs) Handles But_Bell.Click
MessageBox.Show("Ding a ling")
End Sub
End Class
First Decendant
Public Class BellsAndWhistles
Inherits Form1
Friend WithEvents But_Whistle As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.But_Whistle = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'But_Whistle
'
Me.But_Whistle.Location = New System.Drawing.Point(112, 38)
Me.But_Whistle.Name = "But_Whistle"
Me.But_Whistle.Size = New System.Drawing.Size(75, 23)
Me.But_Whistle.TabIndex = 1
Me.But_Whistle.Text = "Whistle"
Me.But_Whistle.UseVisualStyleBackColor = True
'
'BellsAndWhistles
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.But_Whistle)
Me.Name = "BellsAndWhistles"
Me.Text = "Bells & Whistles"
Me.Controls.SetChildIndex(Me.But_Whistle, 0)
Me.ResumeLayout(False)
End Sub
Private Sub But_Whistle_Click(sender As System.Object, e As System.EventArgs) Handles But_Whistle.Click
MessageBox.Show("Toot Toot")
End Sub
End Class
Second Descendant
Public Class MoreBellsAndWhistles
Inherits BellsAndWhistles
Friend WithEvents MoreBells As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.MoreBells = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'MoreBells
'
Me.MoreBells.Location = New System.Drawing.Point(30, 145)
Me.MoreBells.Name = "MoreBells"
Me.MoreBells.Size = New System.Drawing.Size(75, 23)
Me.MoreBells.TabIndex = 1
Me.MoreBells.Text = "More Bells"
Me.MoreBells.UseVisualStyleBackColor = True
'
'MoreBellsAndWhistles
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.MoreBells)
Me.Name = "MoreBellsAndWhistles"
Me.Text = "MoreBellsAndWhistles"
Me.Controls.SetChildIndex(Me.MoreBells, 0)
Me.ResumeLayout(False)
End Sub
Private Sub MoreBells_Click(sender As System.Object, e As System.EventArgs) Handles MoreBells.Click
MessageBox.Show("Ting TIng")
End Sub
Private Sub MoreBellsAndWhistles_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Where has the whistle button gone?
The class part of the inheritance has works because you can access it via code.

Try calling MyBase.InitializeComponent() from the second descendant. You'll probably have to change the access level of it, too.
EDIT
This was bugging me all night. It turns out that its a case of missing constructors. If you use reflector you'll see that Form1 has a constructor that calls Me.InitializeComponent() even though that doesn't exist in either Form1.vb or Form1.Designer.vb.
<DesignerGenerated> _
Public Class Form1
Inherits Form
' Methods
Public Sub New()
Me.InitializeComponent
End Sub
...
End Class
If you create a C# WinForms app the constructor is visible so this makes me think its a VB thing to hide it. Also, if you manually add a Sub New to Form1 it will fill in some code for you, essentially "unhiding it".
I'm guessing that VS looked at your code and realized that it was a descendant of System.Windows.Forms.Form but technically it was done improperly since it didn't call MyBase.New() (since it couldn't because it didn't exist) so it was just trying to guess. It "knows" to add a call to InitializeComponent() in the form that it created and it "knows" to do that for the form that you're looking at but it isn't bothering to walk the chain of forms and do it for all of them. Bug? Maybe.
When you set MoreBellsAndWhistles as a startup form you never see either of the new buttons, right? This is how you can tell its more of a VS trickery involved.
Anyway, the solution to the entire problem is to add this to both sub classes:
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
And add this to Form1:
Public Sub New()
InitializeComponent()
End Sub

Related

How to Trigger button click from another form VB Net

I have three forms in total and i want to trigger one of the button on form 3 to be triggered automatically when form 1 loaded
Form 1
Public Class frmIOMain
' In This Form load I want to trigger the above mentioned button
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' I want to Trigger the Above mentioned button here when my form is loaded
' But it is not working for me
frmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
End Sub
End Class
Form 2
Public Class TestEquipmentManagement
Public EquipementTable As New DataTable("EquipmentTable")
Public EquiTypeSelection As String
Public EquiManufacturerSelection As String
Public EquiList_PK As New List(Of Integer)
Dim targetEquipmentList As New List(Of Model.equipment)
Private equipDB As Model.Entities = Nothing
Public Shared viewManager As ViewManager
Private equipment As New List(Of Model.equipment)
'Dim WithEvents excNewPFM As New VBACom
Public EquipCalTable As New DataTable("EquipCalTable")
Public Sub New()
Dim todayplusoneyear As Date
todayplusoneyear = Date.Today
todayplusoneyear = todayplusoneyear.AddYears(1)
'Assign current db
equipDB = frmIOMain.db
End Sub
End Class
Form 3
Public Class frmUpdateDueDates
Private EquipmentUpdates As UpdateCalibrationsViewModel
Private _success As Boolean = False
Public Sub New(db As Entities)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
EquipmentUpdates = New UpdateCalibrationsViewModel(db, New CAQ23(), False)
'Add Handlers
AddHandler EquipmentUpdates.OnProgressChanged, AddressOf progressChangedHandler
AddHandler EquipmentUpdates.OnInfotextChanged, AddressOf infoTextChangedHandler
prgUpdates.Maximum = EquipmentUpdates.intProgressMax
End Sub
Public Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
End Class
I want "cmdUpdate_Click" Button which is on form 3 to be triggered when my form 1 is loaded
Can Anyone tell me how i can do that?
Firstly, create an instance of the form, instead of using its default form instance. Calling a click handler across forms isn't a good idea. The handler may use the arguments sender As Object, e As EventArgs and from outside of the containing class, you can't assume you know that. Better practice would be to create a method which performs the click within the form, such as
Public Class frmUpdateDueDates
Public Sub cmdUpdateClick()
cmdUpdate.PerformClick()
End Sub
Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
End Class
Public Class frmIOMain
Private myFrmUpdateDueDates As frmUpdateDueDates
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myFrmUpdateDueDates = New FrmUpdateDueDates()
myFrmUpdateDueDates.Show()
'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
myFrmUpdateDueDates.cmdUpdateClick()
End Sub
End Class
And you can change the access modifier of the click handler back to Private
Even better would be to put the work into a different method which the click handler calls. Then the other form doesn't even need to know the button exists. Such as
Public Class frmUpdateDueDates
Public Sub DoUpdating()
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
DoUpdating()
End Sub
End Class
Public Class frmIOMain
Private myFrmUpdateDueDates As frmUpdateDueDates
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myFrmUpdateDueDates = New FrmUpdateDueDates()
myFrmUpdateDueDates.Show()
'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
myFrmUpdateDueDates.DoUpdating()
End Sub
End Class

Menu Item Custom Control Events

I am trying to create a menu list item that contains both a textbox and a label as a single item. In the code below I have made the necessary custom control class inherited from ToolStripControlHost and this looks and behaves as expected when created in the form menu.
The problem I am having is that the control's events are not firing the handler routine. In the example below, what I would expect to happen is that when the user types into the text box a message should show (other events have the same problem).
Thank you.
Control Classes:
Public Class ToolStripTextBoxWithLabel
Inherits ToolStripControlHost
Public Sub New(Optional ByVal lblText As String = "label")
MyBase.New(New ControlPanel(lblText))
End Sub
Public ReadOnly Property ControlPanelControl() As ControlPanel
Get
Return CType(Me.Control, ControlPanel)
End Get
End Property
End Class
Public Class ControlPanel
Inherits Panel
Friend WithEvents txt As New TextBox
Friend WithEvents lbl As New Label
Public Sub New(ByVal lblText As String)
Me.Height = 20
lbl.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Bottom
lbl.Text = lblText
lbl.TextAlign = ContentAlignment.BottomLeft
lbl.AutoSize = True
lbl.Height = Me.Height
lbl.Location = New Point(0, 3)
lbl.Parent = Me
txt.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
txt.Location = New Point(lbl.Right + 3, 0)
txt.Width = Me.Width - txt.Left
txt.Parent = Me
End Sub
End Class
Form Implementation:
Public Class Form1
Friend tb_SearchBox As ToolStripTextBoxWithLabel
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
tb_SearchBox = New ToolStripTextBoxWithLabel("Search:") With {.Name = "tb_SearchBox"}
AddHandler tb_SearchBox.TextChanged, AddressOf tb_SearchBox_TextChanged
Item1ToolStripMenuItem.DropDownItems.Add(tb_SearchBox)
End Sub
Private Sub tb_SearchBox_TextChanged(sender As Object, e As EventArgs)
MsgBox("Success")
End Sub
End Class
Using the TextChanged event of your ToolStripTextBoxWithLabel in this instance is inappropriate because that event should only be raised when the Text property of that object changes, which is not happening here. You need to do what Plutonix suggested but you should also do it with your own custom event rather than with the TextChanged event of the host, e.g.
Public Event TextBoxTextChanged As EventHandler
Protected Overridable Sub OnTextBoxTextChanged(e As EventArgs)
RaiseEvent TextBoxTextChanged(Me, e)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
OnTextBoxTextChanged(EventArgs.Empty)
End Sub
Rather than deriving your ControlPanel class from Panel and creating the child controls in code, I would suggest that you create a user control and add the children in the designer. You would then use my answer below in two steps, i.e. the user control would handle the TextChanged event of the TextBox and then raise an event of its own that would, in turn, be handled by the ToolStripTextBoxWithLabel that would its own event.
Thanks to jmcilhinney and Plutonix I have put together the solution. For completeness and future community reference the full solution is below.
User Control:
Public Class CustomTextBox
Public Event TextBoxTextChanged As EventHandler
Protected Overridable Sub OnTextBoxTextChanged(e As EventArgs)
RaiseEvent TextBoxTextChanged(Me, e)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
OnTextBoxTextChanged(EventArgs.Empty)
End Sub
Public Sub New (lblText as string)
InitializeComponent()
Caption = lblText
End Sub
Public Property Caption() As String
Get
Return Label1.Text
End Get
Set(ByVal value As String)
Label1.Text = value
End Set
End Property
Public Overrides Property Text() As String
Get
Return TextBox1.Text
End Get
Set(ByVal value As String)
TextBox1.Text = value
End Set
End Property
Public Class
Implementation:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim SearchBox As New CustomTextBox("Search")
Dim host As ToolStripControlHost = new ToolStripControlHost(windowNewMenu)
AddHandler SearchBox.TextBoxTextChanged, AddressOf SearchBox_TextChanged
ToolStripMenuItem1.DropDownItems.Add(host)
End Sub
Private Sub SearchBox_TextChanged(sender As Object, e As EventArgs)
MsgBox(sender.Text)
End Sub

VB.NET Drag text, set focus and enter key issue

I have an app that allows for text to be dragged-dropped into a text box. I also have a check box that allows the app to always be on top of all windows. My issues is when I drag text into the text box and I hit the enter key, it does not run the function unless I have the window in actual focus (by clicking on it).
My question is, how can I make sure when I drag text into the text box, it will make the window be the focus so when I hit the enter key, it will run my function?
Here is what I am trying with no luck:
Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles Me.DragEnter
Me.Focus()
End Sub
Private Sub Form1_DragOver(sender As Object, e As DragEventArgs) Handles Me.DragOver
Me.Focus()
End Sub
I am posting the code as it works fine for me, try it on new project:
Public Class Form1
Private Sub TextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
' Drop text and move cursor to end of drag-dropped text
TextBox1.Text = e.Data.GetData(DataFormats.Text)
TextBox1.SelectionStart = TextBox1.Text.Length + 1
TextBox1.Focus()
Me.Activate()
End Sub
End Class
and designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.AllowDrop = True
Me.TextBox1.Location = New System.Drawing.Point(30, 54)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(216, 125)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TopMost = True
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
Try to use Me.Activate() instead in DragEnter it should be enough

Repairing VB.NET project

I throw in huge troubles with recreating a multproject solution into a single project solution where my program become unusable.
I have all code files saved and problem is with showing designer in IDE and errors connecting with that.
Situation:
All forms are subclassed with class called cls_transform which make form transparent while moving.
Public Class cls_transform
Inherits System.Windows.Forms.Form
Private _OpacityMove As Double = 0.5
Private _OpacityOriginal As Double = 1
Private Const WM_NCLBUTTONDOWN As Long = &HA1
Private Const WM_NCLBUTTONUP As Long = &HA0
Private Const WM_MOVING As Long = &H216
Private Const WM_SIZE As Long = &H5
Private Sub cls_transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
Static LButtonDown As Boolean
If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
LButtonDown = True
ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then
LButtonDown = False
End If
If LButtonDown Then
If CLng(m.Msg) = WM_MOVING Then
If Me.Opacity <> _OpacityMove Then
_OpacityOriginal = Me.Opacity
Me.Opacity = _OpacityMove
End If
End If
ElseIf Not LButtonDown Then
If Me.Opacity <> _OpacityOriginal Then Me.Opacity = _OpacityOriginal
End If
MyBase.DefWndProc(m)
End Sub
Public Property OpacityMove() As Double
Get
Return _OpacityMove
End Get
Set(ByVal Value As Double)
_OpacityMove = Value
End Set
End Property
Private Sub InitializeComponent()
Me.SuspendLayout()
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Name = "cls_transform"
Me.ResumeLayout(False)
End Sub
End Class
This is how code of one empty form "frm_myForm" look like:
Public Class frm_myForm
Inherits cls_transform
Private Sub frm_myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
And this is Designer code of that form:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frm_myForm
Inherits cls_transform
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'frm_myForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Name = "frm_myForm"
Me.Text = "frm_myForm"
Me.ResumeLayout(False)
End Sub
End Class
When I try to "View Designer" from IDE instead of to see a form I get white screen with error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: frm_myForm --- The base class 'noviprog.cls_transform' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
That happen with all forms which inherits from cls_transform.
When cls_transform was in separate library (separate project of same solution) which was referenced to actual project that works.
Is it possible to get this working when code files and that class would be in same project and how to get this working?

vb.net listen for parent form event on each child form

I am trying to implement a function on my parent form, that when the event fires, I want to perform actions on all of the child forms that are open. Because any given child form may or may not be open at a given time, I can't handle it directly from the event on the parent form: i.e., cant do the following as Child1 may not be initiated at the time:
--Parent Form--
Public Sub ParentEvent()
DoParentAction()
DoChild1Action()
DoChild2Action()
End Sub
Is there a way on each child page to listen for ParentEvent() to be fired? essentially, what I want to do is handle the ParentEvent() being fired, on the child page the same as if a button was clicked on the child page, something like this:
--Child1--
Public Sub ChildEvent() Handles ParentForm.DoParentAction()
DoChild1Action()
End Sub
This is easy to do, you just have to step around VB's WithEvents and Handles syntax to get at it.
Public Class ParentForm
Event OnDoSomething()
Private Sub DoSomething()
RaiseEvent OnDoSomething()
End Sub
End Class
and then
Public Class ChildForm
Public Sub New()
InitializeComponent()
AddHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
Private Sub DoSomething()
' do something
End Sub
Private Sub ChildForm_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
RemoveHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
End Class
It's important to always make sure the event handler is removed before disposing the child form (else you end up with a memory leak).
The above assumes you are using the VB default instance of ParentForm - if you're not, obviously you have to reference things accordingly. A better approach might be to make the parent an argument in the constructor like:
Public Sub New(ByVal parent as ParentForm)
InitializeComponent()
AddHandler parent.OnDoSomething, AddressOf DoSomething
End Sub
also, of course, modifying the RemoveHandler section as well (you'd need to keep a reference to the parent). Another option is to hook/unhook in the ParentChanged event if this is an MDI application.
The only other caveat is that you can't create any of the child forms in the constructor of the parent form since you end up with self-reference during construction.
Sure.
Add a public event to the parent form:
Public Event EventFired(ByVal timestamp As DateTime)
In each child form, add a handler:
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
When you create the child form, add a handler:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
You can use this approach whether you are using an MDI or simply free-floating windows.
FULL CODE
ParentForm Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ParentForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(166, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open Child 1"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 41)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(166, 23)
Me.Button2.TabIndex = 0
Me.Button2.Text = "Open Child 2"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 231)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(166, 23)
Me.Button3.TabIndex = 0
Me.Button3.Text = "Fire Event"
Me.Button3.UseVisualStyleBackColor = True
'
'ParentForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "ParentForm"
Me.Text = "ParentForm"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
End Class
ParentForm Code Behind
Public Class ParentForm
Public Event EventFired(ByVal timestamp As DateTime)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim l_child2 = New ChildForm2()
AddHandler Me.EventFired, AddressOf l_child2.ParentEventFired
l_child2.Show(Me)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
RaiseEvent EventFired(DateTime.Now)
End Sub
End Class
ChildForm1 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
'ChildForm1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm1"
Me.Text = "ChildForm1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm1 Code Behind
Public Class ChildForm1
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class
ChildForm2 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm2
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'ChildForm2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm2"
Me.Text = "ChildForm2"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm2 Code Behind
Public Class ChildForm2
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 2: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class