I've made it so I have a progress bar which is the players health starting at 100 and going down over time via a timer. When the player health progress bar gets to 0, I want a message to come up saying "You died! Game over."
Instead of doing that, it just does that when I click it as soon as the progress bar has reached 0, due to the 'Handles PlayerHealth.Click' bit. But what do I change the PlayerHealth.Click to to make it so the message box comes up when the progress bar just hits 0, without having to click it?
I can't find the right thing in the intellisense list. Or is there a better method?
Here's the piece of code in question :
Private Sub AttackButton_Click(sender As System.Object, e As System.EventArgs) Handles AttackButton.Click
PlayerHealthTimer.Start()
EnemyHealth.Increment(-2)
End Sub
Private Sub PlayerHealthTimer_Tick(sender As System.Object, e As System.EventArgs) Handles PlayerHealthTimer.Tick
PlayerHealth.Increment(-2)
End Sub
Private Sub PlayerHealth_Value(sender As System.Object, e As System.EventArgs) Handles PlayerHealth.Click
If PlayerHealth.Value = 0 Then
MsgBox("You died! Game over.")
End If
End Sub
Ignore the middle sub.
Thank you!
It fires on click because you have the MessageBox in the Sub that is handling the click method.
You may actually want to use the middle sub you said to ignore :). That one handles the logic on each tick.
Private Sub PlayerHealthTimer_Tick(sender As System.Object, e As System.EventArgs) Handles PlayerHealthTimer.Tick
PlayerHealth.Increment(-2)
if PlayerHealth.Value = 0 Then
MsgBox("You died! Game Over.")
''Then make sure to stop the timer
PlayerHealthTimer.Stop()
End If
End Sub
you can just use condition like
if PlayerHealth.value<=0 then
'place a message box or other way to show info message
end if
Related
I have used three timers in my program. All are working fine except the third one. i don't know why?
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
MessageBox.Show("dont repeat please")
Timer3.Stop()
End Sub
In the form_load i had started the timer, but its showing message box again and again at the interval of 3000ms which i have set. Please help.
Unless there is more to it a quick solution is to just move the call stop the timer to before the message box.
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Timer3.Stop()
MessageBox.Show("dont repeat please")
End Sub
Basically the processing of the code in the Timer3_Ticket sub is being blocked by the display of the message box. The timer running on another thread will continue to raise tick events at each time interval until a message box is closed and the stop method is called.
Public Class Form1
Dim Iclick, submit
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Iclick.InvokeMember("click")
With WebBrowser1.Document
For l_index As Integer = 0 To ListBox1.Items.Count - 1
Dim l_text As String = CStr(ListBox1.Items(l_index))
.All("input").InnerText = l_text
System.Threading.Thread.Sleep(5000)
Next
'.All("input").InnerText = "http://wordpress.com"
End With
submit.InvokeMember("click")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://whatwpthemeisthat.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Iclick = WebBrowser1.Document.GetElementById("input")
submit = WebBrowser1.Document.GetElementById("check")
End Sub
End Class
This is my code so far I have a ListBox with URLs I want to check using web browser which theme are they running (if they are wordpress) but the program seems to be bugged when I click START it is NOT responding, until the last element. It has to do something with the system.threading.thread.sleep line but I don't know what am I doing wrong? Thanks.
That's excactly what the Thread.Sleep() method is for. It freezes for 5 seconds.
Also you are replacing the input each time the For loop repeats. So it replaces, waits 5 seconds, replaces, waits 5 sec... and so on.
I guess you are trying to click the submit button for each of the elements, but that's not what you are doing. You have to hit the submit button each time the text has changed. However you really can't be sure about the loading time of the page.
I'd suggest you to place the submit-part inside the loop, then wait, then go into the next iteration. Maybe you could even try to run the website multiple times, one for each listbox item and then apply the right text to the controls in the webpage, hit the button and receive your result.
EDIT: Your best bet for the Thread.Sleep() would be creating a new thread in which you place the loop. This thread can be paused for 5 seconds, and your application will still respond. This is how you create one:
Imports System.Threading.Thread
Dim myThread as Thread
'//..........
'//Button1 is clicked ->
myThread = new Thread (AddressOf myLoop)
myThread.start()
'//..........
Private Sub myLoop ()
'// Loop goes here...
'Sleeping here will only affect the thread that runs this sub. Your form will still be available
End Sub
i am creating a minigame where when the use clicks a button, it "attacks" the monster causing it to lose 5 hp, displayed using a progressbar. then at the same time the monster also attacks making the player lose hp. but the problem is these events happen at the exact same time, and i would like a 2 second interval between the events. ive been trying to get a timer event to work since this morning, but it just wouldnt work here is my code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Tick = 0 Then
Timer1.Stop()
Timer1.Enabled = False
Else
Tick -= 1
End If
End Sub
and here is the attack button event
Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttack.Click
PlayerAttacks(mhealth, attack)
textShiftUp(numlines)
HPBarsAlter(mhealth, health)
Tick = 2
Timer1.Start()
MonsterAttacks(health, mattack, CritRate)
HPBarsAlter(mhealth, health)
MobDeath(mhealth, MobNumber)
Timer1.Stop()
End Sub
please tell me if you need any more information thank you :)
Basically, move your monster attack to the timer
Private Sub btnAttack_Click(...)
btnAttack.Enabled = False ' disable more attacks
PlayerAttacks(mhealth, attack)
textShiftUp(numlines)
HPBarsAlter(mhealth, health)
MonsterTimer.Interval = 2000
MonsterTimer.Start()
End Sub
Private Sub MonsterTimer_Tick(...
' not sure what the old code was doing
MonsterTimer.Stop ' stop the attacks
MonsterAttacks(health, mattack, CritRate)
HPBarsAlter(mhealth, health)
MobDeath(mhealth, MobNumber)
btnAttack.Enabled = True ' allow more attacks
End Sub
EDIT
Added 2 lines to toggle the ability to attack while waiting.
I'm using Visual Basic 2010, and within my form shown sub I need two buttons to be pressed , however I need the first button's code to complete before the moving on to pressing the next button. Is there any way to allow this to happen? Thanks :)
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
BindingNavigatorMoveLastItem.PerformClick()))
'I need this next button click to be carried out after the one above has completely finished
BindingNavigatorMovePreviousItem.PerformClick()))
End Sub
Use methods instead of "button-clicks":
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
MoveLastItem()
MovePreviousItem()
End Sub
Private Sub MoveLastItem()
bindingSource1.MoveLast();
End Sub
Private Sub MovePreviousItem()
bindingSource1.MovePrevious();
End Sub
You just have call these methods from the button-click events handlers as well.
Private Sub BindingNavigatorMoveLastItem_Clicked(sender As Object, args As EventArgs) Handles BindingNavigatorMoveLastItem.Click
MoveLastItem()
End Sub
Private Sub BindingNavigatorMovePreviousItem_Clicked(sender As Object, args As EventArgs) Handles BindingNavigatorMovePreviousItem.Click
MovePreviousItem()
End Sub
I must admit I don't do VB, but I came across this page and it might be useful to you.
http://msdn.microsoft.com/en-us//library/system.windows.forms.application.doevents.aspx
If that is not relevant, in your situation I would either use the buttons to set flags and simply if-test the flags, or create a do-while loop so that the code can finish executing while the conditions are met. Careful with those, however, as infinite loops are something they are good at.
Another thought is to enable the second button in the last line of the code of the first button?
Hope something helps. Apologies if it is of no use.
I am working at a small VB.NET project which autofill the fields on the Yahoo register page. Is there a way to click on "Check" button and see if the entered ID is OK or not?
Something like if the entered ID is OK then proceed further with filling the field, if not, try another ID and press "Check" button again.
The webbrowser control lets you access elements within the webpage and you can invoke methods on them, so something as simple as this will click the button:
webBrowser1.Document.All("yidHelperBtn").InvokeMember("click");
Add a timer to your application, with an interval of 1000 ms. Here is the code:
Dim CheckButton, yahooId As HtmlElement
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
yahooId = WebBrowser1.Document.GetElementById("yahooid")
CheckButton = WebBrowser1.Document.GetElementById("yidHelperBtn")
yahooId.InnerText = "testID" 'Replace testID by the ID you want
Timer1.Start() 'Starts the timer: within 1000 ms (1 s). It will execute the Timer1_Tick sub.
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
CheckButton.Focus() 'Give the check button the focus
SendKeys.Send("{ENTER}") 'Causes the validation of the check button
Timer1.Stop() 'Stops the timer
End Sub
I added a timer because the browser doesn't seem to validate the Enter key while in the WebBrowser1_DocumentCompleted method.
With this code, you can know if the id you entered is OK or not. It is not complete, but it's a good beginning, try to understand and adapt it for your needs.