Results 1 to 6 of 6

Thread: [RESOLVED] Select Case on PictureBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Resolved [RESOLVED] Select Case on PictureBox

    Hello!

    I'm developing a Wizard-style application, but I'm not using the "VB Wizard Manager" type of program because I need to explicitly manage which page is shown next based on what the current page is as well as user input. I have a "currPic" variable that keeps track of which page (PictureBox) is currently shown.

    In my cmdNext_Click method, I have a Select Case statement that switches on the value of "currPic", and is supposed to show the next page depending on what the current page is. Here is a small example:

    Code:
    Option Explicit
    
    Private currPic As PictureBox
    
    Private Sub Command1_Click()
        
        If currPic Is Nothing Then
            Set currPic = pic1
        End If
        
        Select Case currPic
            
            Case pic1
                Debug.Print "currPic = pic1"
                Set currPic = pic2
            Case pic2
                Debug.Print "currPic = pic2"
                Set currPic = pic3
            Case pic3
                Debug.Print "currPic = pic3"
        
            Case Else
                Debug.Print "currPic not set: " & currPic
                
        End Select
        
    End Sub
    As you can see, the select statement is simply setting the "currPic" variable to reference the next PictureBox in the series. If everything were working correctly, then clicking the button 3 times should produce this output:

    currPic = pic1
    currPic = pic2
    currPic = pic3

    But it produces this instead:

    currPic = pic1
    currPic = pic1
    currPic = pic1

    Based on this it looks like the assignment isn't happening at all; the Select statement always executes the "Case pic1" clause no matter what. But, after the Select statement, if I print currPic.Name to the Debug output, it prints the correct name ("pic1", "pic2", "pic3").

    How can currPic have the correct name but not the correct object reference? I realize I can simply Select on currPic.Name and work around this, but I'm just curious about what's going on here. Am I doing something wrong, or is that the nature of object references in VB6?

    Thank you!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Select Case on PictureBox

    The problem is that Select Case only works with Numeric or String expressions, it does not work with Objects.

    Due to this, what you are actually comparing is the default value of the control type, which in this case is .Picture.Handle , and presumably your controls don't have pictures, so the value of that will be 0 for all of them, thus any will match, and the first you have listed (in this case pic1) will be chosen.


    Rather than working around it though (perhaps with the name, or If statements), I would recommend using a control array of pictureboxes, and simply incrementing an Integer counter to specify which one to show.

  3. #3
    Addicted Member
    Join Date
    Apr 2006
    Posts
    155

    Re: Select Case on PictureBox

    That's because the values of each of the cases are all 0, making it always stay at Case pic1, try this...put a picture in each PictureBox; then try it again, i'm guessing your not using any pictures in these Pictureboxes, just for a container?

    edit: si got to it b4 me
    *Please mark your original post as "Resolved" if you're question has been answered. You can do this by going into Thread Tools at the top of the Thread and "Mark Thread Resolved".

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Select Case on PictureBox

    The Default property of the PictureBox is the Picture property. If there is no image the Picture property equals 0. The Select Case statement will use the default property to make its comparisons.

    To check if one object = another in a Select Case use this code.

    Code:
        Select Case True        
            Case currPic is pic1 
                Debug.Print "currPic = pic1"
                Set currPic = pic2
            Case currPic is pic2
                Debug.Print "currPic = pic2"
                Set currPic = pic3
            Case currPic is pic3
                Debug.Print "currPic = pic3"
            Case Else
                Debug.Print "currPic not set: " & currPic
        End Select

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Select Case on PictureBox

    If these picture boxes do not contain a picture then the reference number is always 0 for all of them. I loaded each pic1, pic2, and pic 3 with a picture and your code works correctly



    EDIT:

    Everybody beat me to the post

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: [RESOLVED] Select Case on PictureBox

    Well that makes sense. You guys are right, there are no pictures as I'm using the PictureBoxes as containers for other controls. It worked when I used Frames, and now I realize that's probably because the Frame's title is the default property used in the Select statement and each title was unique. I guess I should have left well enough alone and just set the Frames' BorderStyle to 0 instead of moving everything over to PictureBoxes. The code provided by brucevde works great.

    Thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width