423,680 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,680 IT Pros & Developers. It's quick & easy.

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

P: 10
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3.         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
  4.         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
  5.         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
  6.         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
  7.         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
  8.         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
  9.         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
  10.         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
May 6 '14 #1

✓ answered by Luk3r

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

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

Share this Question
Share on Google+
9 Replies


Rabbit
Expert Mod 10K+
P: 12,272
Please use code tags when posting code or formatted data.

You haven't told us which scenarios result in incorrect results.
May 6 '14 #2

P: 10
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.

Expand|Select|Wrap|Line Numbers
  1. 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?
May 6 '14 #3

Rabbit
Expert Mod 10K+
P: 12,272
This is wrong:
Expand|Select|Wrap|Line Numbers
  1. (Textbox4.Text = "1" Or "2" Or "3" Or "4" Or "5")
It has to be this:
Expand|Select|Wrap|Line Numbers
  1. (Textbox4.Text = "1" Or Textbox4.Text = "2" Or Textbox4.Text = "3" Or Textbox4.Text = "4" Or Textbox4.Text = "5")
May 7 '14 #4

100+
P: 299
In Addition to what Rabbit said, you could also use greater than or equal to, or less than or equal to. Example:
Expand|Select|Wrap|Line Numbers
  1. 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'.
May 7 '14 #5

P: 10
Thank you, this has resolved some problems except now the code:
Expand|Select|Wrap|Line Numbers
  1.  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.
May 7 '14 #6

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

Expand|Select|Wrap|Line Numbers
  1. If Textbox1.Text = "11" And Textbox2.Text = "11" And Textbox3.Text = "11" Then
  2.     If Textbox4.Text = "1" Or Textbox4.Text = "2" Or Textbox4.Text = "3" Or Textbox4.Text = "4" Or Textbox4.Text = "5" Then
  3.         PictureBox2.Visible = True
  4.     Else
  5.         PictureBox2.Visible = False
  6.     End If
  7. End If
Edit**: You may also have to put PictureBox2.Visible = False as your Else in your first If...Else statement. I just tossed some code at you to show nesting.
May 7 '14 #7

Rabbit
Expert Mod 10K+
P: 12,272
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.
May 8 '14 #8

P: 10
Thank you Luk3r and Rabbit my codes are working fine now. The nesting was a necessary step I had skipped.
May 8 '14 #9

100+
P: 299
Glad we could help and happy coding!
May 8 '14 #10

Post your reply

Sign in to post your reply or Sign up for a free account.