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

Recommended Answers

All 15 Replies

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!!

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.

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

Did i violate a rule?

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

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?

I'm sorry about my title.

thank you for your warning.

@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

Don't quarrel guys.

Anyways, thanks for both of you.

Please mark thread as solve if your problem was solved.

Cheers!!

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?

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.

why you declare the SUM as string?

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

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

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.;)

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.