Enable a counter at a specific time everyday - vb.net

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub

Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

Related

create function for sender object handler

how do i create a function for a sender as object click? I tried something, but it didn't work out.
Private Function functionName(ByVal sender As Object, e As EventArgs)
If sender.checked = True Then
For i As Integer = 2 To 14
If i <> 2 Then
Dim cbClubs = DirectCast(Controls.Item("cbBtt" & i & "detrefla"), CheckBox) 'Clubs
cbClubs.Checked = False
End If
Next
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla, sender)
End Sub
Let's put some order in this code.
Private Function functionName(ByVal sender As Object, e As EventArgs)
'...
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla, sender)
End Sub
Why are you using e As EventArgs in functionName(...) declaration? It's never used, so u can delete it:
Private Function functionName(ByVal sender As Object)
In functionName you are evaluating sender.Checked (a tip: you can use If sender.Checked instead of If sender.Checked = True): why not to define sender as CheckBox, instead of Object?
Private Function functionName(ByVal sender As CheckBox)
Now, let's see the for Loop:
For i As Integer = 2 To 14
If i <> 2 Then
'...
End If
Next
You are evaluating i from 2 to 14, but you don't want to do anything if i = 2. Why not to start the for loop from i=3? It's faster and better.
For i As Integer = 3 To 14
'...
Next
Another thing: functionName() doesn't produce an output, so it should be a Sub, not a Function.
Now, your code is:
Private Sub functionName(ByVal sender As CheckBox)
If sender.Checked Then
For i As Integer = 3 To 14
'...
Next
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla)
End Sub
Furthermore, if functionName is called only by Button1_Click and here it is only used with cbBtt2detrefla, you can avoid separating the two subs:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cbBtt2detrefla.Checked Then
For i As Integer = 3 To 14
'...
Next
End If
End Sub
Last, but not least:
Dim cbClubs = DirectCast(Controls.Item("cbBtt" & i & "detrefla"), CheckBox) 'Clubs
cbClubs.Checked = False
This works only if cbBtt3detrefla and the others checkBoxes are directly on the form. If they are in a Panel, for example, you'll get a System.NullReferenceException. If you want to iterate on every control, you can do something like this.

StopWatch and Timer Accuracy

I'm using the following code to control a label to output a stopwatch. The buttons work, and the label is outputting the information mostly correctly, however it only update every so often and i would like for the label to update the information every millisecond.
Private SW As New Stopwatch
Dim timercount As Integer = 1 'The number of seconds
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SW.Start()
Timer1.Start()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer1.Enabled = False 'Stop the timer
timercount = 0 'Reset to 0 seconds
Label10.Text = "00:00:00.000"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = SW.Elapsed
Label10.Text = ts.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer1.enabled = False
End Sub
Any help is much appreciated.
You can set the timer to a reasonable value, say 20 times a second, to rapidly display it changing.
You can create the timer and stopwatch in code, which I think is easier than adding a control to the form—you can see right there in the code what is being programmed. I used sensible names for the buttons so that you can tell what is meant to do what.
If you keep the sub to display the data separate from the code that processes the data, it is easy to change the display in just one place, in case someone decided they wanted it written as words, for example, or maybe the hours in a separate box, or something.
Public Class Form1
Dim sw As New Stopwatch
Dim tim As Timer
Private Sub ShowElapsedTime()
lblTime.Text = sw.Elapsed.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub bnStart_Click(sender As Object, e As EventArgs) Handles bnStart.Click
sw.Restart()
tim.Enabled = True
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
sw.Stop()
tim.Enabled = False
ShowElapsedTime()
End Sub
Private Sub bnReset_Click(sender As Object, e As EventArgs) Handles bnReset.Click
tim.Enabled = False
sw.Reset()
ShowElapsedTime()
End Sub
Private Sub tim_Tick(sender As Object, e As EventArgs)
ShowElapsedTime()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tim = New Timer With {.Interval = 50}
AddHandler tim.Tick, AddressOf tim_Tick
ShowElapsedTime()
End Sub
End Class

Timer.tick within 1 min back-forth

