Home > Net >  Lock button when user put a specific value on one of the choices
Lock button when user put a specific value on one of the choices

Time:03-29

enter image description here I want to have a code that the user can only input 1 of the 4 rooms. I tried many ways, but I got stucked up. this is the code of my form rooms.

Public Class formsnacks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnbackkk.Click
    Me.Hide()
    Formrooms.Show()
    Dim itemcost(30)
End Sub

Private Sub btnincf_Click(sender As Object, e As EventArgs) Handles btnincf.Click
    lblfries.Text  = 1
    If lblfries.Text >= 10 Then
        lblfries.Text = 10
    End If

End Sub

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblfries.BackColor = ColorTranslator.FromHtml("#100F0E")
    lblburger.BackColor = ColorTranslator.FromHtml("#100F0E")
    lblcupcake.BackColor = ColorTranslator.FromHtml("#100F0E")
    lblpizza.BackColor = ColorTranslator.FromHtml("#100F0E")
End Sub

Private Sub btndecf_Click(sender As Object, e As EventArgs) Handles btndecf.Click
    lblfries.Text -= 1
    If lblfries.Text <= 0 Then
        lblfries.Text = 0
    End If
End Sub

Private Sub btndecc_Click(sender As Object, e As EventArgs) Handles btndecc.Click
    lblcupcake.Text -= 1
    If lblcupcake.Text <= 0 Then
        lblcupcake.Text = 0
    End If
End Sub

Private Sub btnincc_Click(sender As Object, e As EventArgs) Handles btnincc.Click
    lblcupcake.Text  = 1
    If lblcupcake.Text >= 10 Then
        lblcupcake.Text = 10
    End If
End Sub

Private Sub btndecb_Click(sender As Object, e As EventArgs) Handles btndecb.Click
    lblburger.Text -= 1
    If lblburger.Text <= 0 Then
        lblburger.Text = 0
    End If

End Sub

Private Sub btnincb_Click(sender As Object, e As EventArgs) Handles btnincb.Click
    lblburger.Text  = 1
    If lblburger.Text >= 10 Then
        lblburger.Text = 10
    End If
End Sub

Private Sub btndecp_Click(sender As Object, e As EventArgs) Handles btndecp.Click
    lblpizza.Text -= 1
    If lblpizza.Text <= 0 Then
        lblpizza.Text = 0
    End If

End Sub

Private Sub btnincp_Click(sender As Object, e As EventArgs) Handles btnincp.Click
    lblpizza.Text  = 1
    If lblpizza.Text >= 10 Then
        lblpizza.Text = 10
    End If
End Sub

I want to lock a button if one of 4 rooms have already have value. This is the one of the 5 major problems I had on my system. It is too difficult for me as a young student.

CodePudding user response:

I don't understand exactly, but I think that's the solution. [1]: https://i.stack.imgur.com/D62kl.png [example][1]

And I want an explanation of the idea you want to do if I don't help you with this solution

Dim once_room As Integer

Private Sub Room1_Click(sender As Object, e As EventArgs) Handles Room1.Click
    once_room = 1
End Sub

Private Sub Room2_Click(sender As Object, e As EventArgs) Handles Room2.Click
    once_room = 2
End Sub

Private Sub Room3_Click(sender As Object, e As EventArgs) Handles Room3.Click
    once_room = 3
End Sub

Private Sub Room4_Click(sender As Object, e As EventArgs) Handles Room4.Click
    once_room = 4
End Sub

Private Sub Next_Click(sender As Object, e As EventArgs) Handles Next.Click
    Select Case (once_room)
        Case 1
            ' Go Room1
        Case 2
            ' Go Room2
        Case 3
            ' Go Room3
        Case 4
            ' Go Room4
        Case Else
            MsgBox("") ' Here to tell user he not selected a room
    End Select
End Sub

CodePudding user response:

If I've managed to pick out the actual meaning from your poor explanation, this type of thing will do the job:

Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click,
                                                                    Button3.Click,
                                                                    Button2.Click,
                                                                    Button1.Click
    For Each btn In Controls.OfType(Of Button)()
        btn.Enabled = btn Is sender
    Next
End Sub

One event handler for all Buttons and then set the Enabled property of each Button. sender is the object that raised the event, i.e. the Button that was clicked. All but that Button are not the sender so all but that Button will have their Enabled property set to False.

Note that this assumes that all those Buttons are in the same container - the form in this specific case - and that container contains no other Buttons. If that's not currently the case, you can either hard-code the list of Buttons to loop over or move just those Buttons into a new container, e.g. a Panel.

  • Related