Jump to content

Visual Basic (Visual Basic Studio) code to run in backgroud and window always on top


Recommended Posts

Posted

I'm trying to get a code run in the background and autoupdate with out any prompt from myself, easier if I explain the program and what I've got;

 

I'm trying to make a basic program that will change the text of a label based on time,

Between 9:15 am and 10:30 am the label will show Lesson 1 etc.

 

I can get it to work as long as the window is active but I can't have the window active as I need to carry on with other peaces of work, I've tried load, active, deavtive and it just wont run in the background.

 

Also if there is a way for it to alwyas be in the foreground/window on top (not sure of the lingo) then that would be great to (it's just gonna be a small window in the bottom right of the screen when running etc.).

 

Thanks if any one can help :)

Posted

What are you using just a timer/label? If so that should work fine refreshing in the background without it being active. Do you have example code you're using? Just the form etc

 

Steve

  • Thanks 1
Posted (edited)

From, Label, combo box (explain in a moment), and thats about it atm, might add more later but it's early stages;

[color=#008000]'Varables for times of the day, st = Start, ed = End
[/color][color=#0000ff]Dim [/color]varBefore_SchoolSt [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varBefore_SchoolEd [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varForm_TimeSt [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varForm_TimeEd [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varLesson_1St [color=#0000ff]As String  [/color]      
[color=#0000ff]Dim [/color]varLesson_1Ed [color=#0000ff]As String [/color]       
[color=#0000ff]Dim [/color]varBreak1St [color=#0000ff]As String     [/color]   
[color=#0000ff]Dim [/color]varBreak1Ed [color=#0000ff]As String    [/color]    
[color=#0000ff]Dim [/color]varLesson_2St [color=#0000ff]As String      [/color]  
[color=#0000ff]Dim [/color]varLesson_2Ed [color=#0000ff]As String [/color]       
[color=#0000ff]Dim [/color]varBreak2St [color=#0000ff]As String        
Dim[/color] varBreak2Ed [color=#0000ff]As String        
Dim [/color]varLesson_3St [color=#0000ff]As String        
Dim[/color] varLesson_3Ed [color=#0000ff]As String        
Dim[/color] varLunchSt [color=#0000ff]As String        
Dim[/color] varLunchEd [color=#0000ff]As String        
Dim [/color]varLesson_4St [color=#0000ff]As String        
Dim[/color] varLesson_4Ed [color=#0000ff]As String        
Dim[/color] varAfterSchoolSt [color=#0000ff]As String        
Dim[/color] varAfterSchoolEd [color=#0000ff]As String[/color]        

[color=#008000]'Set varables with times (testing atm, will adjust depending on option selected in combobox)        [/color]
varBefore_SchoolSt = [color=#b22222]"07:00"[/color]        
varBefore_SchoolEd = [color=#b22222]"08:55" [/color]       
varForm_TimeSt = [color=#b22222]"08:55" [/color]       
varForm_TimeEd =[color=#b22222] "09:15"[/color]        
varLesson_1St = [color=#b22222]"09:15"[/color]        
varLesson_1Ed = [color=#b22222]"10:15"[/color]        
varBreak1St = [color=#b22222]"10:15"[/color]        
varBreak1Ed = [color=#b22222]"10:30"[/color]        
varLesson_2St = [color=#b22222]"10:30"[/color]        
varLesson_2Ed = [color=#b22222]"11:30"[/color]        
varBreak2St = [color=#b22222]"11:30"  [/color]      
varBreak2Ed = [color=#b22222]"11:45"  [/color]      
varLesson_3St = [color=#b22222]"11:45"    [/color]    
varLesson_3Ed = [color=#b22222]"12:45"     [/color]   
varLunchSt = [color=#b22222]"12:45"  [/color]      
varLunchEd = [color=#b22222]"13:30"    [/color]    
varLesson_4St = [color=#b22222]"13:30"  [/color]      
varLesson_4Ed = [color=#b22222]"14:30"[/color]        
varAfterSchoolSt = [color=#b22222]"14:30" [/color]       
varAfterSchoolEd = [color=#b22222]"17:40"[/color]        

[color=#008000]'Current time Varable  [/color]      
[color=#0000ff]Dim [/color]varTime [color=#0000ff]As String [/color]       
[color=#008000]'Current time varable set as hh:mm   [/color]     
varTime = Hour(TimeOfDay) & [color=#b22222]":"[/color] & Minute(TimeOfDay)        
[color=#008000]'Varable for result for bellow if statments        [/color]
[color=#0000ff]Dim [/color]varResult [color=#0000ff]As String[/color]        

[color=#008000]'If statments to where there current time falls under   [/color]     
[color=#008000]'Before school 07:00 - 08:55[/color]        
[color=#0000ff]If [/color]varTime >= varBefore_SchoolSt [color=#0000ff]And [/color]varTime <= varBefore_SchoolEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Before School[/color]
           varResult = [color=#b22222]"Before School"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Formtime 08:55 - 09:15 [/color]       
[color=#0000ff]ElseIf [/color]varTime >= varForm_TimeSt [color=#0000ff]And [/color]varTime <= varForm_TimeEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Form Time[/color]
           varResult = [color=#b22222]"Form Time"[/color]
           [color=#008000]'Set label to show the string in result varable
[/color]            lblLesson.Text = varResult            
[color=#008000]'Lesson 1 09:15 - 10:15[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varLesson_1St [color=#0000ff]And[/color] varTime <= varLesson_1Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 1[/color]
           varResult = [color=#b22222]"Lesson 1"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'1st Break 10:15 - 10:30[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varBreak1St [color=#0000ff]And [/color]varTime <= varBreak1Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Break[/color]
           varResult = [color=#b22222]"Break"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 2 10:30 - 11:30[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varLesson_2St [color=#0000ff]And [/color]varTime <= varLesson_2Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 2[/color]
           varResult = [color=#b22222]"Lesson 2"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'2nd Break 11:30 - 11:45[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varBreak2St [color=#0000ff]And [/color]varTime <= varBreak2Ed [color=#0000ff]Then[/color]
          [color=#008000] 'Set result varable to Break[/color]
           varResult = [color=#b22222]"Break"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 3 11:45 - 12:45 [/color]       
[color=#0000ff]ElseIf [/color]varTime >= varLesson_3St [color=#0000ff]And [/color]varTime <= varLesson_3Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 3[/color]
           varResult = [color=#b22222]"Lesson 3"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lunch 12:45 - 13:30   [/color]     
[color=#0000ff]ElseIf [/color]varTime >= varLunchSt [color=#0000ff]And [/color]varTime <= varLunchEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lunch[/color]
           varResult = [color=#b22222]"Lunch"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 4 13:30 - 14:30  [/color]      
[color=#0000ff]ElseIf [/color]varTime >= varLesson_4St [color=#0000ff]And [/color]varTime <= varLesson_4Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 4[/color]
           varResult = [color=#b22222]"Lesson 4"[/color]
          [color=#008000] 'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'After school 14:30 - 17:40   [/color]     
[color=#0000ff]ElseIf [/color]varTime >= varAfterSchoolSt [color=#0000ff]And [/color]varTime <= varAfterSchoolEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Afterschool[/color]
           varResult = [color=#b22222]"AfterSchool"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult        
[color=#0000ff]End If
[/color]

 

(Took a while to sort that mess of a code x_x), I've not included any subs as tbh it could go anywhere but that the general idea of the code, the combo box would determin the times being set and I can create a seperate ifstatment and a public varable to chnage the times accordingly etc. I'm not sure how to get it to auto refresh and run constantly in the background etc.

 

(Sure I've said this but I've slept since then lol), it's just a program to run in the bottom right of the screen and when the time changes the caption in the label changes depending on the time.

 

Hope that helps, thanks again :)

Edited by DPenfold
spelling (kinda)
Posted (edited)


[color=#0000ff]Private Sub[/color] Form1_Load(sender [color=#0000ff]As Object[/color], e [color=#0000ff]As [/color]EventArgs) [color=#0000ff]Handles Me[/color].Load
       Timer1.Enabled = [color=#0000ff]True
[/color]        Timer1.Start()

       [color=#add8e6]Me[/color].TopMost = [color=#0000ff]True[/color]
[color=#0000ff]End Sub

[/color][color=#0000ff]Private Sub[/color] Timer1_Tick(sender [color=#0000ff]As Object[/color], e [color=#0000ff]As [/color]EventArgs) [color=#0000ff]Handles [/color]Timer1.Tick
[color=#008000]'Varables for times of the day, st = Start, ed = End
[/color][color=#0000ff]Dim [/color]varBefore_SchoolSt [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varBefore_SchoolEd [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varForm_TimeSt [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varForm_TimeEd [color=#0000ff]As String[/color]        
[color=#0000ff]Dim [/color]varLesson_1St [color=#0000ff]As String  [/color]      
[color=#0000ff]Dim [/color]varLesson_1Ed [color=#0000ff]As String [/color]       
[color=#0000ff]Dim [/color]varBreak1St [color=#0000ff]As String     [/color]   
[color=#0000ff]Dim [/color]varBreak1Ed [color=#0000ff]As String    [/color]    
[color=#0000ff]Dim [/color]varLesson_2St [color=#0000ff]As String      [/color]  
[color=#0000ff]Dim [/color]varLesson_2Ed [color=#0000ff]As String [/color]       
[color=#0000ff]Dim [/color]varBreak2St [color=#0000ff]As String        
Dim[/color] varBreak2Ed [color=#0000ff]As String        
Dim [/color]varLesson_3St [color=#0000ff]As String        
Dim[/color] varLesson_3Ed [color=#0000ff]As String        
Dim[/color] varLunchSt [color=#0000ff]As String        
Dim[/color] varLunchEd [color=#0000ff]As String        
Dim [/color]varLesson_4St [color=#0000ff]As String        
Dim[/color] varLesson_4Ed [color=#0000ff]As String        
Dim[/color] varAfterSchoolSt [color=#0000ff]As String        
Dim[/color] varAfterSchoolEd [color=#0000ff]As String[/color]        

[color=#008000]'Set varables with times (testing atm, will adjust depending on option selected in combobox)        [/color]
varBefore_SchoolSt = [color=#b22222]"07:00"[/color]        
varBefore_SchoolEd = [color=#b22222]"08:55" [/color]       
varForm_TimeSt = [color=#b22222]"08:55" [/color]       
varForm_TimeEd =[color=#b22222] "09:15"[/color]        
varLesson_1St = [color=#b22222]"09:15"[/color]        
varLesson_1Ed = [color=#b22222]"10:15"[/color]        
varBreak1St = [color=#b22222]"10:15"[/color]        
varBreak1Ed = [color=#b22222]"10:30"[/color]        
varLesson_2St = [color=#b22222]"10:30"[/color]        
varLesson_2Ed = [color=#b22222]"11:30"[/color]        
varBreak2St = [color=#b22222]"11:30"  [/color]      
varBreak2Ed = [color=#b22222]"11:45"  [/color]      
varLesson_3St = [color=#b22222]"11:45"    [/color]    
varLesson_3Ed = [color=#b22222]"12:45"     [/color]   
varLunchSt = [color=#b22222]"12:45"  [/color]      
varLunchEd = [color=#b22222]"13:30"    [/color]    
varLesson_4St = [color=#b22222]"13:30"  [/color]      
varLesson_4Ed = [color=#b22222]"14:30"[/color]        
varAfterSchoolSt = [color=#b22222]"14:30" [/color]       
varAfterSchoolEd = [color=#b22222]"17:40"[/color]        

[color=#008000]'Current time Varable  [/color]      
[color=#0000ff]Dim [/color]varTime [color=#0000ff]As String [/color]       
[color=#008000]'Current time varable set as hh:mm   [/color]     
varTime = Hour(TimeOfDay) & [color=#b22222]":"[/color] & Minute(TimeOfDay)        
[color=#008000]'Varable for result for bellow if statments        [/color]
[color=#0000ff]Dim [/color]varResult [color=#0000ff]As String[/color]        

[color=#008000]'If statments to where there current time falls under   [/color]     
[color=#008000]'Before school 07:00 - 08:55[/color]        
[color=#0000ff]If [/color]varTime >= varBefore_SchoolSt [color=#0000ff]And [/color]varTime <= varBefore_SchoolEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Before School[/color]
           varResult = [color=#b22222]"Before School"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Formtime 08:55 - 09:15 [/color]       
[color=#0000ff]ElseIf [/color]varTime >= varForm_TimeSt [color=#0000ff]And [/color]varTime <= varForm_TimeEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Form Time[/color]
           varResult = [color=#b22222]"Form Time"[/color]
           [color=#008000]'Set label to show the string in result varable
[/color]            lblLesson.Text = varResult            
[color=#008000]'Lesson 1 09:15 - 10:15[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varLesson_1St [color=#0000ff]And[/color] varTime <= varLesson_1Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 1[/color]
           varResult = [color=#b22222]"Lesson 1"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'1st Break 10:15 - 10:30[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varBreak1St [color=#0000ff]And [/color]varTime <= varBreak1Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Break[/color]
           varResult = [color=#b22222]"Break"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 2 10:30 - 11:30[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varLesson_2St [color=#0000ff]And [/color]varTime <= varLesson_2Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 2[/color]
           varResult = [color=#b22222]"Lesson 2"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'2nd Break 11:30 - 11:45[/color]        
[color=#0000ff]ElseIf [/color]varTime >= varBreak2St [color=#0000ff]And [/color]varTime <= varBreak2Ed [color=#0000ff]Then[/color]
          [color=#008000] 'Set result varable to Break[/color]
           varResult = [color=#b22222]"Break"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 3 11:45 - 12:45 [/color]       
[color=#0000ff]ElseIf [/color]varTime >= varLesson_3St [color=#0000ff]And [/color]varTime <= varLesson_3Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 3[/color]
           varResult = [color=#b22222]"Lesson 3"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lunch 12:45 - 13:30   [/color]     
[color=#0000ff]ElseIf [/color]varTime >= varLunchSt [color=#0000ff]And [/color]varTime <= varLunchEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lunch[/color]
           varResult = [color=#b22222]"Lunch"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'Lesson 4 13:30 - 14:30  [/color]      
[color=#0000ff]ElseIf [/color]varTime >= varLesson_4St [color=#0000ff]And [/color]varTime <= varLesson_4Ed [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Lesson 4[/color]
           varResult = [color=#b22222]"Lesson 4"[/color]
          [color=#008000] 'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult            
[color=#008000]'After school 14:30 - 17:40   [/color]     
[color=#0000ff]ElseIf [/color]varTime >= varAfterSchoolSt [color=#0000ff]And [/color]varTime <= varAfterSchoolEd [color=#0000ff]Then[/color]
           [color=#008000]'Set result varable to Afterschool[/color]
           varResult = [color=#b22222]"AfterSchool"[/color]
           [color=#008000]'Set label to show the string in result varable[/color]
           lblLesson.Text = varResult        
[color=#0000ff]End If[/color]
[color=#0000ff]
[/color][color=#0000FF]End Sub[/color]

Think I got it (looked up timers), I've put the updated code above, just gotta figure out a way to keep it in the foreground now lol

 

UPDATE: It works AND I got it to stay the foreground, thanks for the input Steve21

Post can be closed now :)

Edited by DPenfold
Update
  • Thanks 1
Posted

Only a bit of a thought, but really the way you're doing that you don't need both variables (as in start and end of the lessons). As whether you use the elseif or switch it'll stop as soon as it hits one that's valid. Also you can just move the print to the end rather than repeating it

 

Example with cases, but same logic applies to if/elif (If past 10, then elseif past 9, won't go to the 9 if the 10 matches so no need to check if it's below the value), so could remove half the variables you're setting:

 

        varBefore_SchoolSt = "07:00"
       varForm_TimeSt = "08:55"
       varLesson_1St = "09:15"
       varBreak1St = "10:15"
       varLesson_2St = "10:30"
       varBreak2St = "11:30"
       varLesson_3St = "11:45"
       varLunchSt = "12:45"
       varLesson_4St = "13:30"
       varAfterSchoolSt = "14:30"

       Dim varTime As String
       varTime = Hour(TimeOfDay) & ":" & Minute(TimeOfDay)

       Dim varResult As String

       Select Case varTime
           Case Is >= varAfterSchoolSt
               varResult = "AfterSchool"
           Case Is >= varLesson_4St
               varResult = "Lesson 4"
           Case Is >= varLunchSt
               varResult = "Lunch"
           Case Is >= varLesson_3St
               varResult = "Lesson 3"
           Case Is >= varBreak2St
               varResult = "Break"
           Case Is >= varLesson_2St
               varResult = "Lesson 2"
           Case Is >= varBreak1St
               varResult = "Break"
           Case Is >= varLesson_1St
               varResult = "Lesson 1"
           Case Is >= varForm_TimeSt
               varResult = "Form Time"
           Case Is >= varBefore_SchoolSt
               varResult = "Before School"
           Case Else
               varResult = "??"
       End Select

       lblLesson.Text = varResult

  • Thanks 2
Posted

Nice I'll look into that, my knowledge was quite basic when I first did VB (well VB6 lol) in college and we didn't use cases then just if statements and I've been able to migrate that over to excel, some scripting and VBS.

 

Your code does clear up allot than repeated variables etc. took ageist to figure out to check a time between the start/end times etc.

 

I'll give it a try, thank you!! :)

  • Thanks 1
Posted

It'll be the same with IF's if you prefer them just reverse them and remove the doubles

 

e.g.

 

If varTime >= varForm_TimeSt Then
           varResult = "Form Time"
ElseIf varTime >= varBefore_SchoolSt  Then
           varResult = "Before School"


lblLesson.Text = varResult

 

etc

 

As if it hits the first IF, the ELIF ("else" if) won't trigger as it's not an else if that makes sense

 

Steve

  • Thanks 1
Posted
Only a bit of a thought, but really the way you're doing that you don't need both variables (as in start and end of the lessons). As whether you use the elseif or switch it'll stop as soon as it hits one that's valid. Also you can just move the print to the end rather than repeating it

 

Example with cases, but same logic applies to if/elif (If past 10, then elseif past 9, won't go to the 9 if the 10 matches so no need to check if it's below the value), so could remove half the variables you're setting:

 

        varBefore_SchoolSt = "07:00"
       varForm_TimeSt = "08:55"
       varLesson_1St = "09:15"
       varBreak1St = "10:15"
       varLesson_2St = "10:30"
       varBreak2St = "11:30"
       varLesson_3St = "11:45"
       varLunchSt = "12:45"
       varLesson_4St = "13:30"
       varAfterSchoolSt = "14:30"

       Dim varTime As String
       varTime = Hour(TimeOfDay) & ":" & Minute(TimeOfDay)

       Dim varResult As String

       Select Case varTime
           Case Is >= varAfterSchoolSt
               varResult = "AfterSchool"
           Case Is >= varLesson_4St
               varResult = "Lesson 4"
           Case Is >= varLunchSt
               varResult = "Lunch"
           Case Is >= varLesson_3St
               varResult = "Lesson 3"
           Case Is >= varBreak2St
               varResult = "Break"
           Case Is >= varLesson_2St
               varResult = "Lesson 2"
           Case Is >= varBreak1St
               varResult = "Break"
           Case Is >= varLesson_1St
               varResult = "Lesson 1"
           Case Is >= varForm_TimeSt
               varResult = "Form Time"
           Case Is >= varBefore_SchoolSt
               varResult = "Before School"
           Case Else
               varResult = "??"
       End Select

       lblLesson.Text = varResult

 

I'm trying the cases and it's off by a minute, like doing a test now 15:39, so I set Lesson 4 to 15:40 and After School for 15:41 but the label doesn't change until 15:40 (L4) and 15:41 (AS), it's like the code is ignoring the "=" part, I even added seconds to the variable in case that was the cause, weird!

Posted

I'm not sure what you mean, if you set L4 to 15:40 why would you want it to change at 15:39?

 

You got an example what you're trying code wise?

 

Steve

  • Thanks 1
Posted

when the real time hits 15:40 it doesn't change anything.... but when it turns to 15:41 the label shows Lesson 4

then same with After School but at 15:42 not 15:41

 

varLesson 4 = 15:40

VarAfterSchool = 1541

 

Current time = 15:39 (so no change)

Current time = 15:40 (no change? Should be Lesson 4)

Current time = 15:41 (Lesson 4, should be afterschool)

Current time = 15:42 (After school)

 

Code is the same as yours when you posted cases :), I can send over the project folder for MS Visual Studio if the forum will accept zipped files

Posted

Is this you running it manually, or with the timer? Nothing silly like your timer is only set to refresh once per minute? Just tried it here and it's working fine it seems with the current times

 

Think you can attach a zip to the post, but if not shout and can send over an email if it's easier

 

Steve

  • Thanks 1
Posted

Forum went down, worried if it was me as it wasn't uploaded haha, I've cleaned it up now so it looks decent and not a generic vb project, added comments etc., it has a try icon (and it will hide if closed - to close clock on the program, options > close - or click stop if your using VS etc.), not sure about the context menu? wouldn't appear for me, I think it comes up if you right click the tray icon but it wouldn't for me if you see it let me know.

 

The current variables ARE off, by 1 minute, at the moment due to the error I was having so just change them when you debug it etc.

 

no immediate rush at the moment still tinkering with it etc. feel free to use it yourself if you find a use (maybe new staff?)

Posted

It's because you changed your code to what you sent me :p

 

In the original one you used string:

Dim varLesson_4Ed As String

Dim varAfterSchoolSt As String

Dim varAfterSchoolEd As String

 

In this one you're using datetime:

Dim varLunch As DateTime

Dim varLesson_4 As DateTime

Dim varAfterSchool As DateTime

 

String will return exactly what you said e.g. 16:58

Comparing it to DateTime would make it like 16:58:00

 

So when doing a >= than it won't match until it's 16:59

 

If you change the variables back to String does it fix it for you?

 

Steve

  • Thanks 1
Posted

Alternatively if you did want DateTime instead of String, change:

 

varTime = Hour(TimeOfDay) & ":" & Minute(TimeOfDay)

 

to

 

varTime = TimeOfDay

 

Steve

  • Thanks 1
Posted
Alternatively if you did want DateTime instead of String, change:

 

varTime = Hour(TimeOfDay) & ":" & Minute(TimeOfDay)

 

to

 

varTime = TimeOfDay

 

Steve

 

God F*&@"$% Damn it! lol I changed it from string as it caused an error and i found the time variables and it worked, I've kept it all time based for now, so you can adjust the code to above and increase variables my a minute etc.

 

I just gotta figure out the context menu for the tray icon, something for tomorrow as I need to head home lol Thanks again!!!! (note: when I add the note information who made it version etc. I'll put your name in as you did help out too!!)

Posted

Sometimes it's the simplest of fixes that take the longest to find! :p Yeah it'd be better to use the TimeOfDay option really, as doing it your original way you'll have problems when using times less than 10 minutes, as something like 09:05 would be converted to 9:5 using your hour and minute method, comparing the full datetime vs timeofday will give you down to second-based options

 

Steve

  • Thanks 1
Posted
Sometimes it's the simplest of fixes that take the longest to find! :p Yeah it'd be better to use the TimeOfDay option really, as doing it your original way you'll have problems when using times less than 10 minutes, as something like 09:05 would be converted to 9:5 using your hour and minute method, comparing the full datetime vs timeofday will give you down to second-based options

 

Steve

 

Think that was the problem I was getting in the mornings 07, 08, 09 but chances are I would have gotten it on minutes when I sort the other times out and add custom times, its running smoothly at the moment shwoing "before school" when i logged in this morning.

 

I'll let you know if I figure out the other stuff to lol

Posted

"Finished" the program if anyone was interested, based on time shows what the current period/lesson/break, has present times based on the school I work at (Normal, Lockdown (remote learning), Social Distancing and custom (for half days).

 

Feel free for anyone contact me if there is any issues I'll do my best to respond/fix anything etc.

  • Thanks 1
Posted

Found a few little niggles for you :p Or at least in my eyes.

 

For some reason you have it set to close the context menu (taskbar wise), if you click on TimeTable Type rather than Close?

 

You also have a few problems with your way you're doing the tick boxes, as they're looping around when you change one so you end up with the wrong items being fired, so for example I'm currently in "Li-Normal" but with "Lockdown selected", or can be in a stage where nothing is selected which errors out the page

 

The way you have it detecting box changes, will fire every time that box is ticked even if it's by your own code. So by default it's on SocialDistance, if I pick lockdown it'll fire ->

 

chkLockdown_CheckedChanged

Which will in turn fire off - chkSocialDistancing_CheckedChanged - As it's setting chkSocialDistancing

Which will then return fire - chkLockdown_CheckedChanged etc

 

And ends up with nothing being ticked in the options, that will then error out if you open/close it as nothing is selected :p

 

Looking good though! Would be good if you wanted to expand it to get it to save settings somewhere, as currently no-one else is really going to be able to modify it without recoding as if they change times/periods it'll lose it every restart :p

 

Steve

  • Thanks 1
Posted (edited)
Found a few little niggles for you :p Or at least in my eyes.

 

 

Great!

 

 

For some reason you have it set to close the context menu (taskbar wise), if you click on TimeTable Type rather than Close?

 

I noticed that couldn't find out why, it should be close via the menus (both) and the "X" minizine to tray

 

You also have a few problems with your way you're doing the tick boxes, as they're looping around when you change one so you end up with the wrong items being fired, so for example I'm currently in "Li-Normal" but with "Lockdown selected", or can be in a stage where nothing is selected which errors out the page

 

The way you have it detecting box changes, will fire every time that box is ticked even if it's by your own code. So by default it's on SocialDistance, if I pick lockdown it'll fire ->

 

chkLockdown_CheckedChanged

Which will in turn fire off - chkSocialDistancing_CheckedChanged - As it's setting chkSocialDistancing

Which will then return fire - chkLockdown_CheckedChanged etc

 

And ends up with nothing being ticked in the options, that will then error out if you open/close it as nothing is selected :p

 

Now this confused me (the program), it's a simple thing I did of it just making sure only one is ticked but normal wouldn't be ticked if I went to edit times and I can't figure out why, if you have better suggestions to enforce only one checkbox is ticked let me know :)

 

 

Looking good though! Would be good if you wanted to expand it to get it to save settings somewhere, as currently no-one else is really going to be able to modify it without recoding as if they change times/periods it'll lose it every restart :p

 

Steve

 

Now that is something I want to do (save files or something) but I don't know how in any shape or form and how to reference it! At first, I thought maybe a CSV/Excel file and tell the text boxes to pull the data from that but the issue is the program forces the excel to open in the background and close. I didn't want that, I wanted to be little more streamlined/smoother, to my understanding unless set in coding variables can't be saved/stored it's volatile/temp memory right?

Edited by DPenfold
Posted
I noticed that couldn't find out why, it should be close via the menus (both) and the "X" minizine to tray

 

Because you have it in the code :p Lines 249-253 (Private Sub CloseToolStripMenuItem_Click_1(sender As Object, e As EventArgs) Handles TimeTableTypeToolStripMenuItem.Click), assumed you put it in for a reason, just can't figure out why but guessing not then :D

 

Now this confused me (the program), it's a simple thing I did of it just making sure only one is ticked but normal wouldn't be ticked if I went to edit times and I can't figure out why, if you have better suggestions to enforce only one checkbox is ticked let me know :)

 

I would just change your event, take away the checkstate changes, and just use click so it fires when clicked on only (as you're doing the changes from the menus in the other forms code)

 

e.g.

 

Private Sub chkSocialDistancing_Click(sender As Object, e As EventArgs) Handles chkSocialDistancing.Click

chkNormal.Checked = False

chkLockdown.Checked = False

chkCustom.Checked = False

End Sub

 

Now that is something I want to do (save files or something) but I don't know how in any shape or form and how to reference it! At first, I thought maybe a CSV/Excel file and tell the text boxes to pull the data from that but the issue is the program forces the excel to open in the background and close. I didn't want that, I wanted to be little more streamlined/smoother, to my understanding unless set in coding variables can't be saved/stored it's volatile/temp memory right?

 

At the very basic level you could just use a text file that's read from when the program opens and writes changes to when closed. So it's then populating your variables from the text file if it exists, else uses the defaults. Obviously can get a lot more complex but good starting point

 

Steve

Posted
Because you have it in the code :p Lines 249-253 (Private Sub CloseToolStripMenuItem_Click_1(sender As Object, e As EventArgs) Handles TimeTableTypeToolStripMenuItem.Click), assumed you put it in for a reason, just can't figure out why but guessing not then :D

 

I actually didn't know which was which as the both had close lol, but sorted, removed and even made the menu appear as right mouse click lol

 

I would just change your event, take away the checkstate changes, and just use click so it fires when clicked on only (as you're doing the changes from the menus in the other forms code)

 

e.g.

 

Private Sub chkSocialDistancing_Click(sender As Object, e As EventArgs) Handles chkSocialDistancing.Click

chkNormal.Checked = False

chkLockdown.Checked = False

chkCustom.Checked = False

End Sub

 

 

I got mixed up first (still tired and dyslexia over looking changed state to click etc.) but I got what you mean and changed it, I've also added fix to identify what the current selection is when edit form opens up;

 

       [color=#008000]'checks the current  timetable type and applies it to the check box
[/color]        [color=#0000ff]If[/color] frmMain.TimeTableType = [color=#b22222]"Normal"[/color] [color=#0000ff]Then
[/color]            chkNormal.Checked = [color=#0000ff]True
[/color]        [color=#0000ff]ElseIf[/color] frmMain.TimeTableType = [color=#b22222]"Lockdown"[/color] [color=#0000ff]Then
[/color]            chkLockdown.Checked = [color=#0000ff]True
[/color]        [color=#0000ff]ElseIf[/color] frmMain.TimeTableType = [color=#b22222]"SocialDistancing"[/color] [color=#0000ff]Then
[/color]            chkSocialDistancing.Checked = [color=#0000ff]True
[/color]        [color=#0000ff]ElseIf [/color]frmMain.TimeTableType = [color=#b22222]"Custom"[/color] [color=#0000ff]Then
[/color]            chkCustom.Checked = [color=#0000ff]True
[/color]        [color=#0000ff]End If
[/color]

and in the clicked check boxes it changes the form (now that I know I can do this)

       [color=#008000]'Changes the variable in main form[/color]
       frmMain.TimeTableType = [color=#b22222]"Lockdown"[/color]

So now, when user opens program and goes to edit it will have the default social distancing selected, from here if the user changes the type in either menu or check boxes it will all sync correctly :)

At the very basic level you could just use a text file that's read from when the program opens and writes changes to when closed. So it's then populating your variables from the text file if it exists, else uses the defaults. Obviously can get a lot more complex but good starting point

 

Steve

 

I have an idea, I got a test working but it's fair bit of coding plus figuring out how to do a fail safe where it can create the file (maybe if statement if file not exist, created it using the default values etc.), my test works nicely and saves but doesn't take in to account if use changes a text box (not the saved file), to lightly explain the test i did;

 

had richtextbox, I can call a file (.ini/textfile) and loaded it in to the textbox;

 

test1: Hello

test2: Right

test3: Bye

 

The code can read each line, if statement triggers if it finds "test 2". If statement removes "test 2:" and stores remains in label, then reads next line etc.

 

[color=#008000]'file location
[/color][color=#0000ff]Dim [/color]FILE_NAME [color=#0000ff]As String[/color] = [color=#a52a2a]"C:\test.ini"[/color] [color=#008000]'Will add code to determine program location and use that to store/load .ini[/color][color=#008000]'loads .ini and extracts needed variables
[/color][color=#0000ff]If[/color] [color=#a9a9a9]System[/color].IO.[color=#40e0d0]File[/color].Exists(FILE_NAME) = [color=#0000ff]True Then
[/color]            RichTextBox1.Clear()
           RichTextBox1.Text = [color=#a9a9a9]System[/color].IO.[color=#40E0D0]File[/color].ReadAllText(FILE_NAME)
       [color=#0000ff]Else
[/color]            MessageBox.Show([color=#a52a2a]"File Does Not Exist"[/color])
       [color=#0000ff]End If
[/color]        [color=#0000ff]For Each[/color] line [color=#0000ff]As String In[/color] RichTextBox1.Lines
           [color=#0000ff]If[/color] line.Contains("test1") [color=#0000ff]Then
[/color]                lblRB1.Text = line.Replace[color=#a52a2a]("test1: "[/color], [color=#a52a2a]""[/color])
           [color=#0000ff]ElseIf[/color] line.Contains("test2") [color=#0000ff]Then
[/color]                lblRB2.Text = line.Replace([color=#a52a2a]"test2: "[/color], [color=#A52A2A]""[/color])
           [color=#0000ff]ElseIf [/color]line.Contains("test3") [color=#0000ff]Then
[/color]                lblRB3.Text = line.Replace([color=#a52a2a]"test3: "[/color], [color=#A52A2A]""[/color])
           [color=#0000ff]End If
[/color]        [color=#0000ff]Next[/color][color=#008000]'saves text box to file
[/color][color=#a9a9a9]System[/color].IO.[color=#40E0D0]File[/color].WriteAllText(FILE_NAME, RichTextBox1.Text)

 

I'd most likely put in a 3rd form "advance edit" or something and use the same set up buttons of Load, Save, Update and Ok, when pulling info from the textboxes in the "simple" edit, I can just pull the textboxes together adding a label (test2: )and rewrite it in the textbox before it saves (same way for it to create the .ini) so that way I can have a back and forth situation; edit in the simple textboxes and import it to .ini or import from .ini into the simple textboxes (think I explained that well enough x_x)

 

Another small edit i might add is the time stamps in the main form and maybe current time too thank fully it's just simple labels and pull existing variables xD

 

Sorry for the mass read x_x

Posted
The test worked, loading/saving to .ini file but I've not implemented it to the the program yet, I'm just racking my brain to get visual basic to run a PowerShell script/code (or batch to launch the PowerShell script), I'll most likely update the program Thursday or Friday
Posted (edited)

Sorry to jump into the thread but just wanted to say this is pretty neat idea for a program :). I had a few suggestions though for the way you trying to read/write data to your program to makes things a little easier. What I would do if it was me is the following.

 

1.) Save the data as a CSV file. As Steve mentioned, you get a whole lot more complicated but this is probably the easiest way for now. I've attached a sample below how I would lay it out

 

Normal,7:12:00,8:55:00,9:15:00,10:30:00,10:50:00,10:30:00,12:05:00,13:20:00,13:55:00,15:10:00
Lockdown,7:00:00,8:55:00,9:15:00,10:15:00,10:30:00,11:30:00,11:45:00,12:45:00,13:30:00,14:30:00
SocialDistance,7:00:00,8:50:00,9:10:00,10:20:00,10:30:00,12:15:00,11:45:00,13:05:00,13:35:00,14:50:00
Custom,7:00:00,8:55:00,9:15:00,10:15:00,10:30:00,11:30:00,11:45:00,12:45:00,13:30:00,14:30:00

 

2.) Read from the CSV file and at the same time update the textbox with the relevant data. This saves you putting the data to a richTextBox and then using string replace function to decipher the data. I would place the code in your formLoading event

 

'Open the file for reading
       Using reader As StreamReader = New StreamReader("times.csv")
           'Read the first line in the file and save to variables
           Dim line As String = reader.ReadLine
           While Not line Is Nothing
               'Keep looping till we have read all the lines
               'Split the current line everytime there is a comma and place int on array
               Dim strArr() As String = line.Split(",")
               'Get the first value in the array
               Select Case strArr(0)
                   Case "Normal"
                       'Update textboxes with relevant times
                       frmTimes.txtBefore_School_NTime.Text = strArr(1)
                       frmTimes.txtForm_Time_NTime.Text = strArr(2)
                       frmTimes.txtLesson_1_NTime.Text = strArr(3)
                       frmTimes.txtBreak1_NTime.Text = strArr(4)
                       frmTimes.txtLesson_2_NTime.Text = strArr(5)
                       frmTimes.txtBreak2_NTime.Text = strArr(6)
                       frmTimes.txtLesson_3_NTime.Text = strArr(7)
                       frmTimes.txtLunch_NTime.Text = strArr(8)
                       frmTimes.txtLesson_4_NTime.Text = strArr(9)
                       frmTimes.txtAfterSchool_NTime.Text = strArr(10)
                   Case "Lockdown"
                       'Update textboxes with relevant times
                       frmTimes.txtBefore_School_LTime.Text = strArr(1)
                       frmTimes.txtForm_Time_LTime.Text = strArr(2)
                       frmTimes.txtLesson_1_LTime.Text = strArr(3)
                       frmTimes.txtBreak1_LTime.Text = strArr(4)
                       frmTimes.txtLesson_2_LTime.Text = strArr(5)
                       frmTimes.txtBreak2_LTime.Text = strArr(6)
                       frmTimes.txtLesson_3_LTime.Text = strArr(7)
                       frmTimes.txtLunch_LTime.Text = strArr(8)
                       frmTimes.txtLesson_4_LTime.Text = strArr(9)
                       frmTimes.txtAfterSchool_LTime.Text = strArr(10)
                   Case "SocialDistance"
                       'ETC
                   Case "Custom"
                       'ETC
               End Select
               'Move onto the next line in the file
               line = reader.ReadLine
           End While
       End Using

 

3.) Write data back to the text file once changes have been made

 

           'Save changes
           Using writer As StreamWriter = New StreamWriter("times.csv")
               'Normal Times
               writer.WriteLine("Normal," & txtBefore_School_NTime.Text & "," & txtForm_Time_NTime.Text & "," & txtLesson_1_NTime.Text & "," & txtBreak1_NTime.Text & "," & txtLesson_2_NTime.Text & "," & txtBreak2_NTime.Text & "," & txtLesson_3_NTime.Text & "," & txtLunch_NTime.Text & "," & txtLesson_4_NTime.Text & "," & txtAfterSchool_NTime.Text)
           End Using

 

Note you will need to place the following code at the top of your file to get the StreamReader/StreamWriter working

 

Imports System.IO

 

Hope this is useful :)

Edited by Dragnog
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...