'Lock button when user put a specific value on one of the choices

I want to lock the other buttons when the user input an amount to one of the four rooms. Instead if I can't lock, please help me do the otherway whereas when the user click the other rooms, the other rooms will freeze to 0 value.

Public Class Formrooms

    Dim birthday As Double
    Dim party As Double
    Dim vip As Double
    Dim deluxe As Double
    
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lblparty.BackColor = ColorTranslator.FromHtml("#100F0E")
        lblbday.BackColor = ColorTranslator.FromHtml("#100F0E")
        lblvip.BackColor = ColorTranslator.FromHtml("#100F0E")
        lbldeluxe.BackColor = ColorTranslator.FromHtml("#100F0E") 
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Me.Hide()
        Formsnacks.Show()
    End Sub

    Private Sub btnbirthday_Click(sender As Object, e As EventArgs)
        Me.Hide()
        Formsnacks.Show()
    End Sub

    Private Sub btnvip_Click(sender As Object, e As EventArgs)
        Me.Hide()
        Formsnacks.Show()
    End Sub

    Private Sub btndeluxe_Click(sender As Object, e As EventArgs)
        Me.Hide()
        Formsnacks.Show()
    End Sub

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

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

    Private Sub btndecv_Click(sender As Object, e As EventArgs) Handles btndecv.Click
        lblvip.Text -= 1
        If lblvip.Text <= 0 Then
            lblvip.Text = 0
        End If
    End Sub

    Private Sub btndecd_Click(sender As Object, e As EventArgs) Handles btndecd.Click
        lbldeluxe.Text -= 1
        If lbldeluxe.Text <= 0 Then
            lbldeluxe.Text = 0
        End If
    End Sub

    Private Sub btnincp_Click(sender As Object, e As EventArgs) Handles btnincp.Click

        lblparty.Text += 1
        If lblparty.Text >= 3 Then
            lblparty.Text = 3
        End If
    End Sub

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

    Private Sub btnincv_Click(sender As Object, e As EventArgs) Handles btnincv.Click
        lblvip.Text += 1
        If lblvip.Text >= 3 Then
            lblvip.Text = 3
        End If
    End Sub

    Private Sub btnincd_Click(sender As Object, e As EventArgs) Handles btnincd.Click
        lbldeluxe.Text += 1
        If lbldeluxe.Text >= 3 Then
            lbldeluxe.Text = 3
        End If

    End Sub

    Private Sub Number_only(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
        Dim dialog As DialogResult
        If lblbday.Text = 0 And lbldeluxe.Text = 0 And lblparty.Text = 0 And lblvip.Text = 0 Then
            dialog = MessageBox.Show("You must choose the rooms", "Error", MessageBoxButtons.OK)
        ElseIf lblbday.Text > 0 And lblparty.Text >= 1 And lblvip.Text = 1 And lbldeluxe.Text > 1 Then
            dialog = MessageBox.Show("You can only choose 1 room", "Error", MessageBoxButtons.OK)
        Else
            Me.Hide()
            formsnacks.Show()

        End If
    End Sub

    Private Sub btnbackr_Click(sender As Object, e As EventArgs) Handles btnbackr.Click
        Me.Hide()
        Formtakeorders.Show()
    End Sub

    Private Sub lblparty_Click(sender As Object, e As EventArgs) Handles lblparty.Click

    End Sub

End Class


Solution 1:[1]

I think you should get rid of the four buttons (party, birthday, vip, deluxe) and just have ONE "next" button. Then just reset the other hour selections to zero whenever you increase one of the room values:

Public Class Formrooms

    Dim party As Integer
    Dim birthday As Integer
    Dim vip As Integer
    Dim deluxe As Integer

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

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

    Private Sub btndecv_Click(sender As Object, e As EventArgs) Handles btndecv.Click
        If vip > 0 Then
            vip = vip - 1
            lblvip.Text = vip
        End If
    End Sub

    Private Sub btndecd_Click(sender As Object, e As EventArgs) Handles btndecd.Click
        If deluxe > 0 Then
            deluxe = deluxe - 1
            lbldeluxe.Text = deluxe
        End If
    End Sub

    Private Sub btnincp_Click(sender As Object, e As EventArgs) Handles btnincp.Click
        party = Math.Min(party + 1, 3)
        lblparty.Text = party

        birthday = 0
        vip = 0
        deluxe = 0
        lblbirthday.Text = birthday
        lblvip.Text = vip
        lbldeluxe.Text  deluxe
    End Sub

    Private Sub btnincb_Click(sender As Object, e As EventArgs) Handles btnincb.Click
        birthday = Math.Min(birthday + 1, 3)
        lblbday.Text = birthday

        party = 0
        vip = 0
        deluxe = 0
        lblparty.Text = party
        lblvip.Text = vip
        lbldeluxe.Text  deluxe
    End Sub

    Private Sub btnincv_Click(sender As Object, e As EventArgs) Handles btnincv.Click
        vip = Math.Min(vip + 1, 3)
        lblvip.Text = vip

        party = 0
        birthday = 0
        deluxe = 0
        lblparty.Text = party
        lblbirthday.Text = birthday
        lbldeluxe.Text = deluxe
    End Sub

    Private Sub btnincd_Click(sender As Object, e As EventArgs) Handles btnincd.Click
        deluxe = Math.Min(deluxe + 1, 3)
        lbldeluxe.Text = deluxe

        party = 0
        birthday = 0
        vip = 0
        lblparty.Text = party
        lblbirthday.Text = birthday
        lblvip.Text = vip
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
        If birthday = 0 AndAlso deluxe = 0 AndAlso party = 0 AndAlso vip = 0 Then
            MessageBox.Show("You must choose a room with at least one hour.", "Error", MessageBoxButtons.OK)
        Else
            Me.Hide()
            formsnacks.Show()
        End If
    End Sub

End Class

Solution 2:[2]

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.

Solution 3:[3]

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

' Name my Button : Room1 , Room2 , Room3, Room4

'example Dim ButtonRoom As Button = CType(Me.Controls("Room" & j), Button)

' Name my Button : hello1 , hello2 , hello3, hello4

'example2 Dim ButtonRoom As Button = CType(Me.Controls("hello" & j), Button)

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

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

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

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

Public Function selected_room(ByVal i As Integer)
    For j As Integer = 1 To 4
        Dim ButtonRoom As Button = CType(Me.Controls("<name your button here>" & j), Button)

        If j = i Then
            ButtonRoom.Enabled = True
        Else
            ButtonRoom.Enabled = False
        End If
    Next j
    Return True
End Function

If you do not benefit from the solution I hope you contact me instagram : ovhc_

Vote for me if I've helped you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idle_Mind
Solution 2 user18387401
Solution 3