Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: ProXie on August 01, 2003, 07:54 PM

Title: Simple VB Question
Post by: ProXie on August 01, 2003, 07:54 PM
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...
Title: Re:Simple VB Question
Post by: j0k3r on August 01, 2003, 07:58 PM
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.
Title: Re:Simple VB Question
Post by: Camel on August 01, 2003, 08:12 PM
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.
Title: Re:Simple VB Question
Post by: 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.
Title: Re:Simple VB Question
Post by: ProXie on August 01, 2003, 09:36 PM
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 )
Title: Re:Simple VB Question
Post by: j0k3r on August 01, 2003, 09:38 PM
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
Title: Re:Simple VB Question
Post by: 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
Title: Re:Simple VB Question
Post by: Camel on August 02, 2003, 12:44 AM
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
Title: Re:Simple VB Question
Post by: ProXie on August 02, 2003, 07:27 AM
[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..?
Title: Re:Simple VB Question
Post by: j0k3r on August 02, 2003, 08:27 AM
/me 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.
Title: Re:Simple VB Question
Post by: ProXie on August 02, 2003, 10:54 AM
+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..
Title: Re:Simple VB Question
Post by: j0k3r on August 02, 2003, 12:13 PM
text1.locked = true
Title: Re:Simple VB Question
Post by: ProXie on August 02, 2003, 12:58 PM
Thanks Alot - If I can figure out this point system i will give you +1.
Title: Re:Simple VB Question
Post by: Camel on August 02, 2003, 01:57 PM
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.
Title: Re:Simple VB Question
Post by: j0k3r on August 03, 2003, 12:12 PM
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.
Title: Re: Simple VB Question
Post by: tty on March 04, 2005, 07:07 PM
.
Title: Re: Simple VB Question
Post by: Warrior on March 04, 2005, 09:09 PM
/me whistles for a Moderator or something