Hello everyone!!!! I need some help to make a condition that if a user will select or click more than 1 button, there would be a MsgBox("You can only select one seat for every Process") appeared.... The scenario is this: I have 5 buttons(A3, A4, A5, A6, A7) and on the code below shows that if a user will select any of the buttons, the BackColor of the Button will be Red and so IF a user will select or click again as the users second choice, THEN there would a MsgBox("You can only select one seat for every Process") appeared. What code is appropriate for the Condition? I will really appreciate any HELP ..... Thank You so MUCH!!!:'(

Private Sub A3_Click(sender As Object, e As EventArgs) Handles A3.Click, A4.Click, A5.Click, A6.Click, A7.Click
        sender.BackColor = Color.Red
        If sender     Then
        MsgBox()
        End If

You choose the correct event.
I am trying to modify it. It could help you

Public Class Form1
    Dim btnPress As Boolean = False

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click
        If btnPress Then
            MsgBox("You can only select one seat for every Process")
        Else
            Dim actBtn As Button = Me.ActiveControl
            If (actBtn.Name = "Button1") Or (actBtn.Name = "Button2") Or (actBtn.Name = "Button3") Or (actBtn.Name = "Button4") Or (actBtn.Name = "Button5") Then
                actBtn.BackColor = Color.Red
                btnPress = True
            End If
        End If
    End Sub
End Class

Thanks Santanu Das, I tried to do the codes but the output is same as:

Private Sub A3_Click(sender As Object, e As EventArgs) Handles A3.Click, A4.Click, A5.Click, A6.Click, A7.Click
        sender.BackColor = Color.Red

it appears all the button colors red without appearing the MsgBox("You can only select one seat for every Process"), when a user press another button .... which is more than 1 ....

What if a user wants to de-select a button and select another one? Keep track of the selected button name using a private string variable. If a button hasn't been selected or has been de-selected the value should be String.Empty. If a button is pressed and the variable = String.Empty then set variable = button name.

Because the action involves making a selection, wouldn't you be better off with either radio buttons or checkboxes instead of buttons?

Edited 2 Years Ago by Reverend Jim

Comments
thumbs up for logic

Here is one way (perhaps not the best way) of setting a group of checkboxes to allow zero or one selection. The example uses a GroupBox containing five CheckBoxes.

Private Ignore As Boolean = False

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged, CheckBox5.CheckedChanged

    If Ignore Then Exit Sub

    Dim cbx As CheckBox = sender
    Dim grp As GroupBox = cbx.Parent

    Ignore = True

    For Each box As CheckBox In grp.Controls.OfType(Of CheckBox)()
        If box.Name <> cbx.Name Then
            box.Checked = False
        End If
    Next

    Ignore = False

End Sub

The reason for the Ignore flag is so you can ignore events that get triggered when you change the checkbox statuses within the loop.

Radiobuttons are definitly the way to go here.
If you ever see a vintage transitor radio, you could select a channel by pushing a button, whichwould cause one of the other buttons to deselect automaticaly.
Did you ever wonder why those little round things were called RADIObuttons?

I agree with others that other controls may be more appropriate. However, if you do need to use buttons, you can use the following:

Private BtnName As String = String.Empty

Private Sub A3_Click(sender As System.Object, e As System.EventArgs) Handles A3.Click, A4.Click, A5.Click, A6.Click, A7.Click
    Dim selectedBtn As Button = sender

    If String.IsNullOrEmpty(BtnName) Then
        BtnName = selectedBtn.Name
        sender.BackColor = Color.Red
    Else If BtnName = selectedBtn.Name Then
        BtnName = String.Empty
        sender.BackColor = Control.DefaultBackColor
    Else
        'can only select 1 btn
        MsgBox("You can only select one seat for every Process")
    End If

End Sub

hello Sir Cgeier, i tried to do the codes you give ... but it ended up with an error which is selectedbtn.Name cannot be converted to string

