How can I handle toolbar on / off buttons when the form state changes without using the Select Case statement?

We all know that you can control the on / off of ToolStrip buttons when changing the state of a form using operators Select Case(VB.NET) or switchin C #.

But I remember, my teacher said: "Using these statements is not the right way when developing 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 added a simple code snippet above

So what would you recommend instead?

+3
source share
4 answers

, . Select Case. , (Normal Edit), :

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

?

FormState, SetToolStripButtons():

, ( ). , FormStateNormal SetToolsStripButtons, - :

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

, , , , .

+1

Boolean, enable = this Boolean : ( "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 
0

:

  • , ( "" ). 2. , , , lockcode lockToolStripItems()
  • lockToolStripItems ( "lockcode" ), , char, . : 1 0 , 1 = Enabled = true, 0 =.Enable = false. , . , x 3 , A. for, B.char, C.

As easy as 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
0
source

Source: https://habr.com/ru/post/1782428/


All Articles