-
Mar 6th, 2014, 11:06 AM
#1
Thread Starter
New Member
Set number of clicks according to a number in a textbox
Hey guys, so I have a small problem I'm new to VB and would like some help. I'm currently creating a page refresher bot, just something to do to pass the time and hopefully I'll build myself up to better things. I've tried searching for about two days now and I can't find anything related to this subject, though probably 60% of programs have this function, lol.
I'd like Button2 to complete the amount of refreshes entered in TextBox2. At the moment when I click Button2, it keeps on refreshing once every second, and doesn't stop until I close the program. But, I'd like to also enter the amount of refreshes the bot does before it stops automatically. For example if i want 1,000 refreshes and enter 1000, it does 1,000 and then stops. Here is the code I currently have;
Public Class Form1
Private _counter As Integer
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'reset the counter
_counter = 0
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
'when the document is loaded increment the counter and start the refresh timer
_counter += 1
'if we have reached the maximum refresh number then don't enable the timer again
If _counter >= CInt(TextBox2.Text) Then Exit Sub
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'disable the timer until the document has been loaded - prevent re-entrancy issues
Timer1.Enabled = False
'refresh the current page
WebBrowser1.Refresh()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
There is something wrong because when I click Button2 it navigates to the page and refreshes it twice, although I've entered "5" in TextBox2. Please help, thank you all for reading!
-
Mar 6th, 2014, 12:20 PM
#2
Thread Starter
New Member
Re: Set number of clicks according to a number in a textbox
Anyone?
-
Mar 6th, 2014, 12:49 PM
#3
Re: Set number of clicks according to a number in a textbox
1. Get rid of the WebBrowser1_DocumentCompleted sub completely.
2. Declare another variable to store the max number of refreshes allowed, i.e refresh_limit
3. In button click, check if timer is running... If it is, stop it. Then initialize the 2 variables: refresh_limit = value in textbox, counter = 0. Start the timer.
4. In timer.tick event handler, increment counter by 1. Refresh webbrowser. Test if counter = refresh_limit. If it is then stop the timer.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Mar 6th, 2014, 01:03 PM
#4
Thread Starter
New Member
Re: Set number of clicks according to a number in a textbox
Sorry, but you confused me rather than help
-
Mar 6th, 2014, 01:14 PM
#5
Thread Starter
New Member
Re: Set number of clicks according to a number in a textbox
Could someone possibly fix up the code for me and (or) show me the code that's wrong/needs adding etc?
-
Mar 6th, 2014, 01:23 PM
#6
Re: Set number of clicks according to a number in a textbox
Well, it was an outline.
The DocumentCompleted handler is going to cause you more trouble than benefit for this purpose, which is probably why Stanav suggested you get rid of it. That event can be raised multiple times for modern pages, because parts of the page will trigger it. In this case, you just want a full refresh periodically, so you don't really care whether every piece of the page loaded the last time. Thus you can get rid of that event handler and make your life a bit easier.
You do need a variable for the max number of clicks. Using CInt on a Textbox is a bad idea, because it will crash if the string is not an integer (including if it is empty). What you need for the conversion is Integer.TryParse, but only do it once. When you click the button, use Integer.TryParse to convert the contents of the textbox to an integer and store that in a variable named something like refreshLimit. Do all comparisons against that. As a general rule, try to convert to and from strings as little as possible, so convert the textbox.text one time, put it in a variable, then don't deal with the text again (unless it is changing, which it could, if you wanted, but that's a different matter).
Stanav was assuming in #3 that you can click the button at any time. So, you might click it when the timer is already running. That's not horrible, but he's right that you should probably stop the timer if it is currently running as the first step in the button click event.
You then get the number from the textbox.text and put it into the refreshLimit variable, and also set counter to 0. As a final step, start the timer.
When the timer ticks, add 1 to counter, then compare counter to refreshLimit. If it is at or above, stop the timer.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|