For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > August 2005 > Looping!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Looping!
Arpan

2005-08-27, 9:56 pm

A VB6 Form has a textbox & a command button. When the Form loads, the
value in the textbox is 0. Now what I want is when the button is
clicked for the first time, the value in the textbox should change to
1. If the button is pressed again, then the textbox value should change
to 2, so on & so forth. How do I do this?

Thanks,

Arpan

Ted

2005-08-27, 9:56 pm

' be sure it is integer, otherwise you will get an error
If len(text1.text) = 0 then text1.text="0"
'val converts a numerical string to numerical integer.
text1.text= val(text1.text) + 1



"Arpan" <arpan_de@hotmail.com> wrote in message
news:1125188821.122924.177260@g49g2000cwa.googlegroups.com...
>A VB6 Form has a textbox & a command button. When the Form loads, the
> value in the textbox is 0. Now what I want is when the button is
> clicked for the first time, the value in the textbox should change to
> 1. If the button is pressed again, then the textbox value should change
> to 2, so on & so forth. How do I do this?
>
> Thanks,
>
> Arpan
>



MikeD

2005-08-28, 3:55 am


"Arpan" <arpan_de@hotmail.com> wrote in message
news:1125188821.122924.177260@g49g2000cwa.googlegroups.com...
>A VB6 Form has a textbox & a command button. When the Form loads, the
> value in the textbox is 0. Now what I want is when the button is
> clicked for the first time, the value in the textbox should change to
> 1. If the button is pressed again, then the textbox value should change
> to 2, so on & so forth. How do I do this?



Well, that's not a loop. What you need to do is save the value. I would
recommend a static variable (declared as Long in the command button's Click
event) or a module-level variable (which is better depends on your needs).
Here's an example using a static variable. Add a textbox and command button
to a form and keep the default names.

-----BEGIN CODE
Option Explicit

Private Sub Command1_Click()

Static MyNumber As Long

MyNumber = MyNumber + 1
Text1.Text = CStr(MyNumber)

End Sub

Private Sub Form_Load()

Text1.Text = "0"

End Sub
-----END CODE

Note that there's no loop involved whatsoever. You're merely incrementing a
value with each click of the command button.

If you're unfamiliar with static variables, a static variable is one that
retains its value in successive calls to the procedure in which it's
declared. However, it's scope is still limited to that procedure. For more
information on static variables, look up the Static keyword in VB's Help
(and note differences between a static variable and a module-level
variable).


--
Mike
Microsoft MVP Visual Basic



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com