提问人:MGC 提问时间:11/9/2023 最后编辑:LarsTechMGC 更新时间:11/9/2023 访问量:72
将表单名称传递给变量
Passing a form name to a variable
问:
有人可以告诉我如何将表单名称传递给执行命令的函数吗?
我有一个导航条,在 6 个表单上是相同的。当有人单击条带上的按钮时,无论窗体如何,都会执行相同的命令(启用或禁用条带上的按钮)。
我正在尝试做的是创建一个可以处理所有表单上的按钮的函数。
例如:这是单击编辑按钮时的调用语句:
Private Sub cmdEdit_Click(sender As Object, e As EventArgs) Handles cmdEdit.Click
'Edit Record - Button 6
'Stop
Dim OK As Boolean
IsEditing = True
OK = Button_Edit("Creditors")
'OK = Button_Edit(Creditors) - fail
End Sub
这是调用的函数:
Public Function Button_Edit(ByRef FrmName As Form) As Boolean
Stop
'With FrmName
FrmName.Controls("cmdFirst").Enabled = False
FrmName.Controls("cmdPrevious").Enabled = False
FrmName.Controls("cmdNext").Enabled = False
FrmName.Controls("cmdLast").Enabled = False
FrmName.Controls("cmdSearch").Enabled = False
FrmName.Controls("cmdEdit").Enabled = False
FrmName.Controls("cmdAdd").Enabled = False
FrmName.Controls("cmdDelete").Enabled = False
FrmName.Controls("cmdSave").Enabled = True
FrmName.Controls("cmdReject").Enabled = True
FrmName.Controls("cmdReports").Enabled = False
FrmName.Controls("cmdPrint").Enabled = False
FrmName.Controls("cmdExit").Enabled = True
'End With
Return True
End Function
任何建议将不胜感激。我几乎尝试了网上能找到的所有东西,但无济于事。
答:
这是一个伪代码,如何通过接口来做
interface IClicks
sub Help()
sub OK()
sub cancel()
end interface
class Form1 implements IClicks
sub Help()
' call help
end sub
sub OK()
' call ok
end sub
sub cancel()
' call cancel
end sub
end interface
class Strip inherits StripControl
sub ButtonHelp_OnClick(....)
dim f as IClicks = DirectCast(FindForm(), Iclicks)
f.Help()
end sub
sub ButtonOk_OnClick(....)
dim f as IClicks = DirectCast(FindForm(), Iclicks)
f.OK()
end subend interface
sub ButtonCancel_onClick(....)
dim f as IClicks = DirectCast(FindForm(), Iclicks)
f.Cancel()
end sub
end class
您可以向工具条按钮添加标签值,0 表示“保存”和“拒绝”,1 表示其他按钮。然后,将工具条传递给模块函数,并在此处设置每个按钮的 Enabled 属性。
' module Sub
Public Sub EnableDisableButtons(ts As ToolStrip, enable As Boolean)
For Each ctrl In ts.Controls
If TypeOf ctrl Is ToolStripButton Then
If ctrl.tag = 1 Then ' other buttons
CType(ctrl, ToolStripButton).Enabled = enable
Else ' save & reject buttons
CType(ctrl, ToolStripButton).Enabled = Not enable
End If
End If
Next
End Sub
在其中一个 ToolStripButton 的单击事件中,调用
Private Sub cmdEdit_Click(sender As Object, e As EventArgs) Handles cmdEdit.Click
' False = Save and Reject will be enabled, others will be disabled
EnableDisableButtons(<name of toolstrip in form>, False)
End Sub
它可能有点片状,但 a 可能是你所追求的。UserControl
将 添加到您的项目中。这看起来像一个无边框的形式。将按钮添加到 UserControl(我在示例中添加了四个按钮 - 、)。然后添加启用/禁用代码。还要声明一些 - 这些将在托管表单中处理。请参阅下面的“UserControl1 代码”。UserControl
cmdEdit
cmdPrevious
cmdNext
cmdSave
Events
现在构建您的项目 - 这将在您的工具箱中可用。UserControl1
然后,您可以将工具箱中的 拖到 .然后,在窗体中,可以处理 UserControl1 事件以执行特定于窗体的操作。请参阅下面的“Form1 代码”。UserControl1
Form1
您可能会发现将 UserControl 放入 DLL 并引用它比将其与主代码混合更容易。请记住:您需要先构建 UserControl,然后才能在 IDE 中更新其事件等!
UserControl1 代码
Public Class UserControl1
Public Event EditClick(sender As Object, e As EventArgs)
Public Event PreviousClick(sender As Object, e As EventArgs)
Public Event NextClick(sender As Object, e As EventArgs)
Public Event SaveClick(sender As Object, e As EventArgs)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
cmdEdit.Enabled = True
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdSave.Enabled = False
End Sub
Private Sub cmdEdit_Click(sender As Object, e As EventArgs) Handles cmdEdit.Click
cmdEdit.Enabled = False
cmdPrevious.Enabled = False
cmdNext.Enabled = False
cmdSave.Enabled = True
RaiseEvent EditClick(sender, e)
End Sub
Private Sub cmdPrevious_Click(sender As Object, e As EventArgs) Handles cmdPrevious.Click
cmdEdit.Enabled = True
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdSave.Enabled = False
RaiseEvent PreviousClick(sender, e)
End Sub
Private Sub cmdNext_Click(sender As Object, e As EventArgs) Handles cmdNext.Click
cmdEdit.Enabled = True
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdSave.Enabled = False
RaiseEvent NextClick(sender, e)
End Sub
Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
cmdEdit.Enabled = True
cmdPrevious.Enabled = True
cmdNext.Enabled = True
cmdSave.Enabled = False
RaiseEvent SaveClick(sender, e)
End Sub
End Class
Form1 代码
Public Class Form1
Private Sub UserControl11_EditClick(sender As Object, e As EventArgs) Handles UserControl11.EditClick
MessageBox.Show("Editing")
End Sub
Private Sub UserControl11_NextClick(sender As Object, e As EventArgs) Handles UserControl11.NextClick
MessageBox.Show("Next")
End Sub
Private Sub UserControl11_PreviousClick(sender As Object, e As EventArgs) Handles UserControl11.PreviousClick
MessageBox.Show("Previous")
End Sub
Private Sub UserControl11_SaveClick(sender As Object, e As EventArgs) Handles UserControl11.SaveClick
MessageBox.Show("Record saved")
End Sub
End Class
评论
cmdFirst.Enabled = False
Buttons
Form
Buttons