9 Replies - 327 Views - Last Post: 16 October 2017 - 05:09 PM Rate Topic: -----

#1 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Choosing Between to Images with a If Then Statement

Posted 15 October 2017 - 11:07 PM

Choosing Between to Images with a If Then Statement, which is based on a outcome of the program calculation being equal to the user's input. The program chooses two integers randomly e.g. 5 and 8. The user is to input the addition of the two which is 13 then hit the calculate button to this play a image of displaying right or wrong.

I'm only getting a blue error line under "rightPictureBox" and "wrongPictureBox"......Can I get some advice please.

Option Explicit On
Option Infer Off
Option Strict On




Public Class MainForm

    


    Private Sub uansLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uansLabel.Click

    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close() 
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcButton.Click
       
        Dim fno As Integer
        Dim sno As Integer

        Dim uno, pno As Integer    'un0 is User Answer. pno is Program Answer

       
        Integer.TryParse(anoBox.Text, uno)
        
        pno = fno + sno

        If uno = pno Then

            displayPictureBox.Image = rightPictureBox.Image 
        Else
            displayPictureBox.Image = wrongPictureBox.Image
        End If




    End Sub


    
   
    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        'declare Variables

        Dim fno As New Random 'fno is First Number

        Dim sno As New Random 'sno is Second Number 

        fno = randomGenerator.Next(1, 11)

        sno = randomGenerator.Next(1, 11)
    End Sub

    
End Class 


Is This A Good Question/Topic? 0
  • +

Replies To: Choosing Between to Images with a If Then Statement

#2 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Re: Choosing Between to Images with a If Then Statement

Posted 15 October 2017 - 11:53 PM

I'm no longer getting any errors but I'm only getting the "wrong" image even if the answer is right. The random everything else works fine.

If you have any Ideas how to get the image display part to choose the right one. I'll be thankful..........
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 01:10 AM

That is VB.NET code, not VB6. Moving to VB.NET forum.
Was This Post Helpful? 0
  • +
  • -

#4 PeterH  Icon User is online

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 279
  • Joined: 03-September 09

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 03:53 AM

You should review your variable declarations and consider their 'lifetime' e.g. in your startbutton click event, you declare fno and sno and assign them values, because these are 'local' to that event they will be 'lost' when that event ends i.e. they are no longer available anywhere else in your code. You then declare two variables with the same names in your calcbutton event, these will by default be initialised to zero, hence any calculation made on these will always be zero.

This post has been edited by PeterH: 16 October 2017 - 03:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 05:48 AM

When you have comments like this

'un0 is User Answer. pno is Program Answer

it is an indication that your variables should be named userAnswer and programAnswer.
Was This Post Helpful? 0
  • +
  • -

#6 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 09:52 AM

Thank You for your feedback...........I'm working on it.
Was This Post Helpful? 0
  • +
  • -

#7 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 12:37 PM

I fixed some parts but I'm still getting the same problem, Do I have to use a function to separate the Random then compare the random numbers to the users answer. I need the program to choose the numbers before I input the answer so how do i do two separate functions with one button. Cause when I click calculate it produces the two random numbers and it displays a wrong image even though I have not input my answer.

So logically I need the Random Numbers generated and displayed, then input my answer, then clarify the program answer and users answer. After it displays if right or wrong.
Can I get any more advice please....


Option Explicit On
Option Infer Off
Option Strict On




Public Class mainForm

    




    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close() 
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcButton.Click



    
        Dim firstNumber As Integer ' First Number
        Dim secondNumber As Integer 'Second Number 


        firstNumber = CInt(Int((11 * Rnd()) + 1)) 'pick random number between 1 to 10
        firstNumberBox.Text = firstNumber.ToString

        secondNumber = CInt(Int((11 * Rnd()) + 1)) 'pick random number between 1 to 10
        secondNumberBox.Text = secondNumber.ToString
       
        Dim userAnswer As Integer   'User Answer.
        
        
        Integer.TryParse(userAnswerBox.Text, userAnswer)

         programAnswer =firstNumber + secondNumber

        If programAnswer = userAnswer Then                    'program comparing user answer and programs answer

            answerPictureBox.Image = My.Resources.right  'display if right
        Else

            answerPictureBox.Image = My.Resources.wrong 'display if wrong

        End If


    End Sub
    
    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        
        firstNumber.Text = String.Empty
        secondNumberBox.Text = String.Empty
        userAnswerBox.Text = String.Empty
        answerPictureBox.Image = Nothing
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,674
  • Joined: 01-February 13

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 02:11 PM

You where on the right path with the first code you showed. You need to have one button to pick the two random numbers. Then another button to check if the user entered the right/wrong answer. Switch back to that code.

Then take a look at the code in your startButton_Click event sub. You are declaring the fno and sno variables as Local variables. Local variables will no longer exist after the start button's click event sub has been exited. You need to declare those two variables as Class scoped variables (outside of any subs) instead of Locally scoped variables (inside a sub).

Just to show an example, notice where in the code the class scoped variable is declared. Also notice that is accessible inside both of the button click event subs.
Public Class Form1

    'Declare variables outside of any subs or functions but, inside the Form's Class to make them Class Scoped. These will be accessible from anywhere in the Form's Class.
    Dim MyClassScopedVariable As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyClassScopedVariable = 100
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If MyClassScopedVariable = 100 Then
            MessageBox.Show("It is 100.")
        End If
    End Sub

End Class



Declare your fno and sno variables as class scoped variable. All that needs to happen in the Start button is to assign the random numbers to them. They will also be accessible in the calculate button's click event sub so that you can compare them with the users answer.
Was This Post Helpful? 1
  • +
  • -

#9 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 04:59 PM

Thank You for your advice, I got it to Work...Much Appreciated.

This post has been edited by IronRazer: 16 October 2017 - 05:08 PM
Reason for edit:: Removed large quote

Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,674
  • Joined: 01-February 13

Re: Choosing Between to Images with a If Then Statement

Posted 16 October 2017 - 05:09 PM

You're Welcome, glad you got it working. 8)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1