PDA

View Full Version : Visual Basic question: auto



GumboGorilla
18th November 2005, 06:05
This question regards Visual Basic, not C, C++, or anything else.

OK. I have a button. I need it to perform a function - how do I modify the code to make it so that instead of clicking the button 100 times, the function occurs 100 times when the button occurs once?

Also, it would be preferred that the operation would be as follows:

function
wait 2.5 seconds
function
wait 2.5 seconds

etc.

Any help is greatly appreciated, thanks!

Cheers,
Lanche

Zervic
18th November 2005, 15:42
Couldn't you just use a timer?

GumboGorilla
18th November 2005, 16:09
Yeah, just yesterday I figured this out - first proggie ive made without a tut so thanks for helpin

Zervic
18th November 2005, 17:26
Yeah, just yesterday I figured this out - first proggie ive made without a tut so thanks for helpin

No problem m8, need any more help just pm me

DyslexicCheater
19th November 2005, 01:14
Use a For loop.
Dim i As Integer

For i = 0 To 100
'// Your code here.
Next i

GumboGorilla
19th November 2005, 02:50
OK, so my code so far is basically



Private Sub high_Click()
Do
WebBrowser1.Navigate Text1.Text
Pause 0.4
Loop
End Sub

Private Sub low_Click()
Do
WebBrowser1.Navigate Text1.Text
Pause 1
Loop
End Sub

Private Sub med_Click()
Do
WebBrowser1.Navigate Text1.Text
Pause 0.6
Loop
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Label1.Caption = "Finished. Awaiting..."
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "Thanks for using 'AUTO! Refer Bugger v3.0 by Lanche'", vbOKOnly, "kthxbai"
End Sub

Private Sub WebBrowser1_DownloadBegin()
Label1.Caption = "Processing..."
End Sub


Public Sub Pause(sec As Single)
Dim t As Single
t = Timer
While Timer <= t + sec
DoEvents
Wend
End Sub



BUT

I want to make a textbox, and have Times = Text2.Text

where do I put that in, and also - I need a checkbox and when it is checked, the function will loop infinite times.

sandtrav23456
19th November 2005, 05:24
I think I understand what you're asking, but just to make sure, let me restate it.

You want to have a textbox that specifies the number of times to execute your code instead of just saying 100 times hardcoded. Then, you also want a Checkbox that overwrites your textbox if checked to execute the code infinitely many times.

Assuming thats what you meant, you can accomplish this with nested If-statements and For-loops. Assuming Text2 is the name of your textbox and Check1 is the name of your checkbox, you could do something like this:



Private Sub high_Click()
Dim times As Integer
Dim loop As Integer

times = CInt(Text2.Text)

If Check1.Value = 0 Then
For loop = 0 To times
WebBrowser1.Navigate Text1.Text
Pause 0.4
Next loop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
Pause 0.4
Wend

End If

End Sub


I'm assuming most of the stuff there is pretty self-explanatory once you have a look at it. If not, ask why and I'm sure someone will tell you even if I don't see it first. I only did one sub-procedure, you can apply the same thing with different pauses to the others.

-edit-
As a side-note, if this is a vb program, and you try to do the loop infinitely many times the way I have it up there (so that it will stop if you uncheck the check box) you are going to lose control of your program. It is going to execute infinitely many times, yes, but the interface is going to lock up too, and you'll have to end-process it to get it to stop. To get around this, you can look into multi-threaded applications if you want, but based on the context of this question you might need some more experience before you go there.

GumboGorilla
19th November 2005, 06:21
AWESOME. Thank you!

Just one thing - I need to change pause rates.

With a little experimentation, I realized that the Value property is how far the scroller is. How do I make it so that a higher value makes higher rates? I'd need it ranging from Pause 0.4 to Pause 1.2, but the only way I can think of working this is setting my scrollbar and making math out of it, e.g. scrl.Value divided by (whatever it takes to get to the 0.4 to 1.2 range) equals pause rate.

I have NO idea how to do this..

also, I'd just like to thank all of you for helping me so much throughout this process!

