将表单名称传递给变量

Passing a form name to a variable

提问人:MGC 提问时间:11/9/2023 最后编辑:LarsTechMGC 更新时间:11/9/2023 访问量:72

问:

有人可以告诉我如何将表单名称传递给执行命令的函数吗?

我有一个导航条,在 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

任何建议将不胜感激。我几乎尝试了网上能找到的所有东西,但无济于事。

vb.net winforms

评论

0赞 T.S. 11/9/2023
很少有方法可以做到这一点。您可以在条带中提出事件并在表单中收听它们。或者让你的表单实现接口,传递给条带,然后在条带中调用接口方法
0赞 Idle_Mind 11/9/2023
代码应该与当前表单一起使用,还是修改不同的表单?每种表单类型一次只打开一个实例吗?我们需要更多细节来使代码有用。
0赞 MGC 11/9/2023
它(应该)与显示的当前表单一起工作。一次只显示一个窗体。包含按钮的导航控件在所有窗体中都是相同的。无论显示哪种形式,功能都是相同的。
1赞 ourmandave 11/9/2023
@MGC 如果您只打开了一个表单,并且导航条形码在每个表单中都重复,我不知道为什么要传递表单名称。只需从字面上设置所有控件,等等。还是我错过了什么?cmdFirst.Enabled = False
0赞 jmcilhinney 11/9/2023
听起来你以错误的方式做这件事。一种选择是创建一个包含所有代码的表单,并将代码放在那里,然后让所有其他表单继承它而不是 .另一种选择是创建一个包含所有 的用户控件,并将代码放在其中,然后将该用户控件添加到每个窗体中。VB 是一种面向对象语言,所以要编写面向对象代码。ButtonsFormButtons

答:

0赞 T.S. 11/9/2023 #1

这是一个伪代码,如何通过接口来做


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赞 F0r3v3r-A-N00b 11/9/2023 #2

您可以向工具条按钮添加标签值,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 
1赞 SSS 11/9/2023 #3

它可能有点片状,但 a 可能是你所追求的。UserControl

将 添加到您的项目中。这看起来像一个无边框的形式。将按钮添加到 UserControl(我在示例中添加了四个按钮 - 、)。然后添加启用/禁用代码。还要声明一些 - 这些将在托管表单中处理。请参阅下面的“UserControl1 代码”。UserControlcmdEditcmdPreviouscmdNextcmdSaveEvents

现在构建您的项目 - 这将在您的工具箱中可用。UserControl1

然后,您可以将工具箱中的 拖到 .然后,在窗体中,可以处理 UserControl1 事件以执行特定于窗体的操作。请参阅下面的“Form1 代码”。UserControl1Form1

您可能会发现将 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