I am trying to create a combination password-like system making pictures appear

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ScottDay96
    New Member
    • May 2014
    • 10

    I am trying to create a combination password-like system making pictures appear

    Before starting please note I am a complete novice so most of my codes will probably be messy and overcomplicated . I am trying to create a program where when you enter the correct series of digits (In this case 4 digits ranging from 1-12) a picture would appear. The problem is that though it is working for when the correct combination of digits is used, there are also a few occasions when the pictures appear even though the combination is incorrect. This happens mostly when 2 or more numbers match. I think the main one of the issues that complicates this is the fact that the combination can be allowed in any order, and also the same picture needs to become visible for different codes. E.g. 11 11 11 2 gives the same picture as 11 11 11 5 and so I have tried to group them rather than write the code out numerous times.

    Here is the code I am currently using:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
           
            If Textbox1.Text = "11" And Textbox2.Text = "11" And Textbox3.Text = "11" And (Textbox4.Text = "1" Or "2" Or "3" Or "4" Or "5") Then PictureBox2.Visible = True Else PictureBox2.Visible = False
            If Textbox1.Text = "11" And Textbox2.Text = "11" And (Textbox3.Text = "1" Or "2" Or "3" Or "4" Or "5") And Textbox4.Text = "11" Then PictureBox3.Visible = True Else PictureBox3.Visible = False
            If Textbox1.Text = "11" And (Textbox2.Text = "1" Or "2" Or "3" Or "4" Or "5") And Textbox3.Text = "11" And Textbox4.Text = "11" Then PictureBox4.Visible = True Else PictureBox4.Visible = False
            If (Textbox1.Text = "1" Or "2" Or "3" Or "4" Or "5") And Textbox2.Text = "11" And Textbox3.Text = "11" And Textbox4.Text = "11" Then PictureBox1.Visible = True Else PictureBox1.Visible = False
            If (Textbox1.Text = "8" Or "9") And (Textbox2.Text = "8" Or "9") And (Textbox3.Text = "8" Or "9") And Textbox4.Text = "12" Then PictureBox5.Visible = True Else PictureBox5.Visible = False
            If (Textbox1.Text = "8" Or "9") And (Textbox2.Text = "8" Or "9") And Textbox3.Text = "12" And (Textbox4.Text = "8" Or "9") Then PictureBox6.Visible = True Else PictureBox6.Visible = False
            If (Textbox1.Text = "8" Or "9") And Textbox2.Text = "12" And (Textbox3.Text = "8" Or "9") And (Textbox4.Text = "8" Or "9") Then PictureBox7.Visible = True Else PictureBox7.Visible = False
            If Textbox1.Text = "12" And (Textbox2.Text = "8" Or "9") And (Textbox3.Text = "8" Or "9") And (Textbox4.Text = "8" Or "9") Then PictureBox8.Visible = True Else PictureBox8.Visible = False
    Last edited by Rabbit; May 6 '14, 10:15 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code or formatted data.

    You haven't told us which scenarios result in incorrect results.

    Comment

    • ScottDay96
      New Member
      • May 2014
      • 10

      #3
      There seem to be endless incorrect results.
      The Image I want to appear from the combinations only:
      "11 11 11 1", "11 11 11 2", "11 11 11 3", "11 11 11 4" or "11 11 11 5" also appears when the combinations "11 11 11 6", "11 11 11 7", "11 11 11 8", "11 11 11 9" or "11 11 11 10" are typed.

      Code:
      If Textbox1.Text = "11" And Textbox2.Text = "11" And Textbox3.Text = "11" And (Textbox4.Text = "1" Or "2" Or "3" Or "4" Or "5") Then PictureBox2.Visible = True Else PictureBox2.Visible = False
      This is only one example as this seems to happen for most of the others as well. I hope this makes more sense?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        This is wrong:
        Code:
        (Textbox4.Text = "1" Or "2" Or "3" Or "4" Or "5")
        It has to be this:
        Code:
        (Textbox4.Text = "1" Or Textbox4.Text = "2" Or Textbox4.Text = "3" Or Textbox4.Text = "4" Or Textbox4.Text = "5")

        Comment

        • Luk3r
          Contributor
          • Jan 2014
          • 300

          #5
          In Addition to what Rabbit said, you could also use greater than or equal to, or less than or equal to. Example:
          Code:
          Textbox4.text <= 5
          Which would cover the whole range of 1 through 5 instead of typing it all out. BUT, Rabbit is right that your syntax is definitely off when using 'Or'.

          Comment

          • ScottDay96
            New Member
            • May 2014
            • 10

            #6
            Thank you, this has resolved some problems except now the code:
            Code:
             If Textbox1.Text = "12" And (Textbox2.Text = "8" Or Textbox2.Text = "9") And (Textbox3.Text = "8" Or Textbox3.Text = "9") And (Textbox4.Text = "8" Or Textbox4.Text = "9") Then PictureBox8.Visible = True Else PictureBox8.Visible = False
            Does not accept combinations such as: "12 8 9 8", "12 9 9 9" etc. as valid when they should work. Thank you for your help, sorry for the lack of understanding of coding.

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              I'm not 100% certain with VB, but I believe you have to nest the Or statement with the And statement:

              Code:
              If Textbox1.Text = "11" And Textbox2.Text = "11" And Textbox3.Text = "11" Then
              	If Textbox4.Text = "1" Or Textbox4.Text = "2" Or Textbox4.Text = "3" Or Textbox4.Text = "4" Or Textbox4.Text = "5" Then
              		PictureBox2.Visible = True
              	Else
              		PictureBox2.Visible = False
              	End If
              End If
              Edit**: You may also have to put PictureBox2.Vis ible = False as your Else in your first If...Else statement. I just tossed some code at you to show nesting.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                What you have as your expression should work fine. There's something else going on. Try nesting the ifs and stepping through the code to see what the actual values are and how far down into the nested ifs the code gets.

                Comment

                • ScottDay96
                  New Member
                  • May 2014
                  • 10

                  #9
                  Thank you Luk3r and Rabbit my codes are working fine now. The nesting was a necessary step I had skipped.

                  Comment

                  • Luk3r
                    Contributor
                    • Jan 2014
                    • 300

                    #10
                    Glad we could help and happy coding!

                    Comment

                    Working...