sandtrav23456
19th November 2005, 06:54
So um, I really have no idea where the "scroll bar" came from, but I'm assuming you just have a basic horizontal or vertical scroll bar stuck onto your form and you want to be able to drag that to increase the rate, so you only need 1 button; assuming I'm right about this interpretation, you need a mathematical equation to relate the position of the scrollbar to the amount of speed you want. So lets think this through:

You have a range in pause rates of .8 (1.2 - .4), so if you can figure out a percentage of how much scrollbar is currently in use, you can multiply it by .8 to figure out how much relative pause rate your scroll bar is requesting, and then add that to your base pause rate, .4, to get your absolute pause rate.

The scrollbar has a maximum value of 32767 (scrollbars are given 2 bytes, and there are supposed to be 2 scrollbars per window, 2bytes = 65535 possible values / 2 = 32767... you can verify this by just having it output the value of the scrollbar at a maximum point). Therefore, if you're looking for a percentage position, the scrollbars current position divided by its maximum position will yield your percent, so you can determine this via the equation:

percent = scroll.Value / 32767.
(Assuming scroll is the name of your scrollbar)

There's a problem with this: Horizontal scroll bars start from the left, but vertical ones start from the top. If you're using a horizontal scroll bar, this percentage is just fine, but if you're using a vertical one, you want the bottom of the scroll bar to be the slower end: you need to invert your Value property so that the top goes from a value of 0 to 32767, and the bottom goes from a value of 32767 to 0. You can accomplish this via the equation

percent = (32767 - scroll.Value) / 32767

Which essentially just swaps which side is the maximum. Note that swap is not necessary if you are using a horizontal scroll bar (you didn't specify, just trying to cover my bases).

So now we know our percent, we can apply that to the idea I said earlier to get our pause rate.

Pause Rate = Percent * Difference + Base

For a vertical scroll-bar, that yields the equation:
pauseVal = (((32767 - scroll.Value) / 32767) * .8) + .4

and for a horizontal:
pauseVal = ((scroll.Value) / 32767) * .8) + .4

Now you can plug those pause variables into your Pause statement (if you want the user to be able to change the pause rate in the middle of execution, just plug the raw equation in instead of a variable).

Good luck!

GumboGorilla
19th November 2005, 07:53
THANK YOU so much! I was getting a compile error for the horizontal, but I noticed a 3rd bracket missing.

and the noob posts up again:

ugh, I'm getting an error I haven't been getting before.



Private Sub Command2_Click()
Dim wimes As Integer
Dim woop As Integer

wimes = CInt(Text2.Text) <---

If Check1.Value = 0 Then
For woop = 0 To times
WebBrowser1.Navigate Text1.Text
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4
Next woop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4
Wend

End If
End Sub

error 13, mismatch. No idea what happened.. Text2 is called Text2, and wimes was renamed so no conflict formed with Times. same with Loop.

this started happening before i added the scrollbar in.

sandtrav23456
19th November 2005, 08:22
Type mismatch means you are trying to perform an action on an invalid data-type. In this case, its on the CInt. CInt is a function that converts a string to an intger: clearly there's margin for error here because a string contains many characters which are not necessarily valid in an integer. You can overcome this by adding a check to see if the string contains only numeric characters: vb has a function built-in that does this for you.



If IsNumeric(Text2.Text) Then
wimes = CInt(Text2.Text)
Else
Call MsgBox("You must specify a number for # of times.", vbOKOnly)
Text2.SetFocus
End If


I shoulda thought to warn you about that the first time around. Sorry!
Good luck!

GumboGorilla
19th November 2005, 09:54
Thanks very much!
man do I owe you guys a LOT.

EDIT: The code doesn't seem to be working.. it just loops then errors.

Perhaps something is wrong with loopVal?

I'll post the entire thing up in a bit.

sandtrav23456
19th November 2005, 19:58
Well, I'm not sure why its erroring, but I do see one problem. Lets examine your first loop:



If Check1.Value = 0 Then
For woop = 0 To times
WebBrowser1.Navigate Text1.Text
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4
Next woop


You check to see if the checkbox is checked, then you fire off a Loop from 0 to the number in the checkbox. Then you perform your action and then you set pauseVal to the value in the equation we worked through last night... but you never do anything with pauseVal.

