954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie here.. pls Help.!!

how do i add the numbers in label1 and label2 and i want it to appear at label3?

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Use textbox control for input purpose, not label.

Have a command button control on your form first and put this code on it.

Private Sub Command1_Click()

Sum = Val(Text1.Text) + Val(Text2.Text)

Label3.Caption = Sum

End Sub


Please read the forum rules first if this is your first time posting here. http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Cheers!!

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

No, i will not input the two number..

the two labels are linked at a picture box. If the picture1 was clicked, the label1 appears number 1 same as the picture2 and label2.

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Yes, it's my first time to post here.

Did i violate a rule?

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Only with the title. The title needs to have a description of your error, say "Add numbers in labels". Being new, no harm no foul.:)

As far as your question goes, what bexactly do you need if you are not going to get the sum of both. If you click on pic1, label 1 shows 1, why then add the 1 to label 3 as well?

@Abe, your code will not work. You need to declare the sum first -

Private Sub Command1_Click()
Dim Sum As Integer
 
Sum = Val(Text1.Text) + Val(Text2.Text)
 
Label3.Caption = Sum
 
End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

because I'm creating a game. A logic game.

The picture1 is one of the choices in the first question and it is the correct answer. So if the player clicked it, i will give him 1 point which i want to be placed at the label1. Actually my problem is solved i will just do it like this:

Line 4.

Sum = Val(Label1.Caption) + Val(Label2.Caption)

Am I right?

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I'm sorry about my title.

thank you for your warning.

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

@Sir Andre
@Abe, your code will not work. You need to declare the sum first -

It still works without declaring sir ;)

@sk8rock_15
Have a look at this, I included the code for the Picture controls.

Private Sub Command1_Click()

Sum = Val(Label1.Caption) + Val(Label2.Caption)
Label3.Caption = Str(Sum)

End Sub

Private Sub Picture1_Click()
Label1.Caption = "1"
End Sub

Private Sub Picture2_Click()
Label2.Caption = "1"
End Sub
abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

Don't quarrel guys.

Anyways, thanks for both of you.

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Please mark thread as solve if your problem was solved.

Cheers!!

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

Yeah, I already know the code on Picture boxes. Why str? AndreRet declare the SUM as Integer while you are declaring it as String.

What is the correct one?

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

It still works either way.

Str(Sum)


You don't need to declare Sum and Str as string but it will still work

I really don't know why.

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

why you declare the SUM as string?

AndreRet declare it as Integer. What is the correct one?

sk8rock_15
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Just like i said, both of them will work..

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

No it will not. If the OP starts with more advanced coding, he is going to fall into a pitfall, bugging his application. If we do give advice, we give the correct advice.:)

It still works without declaring sir

It will because "Sum" is a function within vb6. What if the OP uses it as a string, as you suggested? This means that if a user adds MyPic12345 it will be declared, as where an integer will throw an error, only wanting numbers, 12345. This is the correct way to make your application bug free.;)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

@Abe, sir andre is right, we need to declare variables even if vb6 accept and run without declaring any variable. Try to put this code on the General Declaration area so you will not forget to declare your variables :D

Option Explicit


You can also make vb6 automatically adds option explicit by going to Tools > Options > Editor Tab > then tick "Require Variable Declaration" > Ok.

jhai_salvador
Junior Poster
180 posts since Mar 2008
Reputation Points: 33
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: