Valhalla Legends Forums Archive | Visual Basic Programming | Simple VB Question

AuthorMessageTime
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 )
[code]
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
[/code]
I wanna know how to add 1 to my score integer...
August 2, 2003, 12:54 AM
j0k3r
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.
August 2, 2003, 12:58 AM
Camel
[quote author=j0k3r link=board=5;threadid=2168;start=0#msg16739 date=1059785900]Edit2: You must also assign it a value before incrementing it.[/quote]
No you don't. Uninitialized numbers and strings default to 0 and "" respectively.
August 2, 2003, 1:12 AM
j0k3r
Ah didn't know that...
[quote]
intScore = "1"
[/quote]
It's usually best to leave out the quotes.
[quote]
intScore = intScore + 1
[/quote]
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.
August 2, 2003, 1:39 AM
ProXie
[quote author=j0k3r link=board=5;threadid=2168;start=0#msg16746 date=1059788369]
Ah didn't know that...
[quote]
intScore = "1"
[/quote]
It's usually best to leave out the quotes.
[quote]
intScore = intScore + 1
[/quote]
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.

[/quote]
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 )
August 2, 2003, 2:36 AM
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[/quote]
August 2, 2003, 2:38 AM
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:
[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
[/code]
2 Command Buttons and 2 Text Boxes...Thanks guys for your help ;D
August 2, 2003, 2:47 AM
Camel
[quote author=ProXie link=board=5;threadid=2168;start=0#msg16766 date=1059792448]
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:
[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
[/code]
2 Command Buttons and 2 Text Boxes...Thanks guys for your help ;D
[/quote]

Additionally, please learn to indent -- it will solve many of your problems:
[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[/code]

[edit] Or even better:
[code]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[/code]
August 2, 2003, 5:44 AM
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..?
August 2, 2003, 12:27 PM
j0k3r
[me=j0k3r]is going to try lamely to answer this[/me]

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.
August 2, 2003, 1:27 PM
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..
August 2, 2003, 3:54 PM
j0k3r
text1.locked = true
August 2, 2003, 5:13 PM
ProXie
Thanks Alot - If I can figure out this point system i will give you +1.
August 2, 2003, 5:58 PM
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.
August 2, 2003, 6:57 PM
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.
August 3, 2003, 5:12 PM
tty
.
March 5, 2005, 1:07 AM
Quarantine
[me=Warrior]whistles for a Moderator or something[/me]
March 5, 2005, 3:09 AM

Search