我被困住了。我的应用程序具有这种“标准表单样式”。我有一个功能,使表单看起来标准。在这个函数中,它可以生成各种对象。例如,小组:
然后该函数为面板悬停时添加一个事件处理程序:
' Add drag panel
Dim panel_drag As New Panel
With panel_drag
.Size = New Size(this.size.Width, 40)
.Location = New Point(0, 0)
.BackColor = var_global.color_base
End With
AddHandler panel_drag.MouseEnter, AddressOf drag_panel_MouseSwitch
AddHandler panel_drag.MouseLeave, AddressOf drag_panel_MouseSwitch
this.Controls.Add(panel_drag)
'this'是调用函数的形式。
Public Shared Sub drag_panel_MouseSwitch(ByVal sender As Object, ByVal e As System.EventArgs)
drag_panel_switch(sender)
End Sub
然后是drag_panel_switch函数:
Public Shared Sub drag_panel_switch(ByVal this As Object, ByVal sender As Object)
If (this.BackColor = var_global.color_base) Then
this.BackColor = var_global.color_base_dark
<form>.cross_exit.BackColor = var_global.color_base_dark
Else
this.BackColor = var_global.color_base
<form>.cross_exit.BackColor = var_global.color_base
End If
End Sub
现在'form'必须是drag_panel所在的表单。如何获取此表单?
编辑:
我发现问题并不完全清楚。在应用程序中,多个表单具有相同的drag_panel。例如,当我将鼠标悬停在'form 1'中的drag_panel上时,该函数如何知道它是'form 1'而不是'form 2'。或者:如何将<form>
变为form_1
(或form_2
)
问题在于我使用的是全局Sub。在这个全局函数中,它为每个调用此函数的表单创建一个drag_panel。当我悬停drag_panel时,会调用drag_panel_switch。因为drag_panel_switch也是全局Sub我不能使用Me。我需要另一种方法来引用drag_panel悬停在其中的表单。 提前谢谢,
Luud Janssen
P.S。我真的尽力找到答案......
答案 0 :(得分:3)
发件人是鼠标事件中的面板,您可以使用它来获取父表单。
Public Shared Sub drag_panel_MouseSwitch(ByVal sender As Object, ByVal e As System.EventArgs)
dim panel as panel = ctype(sender, panel)
drag_panel_switch(panel)
End Sub
Public Shared Sub drag_panel_switch(ByVal this As panel)
Dim myForm as BaseForm = CType(this.FindForm(), BaseForm)
' Use this if you are using the interface
'Dim myForm as IBaseForm = CType(this.FindForm(), IBaseForm)
If (this.BackColor = var_global.color_base) Then
this.BackColor = var_global.color_base_dark
myForm.cross_exit.BackColor = var_global.color_base_dark
Else
this.BackColor = var_global.color_base
myForm.cross_exit.BackColor = var_global.color_base
End If
End Sub
您需要为所有表单实现接口或基类,以便可以在drag_panel_switch中访问您的属性。
Public Class Form1
Inherits BaseForm
End Class
Public Class Form2
Inherits BaseForm
End Class
Public Class BaseForm
Inherits System.Windows.Forms.Form
Public Property cross_exit As PictureBox
End Class
或界面
Public Class Form1
Implements IBaseForm
Public Property cross_exit As PictureBox
End Class
Public Class Form2
Inherits IBaseForm
Public Property cross_exit As PictureBox
End Class
Public Interface IBaseForm
Property cross_exit As PictureBox
End Interface