当表单状态更改时,如何在不使用 Select Case 语句的情况下处理启用/禁用工具条按钮?

发布于 2024-10-09 17:48:49 字数 722 浏览 3 评论 0原文

我们都知道,当表单状态发生变化时,可以使用 Select Case 语句 (VB.NET) 或 C# 中的 switch case 语句来处理启用/禁用 ToolStrip 按钮。

但我记得我的老师说过“当你使用 OOP 开发软件时,使用这些语句并不是正确的方法”。

Private Sub SetToolStripButtons()
    Select Case formState
        Case FormStates.Normal
            btnSave.Enabled = False
            btnCancel.Enabled = False
            btnNew.Enabled = True
            btnEdit.Enabled = True
        Case FormStates.Edit
           btnSave.Enabled = True
            btnCancel.Enabled = True
            btnNew.Enabled = False
            btnEdit.Enabled = False
        '.....
        '.....
    End Select
End Sub 

编辑:我在上面放了一个简单的代码片段

那么你会推荐什么?

We all know that it's possible to handle enabling/disabling ToolStrip buttons when a form state changes by using Select Case statements (VB.NET) or switch case statement in C#.

But I remember my teacher said "using these statements is not a correct way when you are developing a software using OOP".

Private Sub SetToolStripButtons()
    Select Case formState
        Case FormStates.Normal
            btnSave.Enabled = False
            btnCancel.Enabled = False
            btnNew.Enabled = True
            btnEdit.Enabled = True
        Case FormStates.Edit
           btnSave.Enabled = True
            btnCancel.Enabled = True
            btnNew.Enabled = False
            btnEdit.Enabled = False
        '.....
        '.....
    End Select
End Sub 

EDIT: I put a simple code snippet above

So what would you recommend instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感悟人生的甜 2024-10-16 17:48:49

嗯,代码看起来是有效的。我认为使用 Select Case 没有问题。
但是,如果您只有两种状态(正常和编辑),您可以像这样改进代码:

btnSave.Enabled = (formState = FormStates.Edit)
btnCancel.Enabled = (formState = FormStates.Edit) 
btnNew.Enabled = (formState <> FormStates.Edit) 
btnEdit.Enabled = (formState <> FormStates.Edit) 

Well, the code looks like valid. I see no problem in using Select Case.
However, if you have only two states (Normal and Edit), you can improve your code like this:

btnSave.Enabled = (formState = FormStates.Edit)
btnCancel.Enabled = (formState = FormStates.Edit) 
btnNew.Enabled = (formState <> FormStates.Edit) 
btnEdit.Enabled = (formState <> FormStates.Edit) 
夜吻♂芭芘 2024-10-16 17:48:49

您看过状态设计模式吗?

您将有一个抽象类来表示状态 FormState,其中包含一个虚拟 SetToolStripButtons() 方法:

然后该类的每个派生实现负责了解如何设置每个按钮的可见性(对于给定状态)。例如,FormStateNormal 类将重写 SetToolsStripButtons 以包含类似以下内容:

btnSave.Enabled = True
btnCancel.Enabled = True
btnNew.Enabled = False
btnEdit.Enabled = False    

它的含义远不止这些,但希望这应该是一个很好的起点。

Have you looked at the state design pattern?

You would have an abstract class to represent the state FormState that would contain a virtual SetToolStripButtons() method:

Then each derived implementation of that class is responsible for knowing how to set the visibility of each button (for the given state). For example the FormStateNormal class would override the SetToolsStripButtons to contain something like this:

btnSave.Enabled = True
btnCancel.Enabled = True
btnNew.Enabled = False
btnEdit.Enabled = False    

There is more to it than just that, but hopefully this should be a good starting point.

无力看清 2024-10-16 17:48:49

您可以将编辑模式存储在布尔值中,然后设置enable =此布尔值
例如:(将编辑模式设置为“True”,否则设置为“False”,然后调用该函数)

dim isEdit as Boolean

private sub SetToolStripButtons() 
   btnSave.Enabled = isEdit
   btnCancel.Enabled = isEdit
   btnNew.Enabled = Not isEdit
   btnEdit.Enabled = Not isEdit
End Sub 

you can store edit mode in Boolean then set enable= this Boolean
eg: (Set Edit Mode as "True", else set to "False", then call the function)

dim isEdit as Boolean

private sub SetToolStripButtons() 
   btnSave.Enabled = isEdit
   btnCancel.Enabled = isEdit
   btnNew.Enabled = Not isEdit
   btnEdit.Enabled = Not isEdit
End Sub 
与风相奔跑 2024-10-16 17:48:49

以下是我对此的解决方案:

  1. 在工具条控件中单击项目(工具条按钮)a 2.选择大小写用于识别由其文本按下的按钮,然后a
    lockcode 字符串传递给方法 lockToolStripItems()
  2. lockToolStripItems("lockcode") 然后该方法将通过解释传递的参数(将其转换为 char 数组)来处理,然后
    执行for循环来锁定或解锁控件,请注意,1或0是我使用的字符1=Enabled=true,0=.Enable=false。如果未知字符则返回错误消息框,注意方法中的 x 变量有 3 个用途,A. for 语句中的计数器,B. char 数组索引,C. 工具条项索引

就这么简单分三步,希望对您有所帮助。

Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuStripDeliveryNote.ItemClicked

            Select Case e.ClickedItem.ToString

                        Case "&New"

                            lockToolStripItems("10")

                        Case "&Edit"
                            lockToolStripItems("01")

                    End Select
                End Sub

    Private Sub lockToolStripItems(ByVal mylockCode As String)
                Dim myChar As Char
                For x = 0 To 1
                    myChar = mylockCode.Chars(x)
                    If myChar = "1" Then
                        mnuStripDeliveryNote.Items(x).Enabled = True
                    ElseIf myChar = "0" Then
                        mnuStripDeliveryNote.Items(x).Enabled = False
                    Else
                        MsgBox("Error on parameter" & myChar & "" & x)
                    End If
                Next
            End Sub

Below is my solution to this:

  1. On Item clicked event in the toolstrip control(a Tool Strip button) a 2. Select case is used to identify the button pressed by its text then a
    lockcode string is passed to the method lockToolStripItems()
  2. lockToolStripItems("lockcode") then the method will take care of if by interpreting the parameters passed by converting it to a char array then a
    For loop is executed to lock or unlock the controls, Do note that 1 or 0 are the characters I have used 1= Enabled=true, 0 = .Enable=false. if Unknown character then it returns an error message box, Note that x variable in the in the method has 3 uses, A. a counter in the for statement, B.char array index, C. an tool strip item index

As easy as that in 3 steps, hope this helps.

Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles mnuStripDeliveryNote.ItemClicked

            Select Case e.ClickedItem.ToString

                        Case "&New"

                            lockToolStripItems("10")

                        Case "&Edit"
                            lockToolStripItems("01")

                    End Select
                End Sub

    Private Sub lockToolStripItems(ByVal mylockCode As String)
                Dim myChar As Char
                For x = 0 To 1
                    myChar = mylockCode.Chars(x)
                    If myChar = "1" Then
                        mnuStripDeliveryNote.Items(x).Enabled = True
                    ElseIf myChar = "0" Then
                        mnuStripDeliveryNote.Items(x).Enabled = False
                    Else
                        MsgBox("Error on parameter" & myChar & "" & x)
                    End If
                Next
            End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文