• Welcome to Valhalla Legends Archive.
 

Simple VB Question

Started by ProXie, August 01, 2003, 07:54 PM

Previous topic - Next topic

ProXie

Hey All.. I am trying to make a program so that when you get an answer correct it adds 1 to the integer "intScore".  So I did
My Code is
( hopefully tihs will work )

Dim intScore As Integer
Private Sub Command1_Click()
Text2.Text = "Hello"
End Sub
Private Sub Text2_Change()
intScore = "1"
Text1.Text = intScore
End Sub
Private Sub Text1_Change()
If Text2.Text = "Hello" Then
intScore = intScore + 1
Else
MsgBox "Incorrect"
End If
End Sub

I wanna know how to add 1 to my score integer...

j0k3r

#1
Did you declare intScore?

Other than that it IS right.

Edit1: corrected variable spelling
Edit2: You must also assign it a value before incrementing it.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Camel

Quote from: j0k3r on August 01, 2003, 07:58 PMEdit2: You must also assign it a value before incrementing it.
No you don't. Uninitialized numbers and strings default to 0 and "" respectively.

j0k3r

Ah didn't know that...
Quote
intScore = "1"
It's usually best to leave out the quotes.
Quote
intScore = intScore + 1
That code is fine, if there is something wrong then it has to do with some other part of your code. Post the code you are using (try it before you post too please) and tell us what line is highlighted when the error occurs.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

ProXie

Quote from: j0k3r on August 01, 2003, 08:39 PM
Ah didn't know that...
Quote
intScore = "1"
It's usually best to leave out the quotes.
Quote
intScore = intScore + 1
That code is fine, if there is something wrong then it has to do with some other part of your code. Post the code you are using (try it before you post too please) and tell us what line is highlighted when the error occurs.

Beh lots of quotes.. I dont get a highlighted line its just that instead of a 2 which I want I only have 1 ( in my textbox )

j0k3r

Your problem is that you only tell it once to put the value of your variable inside your text box, THEN you increment it.

You want to add a timer to your program or tell it to put your variable inside the box after you've incremented it...

Quote
Dim intScore As Integer
Private Sub Command1_Click()
Text2.Text = "Hello"
End Sub
Private Sub Text2_Change()
intScore = "1"
Text1.Text = intScore
End Sub
Private Sub Text1_Change()
If Text2.Text = "Hello" Then
intScore = intScore + 1
Text1.Text = intScore
Else
MsgBox "Incorrect"
End If
End Sub
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

ProXie

Ok I finally figured it out.  Whatever I did worked.. Haven't took the time to actually think about it yet.  Here is the code:

Private Sub Command1_Click()
intScore = "1"
Text2.Text = intScore
Text1.Text = "Hello"
If Text1.Text = "Hello" Then
intScore = intScore + 1
Text2.Text = intScore
Else
MsgBox "Sorry"
End If
End Sub

Private Sub Command2_Click()
If Text1.Text = "Hello" Then
intScore = intScore + 1
Text2.Text = intScore
Else
MsgBox "Sorry"
End If
End Sub

2 Command Buttons and 2 Text Boxes...Thanks guys for your help  ;D

Camel

#7
Quote from: ProXie on August 01, 2003, 09:47 PM
Ok I finally figured it out.  Whatever I did worked.. Haven't took the time to actually think about it yet.  Here is the code:

Private Sub Command1_Click()
intScore = "1"
Text2.Text = intScore
Text1.Text = "Hello"
If Text1.Text = "Hello" Then
intScore = intScore + 1
Text2.Text = intScore
Else
MsgBox "Sorry"
End If
End Sub

Private Sub Command2_Click()
If Text1.Text = "Hello" Then
intScore = intScore + 1
Text2.Text = intScore
Else
MsgBox "Sorry"
End If
End Sub

2 Command Buttons and 2 Text Boxes...Thanks guys for your help  ;D

Additionally, please learn to indent -- it will solve many of your problems:
Private Sub Command1_Click()
   intScore = "1"
   Text2.Text = intScore
   Text1.Text = "Hello"
   If Text1.Text = "Hello" Then
       intScore = intScore + 1
       Text2.Text = intScore
   Else
       MsgBox "Sorry"
   End If
End Sub

Private Sub Command2_Click()
   If Text1.Text = "Hello" Then
       intScore = intScore + 1
       Text2.Text = intScore
   Else
       MsgBox "Sorry"
   End If
End Sub


[edit] Or even better:
Private Sub Command1_Click()
   intScore = 1 'no quotes on numbers
   
   Text1.Text = "Hello"
   If Text1.Text = "Hello" Then 'you really don't need to check this...
       intScore = intScore + 1
   Else
       MsgBox "Sorry"
   End If
   
   Text2.Text = intScore 'put this at the end so it isnt there twice
End Sub

ProXie

[qoute]Additionally, please learn to indent -- it will solve many of your problems:[/qoute]
Im new to this.. can you explain to me where to index etc..?

j0k3r

* j0k3r is going to try lamely to answer this

You want to indent between two lines of codes (like between opening and closeing sub, between if statements, etc). Code such as (text1.text = "BLAH") or (intVariable = 6) you want to be indented.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

ProXie

+1 to Joker and +1 to Camel to trying to help.  One more question:
I want it so that on a button click it "locks" a textbox.  In my program Text1 has your score in it.  I don't want people changing their score.  :P .. Any ideas? I tried Lock.Text1 .. didn't work..

j0k3r

QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

ProXie

Thanks Alot - If I can figure out this point system i will give you +1.

Camel

The Locked property of a textbox will prevent users from modifying it's contents through the gui; that's probably what you want, but there's also an Enabled property that will entirely lock out the control, turning it gray and preventing it from being highlighted.

j0k3r

Why would he want to prevent them from highlighting it?

If he were to lock out control, he would not be able to modify it, making the textbox (scoreboard) useless.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo