Hello I am a novice / mediocre VB programmer and I made a timer that simply need to update it self every second.
I started having doubts about weather or not it was working so I applied a msg box to my timers code it went off every second updating it self but the progress bar wont? why?
Dim power As PowerStatus = SystemInformation.PowerStatus
Dim percent As Single = power.BatteryLifePercent
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
ProgressBar1.Value = percent * 100
Label1.Text = percent * 100
End Sub
I have a power status and percent that takes the status and turn into a usable percentage then the progress bar uses that percentage but isnt updating like the msgBOX does why?
Well, your timer is probably working well and all, but you don't show where/how you get the updated value of SystemInformation.PowerStatus.BatteryLifePercent.
Maybe you're doing this somewhere else in your code, but as it is posted, you're always displaying the same value, so of course the progressbar is never going to change.
From what I can see from your question, everything should be working ok but I cant see that you have added Timer1.Interval = 1000 however you may have but this with the designer and not the coding side, however nevertheless here is how I did this project so you could see my working example just so you can be sure, If you have any problems let me know and i will do my best to help you out :)
Public Class Form1
Dim Timer1 As New Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 1000
AddHandler Timer1.Tick, AddressOf Timer1_Tick
Timer1.Start()
End Sub
Private Sub Timer1_Tick()
Dim POWER As PowerStatus = SystemInformation.PowerStatus
Dim PERCENT As Single = POWER.BatteryLifePercent
Dim ONCHARGE As PowerStatus = SystemInformation.PowerStatus
ProgressBar1.Value = PERCENT * 100
Label1.Text = "Power Remaining: " & PERCENT * 100 & "%"
If ONCHARGE.PowerLineStatus = PowerLineStatus.Online Then
Label2.Text = "Currently: Charging"
Else
Label2.Text = "Currently: Not Charging"
End If
End Sub
End Class
I added a Progressbar and two labels to the form, one of which is the computer battery and another of which will tell the user if the cable is unplugged in or not, i know you didnt ask for the cable status but i added it just incase you needed it :)
Happy Coding
Related
I have 2 textbox and 2 timer. I first run it in timer1
if not textbox1.text = textbox2.text then
timer2.start
else
msgbox "finished"
end if
But timer1 skip reading when textbox1 is equal to textbox2 and always makes an error.
Do I have a chance to run timer2 as much as textbox2 instead?
For example, if textbox2 is 3, then timer2 runs 3 times. I can get rid of the textbox1 = textbox2 control and run the code that is actually correct and does not work. Please help me. Thank you.
I'm having a hard time understanding the question, but I understand that you want to run some timer multiple times, and then continue to run another timer after x amount of times. This can be accomplished using something like this (I haven't tested this code):
Private WithEvents Timer1, Timer2, Timer3 As New System.Timers.Timer() With {.AutoReset = False}
Private NbTimesRun As New Dictionary(Of System.Timers.Timer, Integer) From {
{Timer1, 0},
{Timer2, 0},
{Timer3, 0}
}
Private Sub Timer1_Elapsed(sender as Object, e As EventArgs) Handles Timer1.Elapsed
Debug.Print("Timer1 elapsed")
' Do some stuff here
NbTimesRun(Timer1) += 1
If NbTimesRun(Timer1) == 3 Then
Timer2.Start()
Else
Timer1.Start()
End If
End Sub
Private Sub Timer2_Elapsed(sender as Object, e As EventArgs) Handles Timer2.Elapsed
Debug.Print("Timer2 elapsed")
' Do some stuff here
NbTimesRun(Timer2) += 1
If NbTimesRun(Timer2) == 3 Then
Timer3.Start()
Else
Timer2.Start()
End If
End Sub
Etc.
This is a pretty bad design pattern, though, and you should instead use a single timer, check the number of times it has run and call a method depending on that number of times, for example. Timers need to be disposed, so override Dispose and call TimerX.Dispose().
I have an infinite loop in this sub because I want the program to keep testing this process to see if the variable has changed. When I run the program in the debugger, nothing shows up, including the form however when I removed the infinite loop from the program, the form showed up again. Does anyone know why this is happening? I should also mention I've tried a DO LOOP as well. Can anyone help?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim greenCount As Integer
Dim looptest As Boolean = True
While looptest = True
For Each control In Me.Controls.OfType(Of Button)
If control.BackColor = Color.Lime Then
greenCount += 1
End If
Next
txtFielder.Text = greenCount.ToString
End While
End Sub
You need to get rid of all that code regardless. Depending on how you're changing the BackColor of those Buttons in the first place, updating the lime count might be best done there. Otherwise, you should be handling the appropriate event, e.g.
Private limeButtonCount As Integer = 0
Private Sub Buttons_BackColorChanged(sender As Object, e As EventArgs) Handles Button3.BackColorChanged,
Button2.BackColorChanged,
Button1.BackColorChanged
If DirectCast(sender, Button).BackColor = Color.Lime Then
limeButtonCount += 1
Else
limeButtonCount -= 1
End If
TextBox1.Text = limeButtonCount.ToString()
End Sub
Note that this code assumes that there are only two possible BackColor values and that all Buttons are not lime by default. If your scenario is a bit more complex than that then you may need to change a code a little, e.g.
Private limeButtonCount As Integer = 0
Private Sub Buttons_BackColorChanged(sender As Object, e As EventArgs) Handles Button3.BackColorChanged,
Button2.BackColorChanged,
Button1.BackColorChanged
limeButtonCount = Controls.OfType(Of Button)().Count(Function(b) b.BackColor = Color.Lime)
TextBox1.Text = limeButtonCount.ToString()
End Sub
Form.Load occurs before a form is displayed for the first time.
This means that you'll never see your form as long as you loop in this event. You probably want to use the Shown event instead.
I wanted my program to show a simple dialog form when the battery percentage of my laptop reaches 80%. I've used SystemInformation.PowerStatus.BatteryLifePercent
to achieve the same and have used a timer event to monitor the battery percentage changing while charging or discharging and check for the battery to reach 80% charge using the above mentioned method. Below is my code.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
TimerChargeMonitor.Interval = 100
TimerChargeMonitor.Enabled = True
End Sub
Private Sub TimerChargeMonitor_Tick(sender As Object, e As EventArgs) Handles TimerChargeMonitor.Tick
If SystemInformation.PowerStatus.BatteryLifePercent = 0.8 Then
NotifBox.Show()
TimerChargeMonitor.Enabled = False
End If
End Sub
The problem is it doesnt work. The dialog form doesnt show up when the battery percentage reaches 80% or any other number.
Your code needs some adjustments:
SystemInformation.PowerStatus.BatteryLifePercent returns a single.
It's better to test it with >= because its value might be slightly different from what you are expecting here.
Then, you have to stop your timer before showing a MessageBox (if that is what NotifBox.Show() is).
So your code would be:
If SystemInformation.PowerStatus.BatteryLifePercent >= 0.8 Then
TimerChargeMonitor.Stop()
NotifBox.Show()
End If
As a note, the tick interval seems way too low for this application.
Maybe set it to TimerChargeMonitor.Interval = 5000
(I don't know what the other timer is for, here).
Okay, so I am making a game, like cookie clicker, or in my case - http://www.silvergames.com/poop-clicker (don't ask...), so that when you click on the icon in the center, it changes the total value by adding 1. On the side, you have a picture which you click to increase the amount it generates automatically every second.
At the moment I have it like this:
The timer tics every second. If the total amount > the cost of upgrade then it shows the picture of the thing you click to upgrade.
When you click that picture -
The cost is taken away from the total amount.
It changes the amount of times you have used that upgrade by +1.
The automatic upgrades per second is changed by +1.
The Cost is increased by 10.
What is happening is that I click the icon in the middle say 5 times (very quickly) and it only comes up with a total of 3. That in itself is a problem, but the even worse problem is that it shows the picture to click, when i told it to only show when the total value was > 10 (the cost of the upgrade).
I am really confused, and any help will be much appreciated.
Thanks
SkySpear
PS. Here's the Code -
Public Class Form1
Private Sub picPoop_Click(sender As Object, e As EventArgs) Handles picPoop.Click
lblPoops.Text = lblPoops.Text + 1
End Sub
Private Sub picCursor_Click(sender As Object, e As EventArgs) Handles picCursor.Click
lblPoops.Text = lblPoops.Text - lblCursorCost.Text
lblCursorAmmount.Text = lblCursorAmmount.Text + 1
lblPoopsPerSec.Text = lblPoopsPerSec.Text + 1
lblCursorCost.Text = lblCursorCost.Text + 10
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblCursorAmmount.Text = 0
lblCursorCost.Text = 10
lblBabyAmmount.Text = 0
lblBabyCost.Text = 100
lblBowlAmmount.Text = 0
picCursor.Hide()
tmrSec.Start()
End Sub
Private Sub tmrSec_Tick(sender As Object, e As EventArgs) Handles tmrSec.Tick
If lblPoops.Text > lblCursorCost.Text Then picCursor.Show()
End Sub
End Class
Again, don't ask where this ridiculous idea came from, I can assure you it wasn't mine.
Your main problem with this is that in your Timer sub, you are comparing text to text. In this case, a value of "3" > "21", since text comparisons work on a char by char basis. When this happens, your pictureBox is being shown. As others suggested, you can use any of the string to numeric conversion functions in your timer event to make this work better.
A slightly better approach would be to declare some class level numeric variables that hold each individual value and displays them when needed. As an example
numPoops += 1
lblPoops.Text = numPoops
This will make sure that all math will work correctly.
You are dealing with the Value of the textboxes, not the Text in it.
You should enclose each textbox with VAL() to get its exact value as a number.
Private Sub picPoop_Click(sender As Object, e As EventArgs) Handles picPoop.Click
lblPoops.Text = VAL(lblPoops.Text) + 1
End Sub
Private Sub picCursor_Click(sender As Object, e As EventArgs) Handles picCursor.Click
lblPoops.Text = VAL(lblPoops.Text) - VAL(lblCursorCost.Text)
lblCursorAmmount.Text = VAL(lblCursorAmmount.Text) + 1
lblPoopsPerSec.Text = VAL(lblPoopsPerSec.Text) + 1
lblCursorCost.Text = VAL(lblCursorCost.Text) + 10
End Sub
Private Sub tmrSec_Tick(sender As Object, e As EventArgs) Handles tmrSec.Tick
If VAL(lblPoops.Text) > VAL(lblCursorCost.Text) Then picCursor.Show()
End Sub
Okay, so I have nearly perfected a game called lucky 7 using visual studio 2010. I want to play a sound when I have won the game (got 7 on one of the three slots). Here is the code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For i = 0 To 2
slots(i) = rand.Next(10)
Next
Label1.Text = (slots(0).ToString)
Label2.Text = (slots(1).ToString)
Label3.Text = (slots(2).ToString)
If slots(0) = 7 Or slots(1) = 7 Or slots(2) = 7 Then
score = score + 100 'REM 10 points for each win
Label4.Text = (score.ToString)
PictureBox1.Visible = True 'REM If you have a PictureBox
PlaySound "C:\WINDOWS\MEDIA\TADA.WAV",
Else
PictureBox1.Visible = False
End If
If score = 500 Then
MsgBox("You Scored 500 Points!", vbInformation)
End If
End Sub
Thing is, I get an error on the 'playsound' statement and I really want to play that sound when the player wins the game!
I also tried to create a reset button, which sets the score and all three slots back to 0, but when I click it, nothing happens. Here is the code for the reset button:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
score = 0
PictureBox1.Visible = False
End Sub
Try using My.Computer.Audio.Play
Have you walked through the code to make sure nothing is happening on the button click event? Clicking to the far left of the line "score = 0" will set a break-point. This will pause the code when you run it. You can hover over the variable score, and it will show it's value. Pressing [F8] will execute it, and you can hover over it and see that it's changed.
From your reset button event, your score is reset to 0 but not the text. No change to the text of your 3 slots as well.
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Label1.Text = "0"
Label2.Text = "0"
Label3.Text = "0"
Label4.Text = "0"
score = 0
PictureBox1.Visible = False
End Sub
As someone mentioned the sound files may not appear on every computer. You should look into using the system beep sound which can be modified to make some weird chimes. I had a lot of fun with it...
As for your reset button... You haven't coded the labels text to reset. I'm guessing you only want a visual change, as the values will still be changed when you play again. However being a reset button i guess you may want to reset each labels text to string.empty or something.
You should try this to play a .wav sound file:
My.Computer.Audio.Play("C:\WINDOWS\MEDIA\TADA.WAV",AudioPlayMode.Background)