I have 2 Form and i want it to be slide show back & forth within 1 minute.
Please write down the code thank you!
My Code:
Form 1
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE.Enabled = True
tmrSLIDE.Start()
Else
tmrSLIDE.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE.Tick
trs1.ShowSync(Form2)
Me.Hide()
End Sub
Form 2:
Private Sub BunifuCheckbox1_OnChange(sender As Object, e As EventArgs) Handles BunifuCheckbox1.OnChange
If BunifuCheckbox1.Checked Then
tmrSLIDE2.Enabled = True
tmrSLIDE2.Start()
Else
tmrSLIDE2.Stop()
End If
End Sub
Private Sub tmrSLIDE_Tick(sender As Object, e As EventArgs) Handles tmrSLIDE2.Tick
trs2.ShowSync(Form1)
Me.Hide()
End Sub
Property tmrSLIDE interval = 60000
property tmrSLIDE2 interval = 120000
I just Want to have 1 Checkbox with a simplified code thank you!

How to delay in adding items in list box vb.net

I am using for, while & do while loop in vb.net to add some values in list box. I want to use a timer to delay in adding values to list box. Please tell the syntax, what should I use & Following is my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer
For a = 1 To 10
ListBox1.Items.Add(a)
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim b As Integer
b = 4
If b < 5 Then
MessageBox.Show("eRROR")
End If
While b <= 3
ListBox2.Items.Add(b)
b = b + 1
End While
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim c As Integer
c = 20
Do
c = c + 1
ListBox3.Items.Add(c)
Loop While c <= 30
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
A timer executes once every n milliseconds, you take advantage of this by handling the timer's Tick event. If you wanted to keep track of what increment you're on, either declare a private variable outside of the scope (see here) of the Tick event or declare a static variable inside the Tick event.
Private Variable:
Private counter As Integer = 0
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
counter += 1
If (counter < 31) Then
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub
Static Variable:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Static counter As Integer = 0
If (counter < 31) Then
counter += 1
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub

How to make a clean and short code in vb.net

I'm pretty new here on Stack Overflow and this is my first time asking a question so please be kind to me if you find this question stupid or anything.
Does anyone know how can I make these codes short, I mean I would like to put all these codes into one line.. Please see the code below.
Private Sub PB_SearchP_MouseHover(sender As Object, e As EventArgs) Handles PB_SearchP.MouseHover
PB_SearchP.Image = My.Resources.search1
End Sub
Private Sub PB_SearchP_MouseLeave(sender As Object, e As EventArgs) Handles PB_SearchP.MouseLeave
PB_SearchP.Image = My.Resources.search
End Sub
Private Sub PB_AddP_MouseHover(sender As Object, e As EventArgs) Handles PB_AddP.MouseHover
PB_AddP.Image = My.Resources.add_1_iconhover
End Sub
Private Sub PB_AddP_MouseLeave(sender As Object, e As EventArgs) Handles PB_AddP.MouseLeave
PB_AddP.Image = My.Resources.add_1_icon
End Sub
Private Sub PB_New_MouseHover(sender As Object, e As EventArgs) Handles PB_New.MouseHover
PB_New.Image = My.Resources.newhover
End Sub
Private Sub PB_New_MouseLeave(sender As Object, e As EventArgs) Handles PB_New.MouseLeave
PB_New.Image = My.Resources.neww
End Sub
Private Sub PB_Save_MouseHover(sender As Object, e As EventArgs) Handles Btn_Save.MouseHover
Btn_Save.Image = My.Resources.savehover
End Sub
Private Sub PB_Save_MouseLeave(sender As Object, e As EventArgs) Handles Btn_Save.MouseLeave
Btn_Save.Image = My.Resources.save
End Sub
Private Sub PB_Update_MouseHover(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseHover
BTN_QUpdate.Image = My.Resources.edithover
End Sub
Private Sub PB_Update_MouseLeave(sender As Object, e As EventArgs) Handles BTN_QUpdate.MouseLeave
BTN_QUpdate.Image = My.Resources.edit
End Sub
A simpler solution is to use a function that sets-up lamba functions as event-handlers:
Public Shared Sub SetUpButton(btn As Button, normalImage As Image, hoverImage as Image)
AddHandler btn.MouseLeave, Sub(o, e) btn.Image = normalImage
AddHandler btn.MouseHover, Sub(o, e) btn.Image = hoverImage
End Sub
Called like so:
SetUpButton( Btn_Save, My.Resources.save, My.Resources.savehover )
SetUpButton( Btn_Update, My.Resources.update, My.Resources.updatehover )
SetUpButton( Btn_New, My.Resources.new, My.Resources.newhover )
and so on...
In C#, for a syntax comparison:
public static void SetUpButton(Button btn, Image normalImage, Image hoverImage) {
btn.MouseLeave += (o,e) => btn.Image = normalImage;
btn.MouseHover += (o,e) => btn.Image = hoverImage;
}