PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] IF-Then Problem.-VBForums
Results 1 to 17 of 17

Thread: [RESOLVED] IF-Then Problem.

  1. #1

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Resolved [RESOLVED] IF-Then Problem.

    I have a button that changes a picture in a picture box, and I tried using If Then Statements to change it like so..

    Code:
    If imgtour.Tag = "pic01" Then
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic02.jpg")
    imgtour.Tag = "pic02"
    End If
    The problem is that I have 20+ If Then statements in this button, when I press the button, it cycles through every picture to the end. How can I make it "pause" at each picture until I click it again?

    My knowledge of VB is pretty limited, so the most basic answers would be most appreciated

    Thanks in advance,
    Shawn

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,620

    Re: IF-Then Problem.

    try using select case imgtour.tag
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: IF-Then Problem.

    ?? What are you trying to do?
    -a- Show a different picture on each click of the command button OR
    -b- Show a "Slide Show" of ALL pictures when clicking on the button??
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Re: IF-Then Problem.

    Could you elaborate on Select Case please?

    What is it?
    How should it be used?

    Opus: I am having it show a different picture each time I click the button.

  5. #5
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: IF-Then Problem.

    I am not sure if slelect case would be different then an IF-Then.

    It totally depends on your code in the button click event.

    Can I have a look at your button click code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  6. #6
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: IF-Then Problem.

    You can put the pictureboxes and tags in arrays and then use one Long to cycle the index.

    vb Code:
    1. 'make a control array of pictureboxes called imgtor
    2.  
    3. Dim tag() As String
    4. 'make as much tags as there are image boxes
    5. 'put an appropriate string in each tag
    6.  
    7. If imgtour(index).Tag = Tag(index) Then
    8.     '...
    9. End If
    10. index = index + 1
    11. If index > UBound(imgtour) Then index = 0

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: IF-Then Problem.

    I assume you have named all your pictures like: "smallpics\pic01.jpg", the number starts at 01 and ends at max at 99.
    in the commandbutton_click event routine
    Code:
    Dim ActualPicture as Integer
    ActualPicture=CInt(Right(imgtour.Tag;2))
    ActualPicture=ActualPicture+1
    'You have to set the correct maximum-number of pictures in here!
    If ActualPicture>99 then
      ActualPicture=1
    End If
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" &CStr(ActualPicture) & ".jpg")
    
    imgtour.Tag = "pic" & CStr(ActualPicture)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Re: IF-Then Problem.

    I don't quite understand what arrays are.

    I understand your code opus but I get a "compile error: Expected: list separator or )" for line
    Code:
    ActualPicture=CInt(Right(imgtour.Tag;2))

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: IF-Then Problem.

    Sorry that should have been
    Code:
    ActualPicture=CInt(Right(imgtour.Tag,2))
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  10. #10

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Re: IF-Then Problem.

    When I compile it and press the button the first time, it works. when I press it the second time I get an error "Run-time error '13': Type mismatch"

    I also changed the code a little and used it as a prev button. That button works about 20 times or so, then I get the same error.

    Code:
    Dim ActualPicture As Integer
    ActualPicture = CInt(Right(imgtour.Tag, 2))
    ActualPicture = ActualPicture - 1
    'You have to set the correct maximum-number of pictures in here!
    If ActualPicture < 1 Then
      ActualPicture = 27
    End If
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" & CStr(ActualPicture) & ".jpg")
    
    imgtour.Tag = "pic" & CStr(ActualPicture)

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: IF-Then Problem.

    I think that you need to make sure that the leading zero is added when you get to single digits
    Code:
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" & _
          Left$("0", 2 - Len(CStr(ActualPicture))) & CStr(ActualPicture) & ".jpg")

  12. #12
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: IF-Then Problem.

    @russow kid: post 4 ?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  13. #13

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Re: IF-Then Problem.

    To make the opus' code work, I actually removed the 0 in the image names and tag names so its just pic1, pic2, etc... now.

    My 2 buttons are

    Code:
    Private Sub Next_Click()
    Dim ActualPicture As Integer
    ActualPicture = CInt(Right(imgtour.Tag, 2))
    ActualPicture = ActualPicture + 1
    'You have to set the correct maximum-number of pictures in here!
    If ActualPicture > 27 Then
      ActualPicture = 1
    End If
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" & CStr(ActualPicture) & ".jpg")
    
    imgtour.Tag = "pic" & CStr(ActualPicture)
    End Sub
    
    Private Sub Prev_Click()
    Dim ActualPicture As Integer
    ActualPicture = CInt(Right(imgtour.Tag, 2))
    ActualPicture = ActualPicture - 1
    'You have to set the correct maximum-number of pictures in here!
    If ActualPicture < 1 Then
      ActualPicture = 27
    End If
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" & CStr(ActualPicture) & ".jpg")
    
    imgtour.Tag = "pic" & CStr(ActualPicture)
    End Sub
    Last edited by russow kid; Apr 4th, 2008 at 04:23 AM.

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: IF-Then Problem.

    I suggest you put the leading zeros back otherwise you'll have to perform another test before
    Code:
    ActualPicture = CInt(Right$(imgtour.Tag, 2))
    beacuse where you have a single digit the rightmost 2 characters wont be numeric (eg Right$ ("Pic1",2) will return "c1" and will give you a type mismatch
    whereas Right$("Pic01",2) will return "01" which is numeric)

  15. #15

    Thread Starter
    Lively Member russow kid's Avatar
    Join Date
    Oct 2005
    Posts
    105

    Re: IF-Then Problem.

    Thanks everyone for your help, it is now working after adding the leading 0 back in.

  16. #16
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,998

    Re: IF-Then Problem.

    just a suggestion. if your pictures are named as 01 to 27 then you will have to use this way to achieve what you want

    Code:
    Sub numberformat()
        Dim ActualPicture As Integer
        ActualPicture = 1
        MsgBox CStr(Format(ActualPicture, "00"))
        'Comment the above and uncomment the below
        'Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic" & CStr(Format(ActualPicture, "00")) & ".jpg")
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    OMEN by HP - 15-ce073tx with Win10+Office 2013. || Mac Book Pro (10.6.8) with Office 2011

  17. #17
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: [RESOLVED] IF-Then Problem.

    Quote Originally Posted by russow kid
    I have a button that changes a picture in a picture box, and I tried using If Then Statements to change it like so..

    Code:
    If imgtour.Tag = "pic01" Then
    Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic02.jpg")
    imgtour.Tag = "pic02"
    End If
    The problem is that I have 20+ If Then statements in this button, when I press the button, it cycles through every picture to the end. How can I make it "pause" at each picture until I click it again?

    My knowledge of VB is pretty limited, so the most basic answers would be most appreciated

    Thanks in advance,
    Shawn
    Code:
    If imgtour.Tag = "pic01" Then
       Set imgtour.Picture = LoadPicture(App.Path & "smallpics\pic02.jpg")
       imgtour.Tag = "pic02"
    
       Exit Sub
    
    End If

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width