Share via

Visual Basics 2022: Select Case Function

computer_sciencehelp 1 Reputation point
2023-01-04T05:12:15.26+00:00

Hello,

I'm creating a memory card game on Visual Basics 2022 Windows Forms App (.NET Framework). As a part of the process I'm using the Select Case function on Visual Basics.

Using this code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AddHandler Button1.Click, AddressOf MyClickHandler
AddHandler Button2.Click, AddressOf MyClickHandler
End Sub

Private Sub MyClickHandler(sender As Object, e As EventArgs)

Select Case CType(sender, Button).Name    
    Case "Button1"    
           picturebox1.show()    

    Case "Button2"    
           picturebox2.show()    

End Select    

End Sub

Is there any way I can hide the same picture after pressing the button a second time? (Ex. The first time "Button1" is pressed, it shows the picturebox and then the second time "Button1" is pressed, it hides the picturebox again.)

Also is there any possible way to assign one function to two buttons at the same time, something like this:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AddHandler Button1.Click, AddressOf MyClickHandler
AddHandler Button2.Click, AddressOf MyClickHandler
End Sub

Private Sub MyClickHandler(sender As Object, e As EventArgs)

Select Case CType(sender, Button).Name    
    Case "Button1" and "Button2"   
           picturebox1.hide() and picture box1.hide()   

End Select    

End Sub

(Essentially my goal is for the matched pictureboxes to disappear after both are correctly selected.)

Any help and feedback will be appreciated, thank you.

Developer technologies | Visual Studio | Setup
Developer technologies | VB
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.


1 answer

Sort by: Most helpful
  1. LesHay 7,146 Reputation points
    2023-01-04T14:53:02.867+00:00

    Hi
    Don't ask multiple questions in the same thread, keep each to it's own thread.
    Regarding the Button Event Handlers. Yes, you can assign as many buttons to the same event handler, either by declaring the assignments as you show using AddHandler, or, by adding the Button ID to the Handler clause itself.
    For the other questions, start new threads for them.

    Here is a very simple example. On the Form there are 3 Buttons (2,3,4) and 3 PictureBoxes (2,3,4)
    This Handler code will deal with all the Buttons via a Case structure.

      Sub AllButtonsClick(sender As Object, e As EventArgs) Handles Button2.Click, Button3.Click, Button4.Click  
        Dim b As Button = DirectCast(sender, Button)  
        Select Case b.Name  
          Case "Button2"  
            PictureBox2.Visible = Not PictureBox2.Visible  
          Case "Button3"  
            PictureBox3.Visible = Not PictureBox3.Visible  
          Case "Button4"  
            PictureBox4.Visible = Not PictureBox4.Visible  
            '  and further Case statements  
        End Select  
      End Sub  
    
    2 people found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.