pauseVal is just a number. By itself it means nothing. Originally you had something that looked like Pause 0.4, which calls sub-procedure Pause and passes it the value .4: this is what you want to do with pauseVal.


Pause pauseVal
(or )
Call Pause(pauseVal)
(if you're being syntactically rigid)

will fix that one right up. Note that you have the same problem in the next loop, and need to fix it too. I'd need to see the error you're getting to figure out why its crashing, though it is possible that it was just trying to execute commands too rapidly and being told to stop by windows (seeing as how it was trying to access another window instead of minding its own business). Thats just a theory based on lack of information though; take it with a grain of salt.

GumboGorilla
19th November 2005, 21:46
Heh - another cryptic piece of advice (no, this is good - it makes me think, you're feeding me tips but I'm learning on my own.)

Do I have to declare pauseVal? And if so, how would I?


Private Sub Command2_Click()
Dim wimes As Integer
Dim woop As Integer
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4

If IsNumeric(Text2.Text) Then
wimes = CInt(Text2.Text)
Else
Call MsgBox("You must specify a number for # of times.", vbOKOnly)
Text2.SetFocus
End If

If Check1.Value = 0 Then
For woop = 0 To Times
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Next woop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Wend

End If
End Sub

That's what I tried, and got a byRef error.

sandtrav23456
19th November 2005, 22:04
Ya, you got it right on the money: you need to declare pauseVal before you start dumping things into it. This is because Visual Basic is too nice, and when you try to use a variable without declaring it, it declares the variable for you (as an indeterminite data-type). Just like the error you got earlier with trying to pass the CInt function an invalid object, you are now trying to pass the Pause sub-procedure an invalid data-type.

Note that you can disable visual basics auto-declare variable feature by using the command "Option Explicit" at the top of your code (before any private variable or procedure declarations, just stick it on the first line). This makes finding errors like that one easier.

But you still need to know what data-type to make it. To figure this out, go examine the Pause sub-procedure you posted earlier:


Public Sub Pause(sec As Single)
Dim t As Single
t = Timer
While Timer <= t + sec
DoEvents
Wend
End Sub


Its taking 1 parameter, sec As Single. This means anytime you pass a value to the Pause sub-procedure, it had beter have the data-type Single (or it had better be cast as a single through a CInt kind of function). Hence, go back up to the top of your sub-procedure and declare pauseVal as a Single right before you use it:


Dim wimes As Integer
Dim woop As Integer
Dim pauseVal As Single
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4


Ta da! Hope that helps!

GumboGorilla
19th November 2005, 22:14
Thanks very much!

If I get this to work, I'll put out the exe.

EDIT: It never ends!!!!!!!!!
If I use the scrollbar, no matter what the number is that I put into the textbox, it will only reconnect once, and unless the Infinite checkbox is selected, it won't do it more than once.

Code:


Private Sub Command1_Click()
Pause 0
End Sub

Private Sub Command2_Click()
Dim wimes As Integer
Dim woop As Integer
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4

If IsNumeric(Text2.Text) Then
wimes = CInt(Text2.Text)
Else
Call MsgBox("You must specify a number for # of times.", vbOKOnly)
Text2.SetFocus
End If

If Check1.Value = 0 Then
For woop = 0 To Times
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Next woop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Wend

End If
End Sub

Private Sub compact_Click()
Form2.Height = 1905
End Sub

Private Sub full_Click()
Form2.Height = 12465
End Sub

Private Sub about_Click()
Form3.Show
End Sub
Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()
MsgBox "Version 4 now with adjustable rates, menus, and boring layout. Good stuff."
MsgBox "NOTE: Make sure you use File>Exit to end the program, otherwise you must end the process manually."
MsgBox "And that would be plain homo."
MsgBox "So here goes..."
End Sub


Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Label1.Caption = "Finished. Awaiting..."
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
MsgBox "Refer Bugger v4 by Lanche, kthxbye", vbOKOnly, "kthxbai"
End Sub

Private Sub WebBrowser1_DownloadBegin()
Label1.Caption = "Processing..."
End Sub


Public Sub Pause(sec As Single)
Dim t As Single
t = Timer
While Timer <= t + sec
DoEvents
Wend
End Sub

sandtrav23456
19th November 2005, 22:37
I'm not sure why you changed the variables times and loop to wimes and woop way back when, but thats your problem. In your for loop you are doing woop = 0 To Times, not woop = 0 To wimes. Times is always 0 because you are doing your math on wimes, not times, so the loop always executes once and only once (0 To 0)

On that note, you might also want to change the condition in your loop to read
woop = 1 To wimes
so that a value of 0 in the textbox actually makes it execute once, not twice.

With those changes, I get (in your command2 procedure)


Private Sub Command2_Click()
Dim wimes As Integer
Dim woop As Integer
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4

If IsNumeric(Text2.Text) Then
wimes = CInt(Text2.Text)
Else
Call MsgBox("You must specify a number for # of times.", vbOKOnly)
Text2.SetFocus
End If

If Check1.Value = 0 Then
For woop = 1 To wimes
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Next woop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
Pause pauseVal
Wend

End If
End Sub


Good luck!

GumboGorilla
20th November 2005, 00:04
ohhh...

I changed it because I already had a loop instance, and the variable conflicted (or at least I THINK it did, because when I changed it back, it worked.)

EDIT: Ack! It still only does it once.

sandtrav23456
20th November 2005, 00:10
well a) in the code you sent me you didn't change for woop = 0 To wimes, it still reads 1 to times, and thats no good
b) i said a while back you needed to Dim pauseVal As Single, and I dont see that in there either...