Private Sub A3_Click(sender As Object, e As EventArgs) Handles A3.Click, A4.Click, A5.Click, A6.Click, A7.Click
        Dim BtnName As String = String.Empty
        Dim selectedBtn As Button = sender

        If String.IsNullOrEmpty(seatPress) Then
            BtnName = selectedBtn
            selectedBtn.BackColor = Color.Red
        ElseIf seatPress = selectedBtn.Name Then
            BtnName = String.Empty
            selectedBtn.BackColor = Control.DefaultBackColor
        Else
            MsgBox("You can only select one seat for every Process")
        End If
    End Sub

Any ideas Sir? I attach the Content or looks of the seats alignment

Edited 2 Years Ago by William_14: messages

Attachments received_m_mid_1412349008694_46c91687498b910311_0.jpeg 45.08 KB

Hello Sir Reverend Jim, ahmmm the reason why I choose button is because it contain the no. of seats with the Proper alignment of seats. I will attach here sir the content of my Program or the looks of seats alignment. Hope you can help me Sir Reverend Jim ... :'(

Attachments received_m_mid_1412349008694_46c91687498b910311_0.jpeg 45.08 KB

hello Sir Ddanbe, ahmmmm i used a Radio Button to select which of the 3 options the user select. so any of the 3 options where a user selected, another form will be open where the user can choose a seat#, coz our Programming project is a Seat Reservation sir .... thats why i choose to use the Button which contain the Seats# with its proper position or alignment of seats. I will attach here sir the Design of my Program .... hope you could help me Sir .... :'(

Attachments received_m_mid_1412349008694_46c91687498b910311_0.jpeg 45.08 KB received_m_mid_1412348982712_2f52b8602923e19574_0.jpeg 48.85 KB

You didn't copy the code correctly. Copy the code as is, line-by-line.

Check line #6:
BtnName = selectedBtn should be BtnName = selectedBtn.Name

Also, when renaming variables, rename all occurrences of the variable.

Pre-requisites:

  • Add 5 buttons to a form named "Form1.vb", name them as follows: A3, A4, A5, A6, A7

Form1.vb:
Note: I changed "sender.BackColor" to "selectedBtn.BackColor"--although either will work.

Public Class Form1

    Private BtnName As String = String.Empty

    Private Sub A3_Click(sender As System.Object, e As System.EventArgs) Handles A3.Click, A4.Click, A5.Click, A6.Click, A7.Click
        Dim selectedBtn As Button = sender

        If String.IsNullOrEmpty(BtnName) Then
            BtnName = selectedBtn.Name
            selectedBtn.BackColor = Color.Red
        Else If BtnName = selectedBtn.Name Then
            BtnName = String.Empty
            selectedBtn.BackColor = Control.DefaultBackColor
        Else
            'can only select 1 btn
            MsgBox("You can only select one seat for every Process")
        End If

    End Sub

End Class

Note: In this kind of scenario, one should probably be able to click on another button without having to de-select the previous one. Change the color on the previous button pressed back to the default color, and change the color of the newly pressed button to red.

Edited 2 Years Ago by cgeier

the reason why I choose button is because it contain the no. of seats with the Proper alignment of seats

I can see why you might prefer buttons then. In that case you can use a modified version of the same code except that you don't need the Ignore flag. You could put each column of buttons in its own groupbox (or panel)

Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click

    Dim btn As Button = sender

    'toggle the clicked button color

    If btn.BackColor = Button.DefaultBackColor Then
        btn.BackColor = Color.Red
    Else
        btn.BackColor = Button.DefaultBackColor
    End If

    'clear all the other buttons

    For Each b As Button In btn.Parent.Controls.OfType(Of Button)()
        If b.Name <> btn.Name Then
            b.BackColor = Button.DefaultBackColor
        End If
    Next

End Sub

Edited 2 Years Ago by Reverend Jim

Thank You all so much for the help ... it is already solved ... Thank YOu so so much
i really appreciate the help .... God Bless

This question has already been answered. Start a new discussion instead.