So either your posts aren't keeping up with your edits or your edits aren't where you think they are. I've already looked at the ByRef mismatch on pauseVal several posts ago, and the fix for it was Dim pauseVal As Single back in your variable declarations...
I'm starting to think you might not understand what variable delcarations are for. If thats the case, I can explain it, I just don't want to do it if you understand it already.

GumboGorilla
20th November 2005, 00:17
heh yeah, I edited my post above because I accidentally posted my old code, sorry.



Private Sub Command2_Click()
Dim wimes As Integer
Dim woop As Integer
Dim pauseVal As Single
pauseVal = (((scroll.Value) / 32767) * 0.8) + 0.4

If IsNumeric(Text2.Text) Then
wimes = CInt(Text2.Text)
Else
Call MsgBox("You must specify a number for # of times.", vbOKOnly)
Text2.SetFocus
End If

If Check1.Value = 0 Then
For woop = 0 To times
WebBrowser1.Navigate Text1.Text
Call Pause(pauseVal)
Next woop

ElseIf Check1.Value = 1 Then 'Check1.Value can also = 2, so be specific.
While Check1.Value = 1
WebBrowser1.Navigate Text1.Text
Call Pause(pauseVal)
Wend

End If
End Sub


theres the one that was supposed to come up.
:shy:

sandtrav23456
20th November 2005, 00:28
I've already told you why it still only does it once too.



If Check1.Value = 0 Then
For woop = 0 To times
WebBrowser1.Navigate Text1.Text
Call Pause(pauseVal)
Next woop


Change that line to read



If Check1.Value = 0 Then
For woop = 1 To wimes
WebBrowser1.Navigate Text1.Text
Call Pause(pauseVal)
Next woop


If you don't understand why, go re-read my first post on the second page.

GumboGorilla
20th November 2005, 01:08
shite. wow, I'm thick - I thought i changed it the other two times.

I understand why my code reverted, though - once the testing bugged, there was no way to get back to design mode without ending VB (no save), and I forgot to set it back.

ack. sorry

EDIT: IT WORKS PERFECTLY!

sandtrav23456
20th November 2005, 01:21
Excellent. So... do you mind telling us exactly what it is we've been helping you build? I'm personally pretty curious by now.

GumboGorilla
20th November 2005, 01:33
Sure thing, I'll upload and post a link (note, i made this for me and my friends so there's some other stuff in there too, just for fun).

Download! (http://s17.yousendit.com/d.aspx?id=23I3Q2TFQ4OBK14TYNQEYF7STP)

edit: everything I didn't ask for help for in this thread was made by me :) this was the first app I made without following any tutorials, either.