Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Help with a VB software


  • Please log in to reply

#1
Masterchiefxx17

Masterchiefxx17

    New Member

  • Member
  • Pip
  • 2 posts
Hello! Im new here.

I just downloaded the Microsoft Visual Basic 2012 RC and wanted to create my first Windows program. I have a small knownledge on VB and know only basic coding, I also only have a small basic view on how the software works.

I wish to make a program where after the computer is idea for x time the computer will shut off. I also want to add that the software will only enable after 12:00 AM.

I have this code here:

Public Class Idle

	Private Sub Idle_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'You should have already set the interval in the designer... 
		Timer.Start()
	End Sub

	Private Sub Idle_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
		Timer.Stop()
		Timer.Start()
	End Sub

	Private Sub Idle_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
		Timer.Stop()
		Timer.Start()
	End Sub

	Private Sub Idle_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
		Timer.Stop()
		Timer.Start()
	End Sub

	Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick
		Application.Exit() 'I just have the program exiting, though you could have it do whatever you want.
	End Sub
End Class

But not sure how it all works or what do I need to add to the program so it works. I understand I need a timer but does this code go in the timer? How do I get the display of the program to look nice?

Thanks for the help! :help:
  • 0

Advertisements


#2
Mucius

Mucius

    Member

  • Member
  • PipPip
  • 77 posts
Well, it looks like you never initialized a timer, so I am not sure if you have one added to your form. Also, Timer is not a valid name, as it is already a class. So, inside of your Toolbox in Visual Studio, select the timer control, and drag and drop it onto your form. By default, the timer's name should be Timer1. Now, it won't appear, as it is not a graphical control. From inside of the properties panel, you can adjust the interval of the timer. This controls how many times the Timer.Tick handler is raised. Also, I don't think you are versed enough with Visual Basic in order to make that app you want. I suggest you try to become a bit more acquainted with the language.
  • 0

#3
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP
A timer wouldn't be required if you could override and receive Windows Messages as your event for the System Time check. For a beginner though, that's a bit of a stretch. A timer will do.

From what I see though, you're trying to start/stop a timer from it's class identifier. You need a unique identifier for an instance of the Timer class. If you added a timer to the form, by default the first one should be Timer1.

In your Timer_Tick event, you'll want to check and compare the system time to a time where you want your computer to shutdown.
  • 0

#4
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts
Hey Masterchief,

Here is some code that will not require you to use a timer control from the toolbox. Although you will still need to use your IDE to make your program look fancy and nice.

Public Class Form
    Private WithEvents myTimer As New Timer With {.Interval = 1000} ' The timer control with a set interval at 1 second

    Private sHour As Integer ' Shutdown hour (24 hour clock)
    Private sMinute As Integer ' Shutodwn minute
    Private appStartDay As Integer = Now.Day ' This will initialize the day to the current day the application was started
    Private appStartMonth As Integer = Now.Month ' This variable is to avoid logical errors at the end of the month

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the shutdown time (Eg. 01:10 AM) 
        sHour = 1
        sMinute = 10

        myTimer.Start() ' Start the timer
    End Sub

    Private Sub myTimer_Tick() Handles myTimer.Tick
        ' First check to make sure that it is passed 00:00 AM [This would assume a new day has started compared to the day the application was run]
        If (appStartDay < Now.Day) Then
            monitorShutdown()
        ElseIf (appStartDay > Now.Day And appStartMonth < Now.Month) Then ' This will ensure that if the new day is on a new month the program will still shutdown at the given time
            monitorShutdown()
        Else
            ' Program continues to monitor the time as normal until criteria of being enabled after 00:00 AM is met
        End If
    End Sub

    Private Sub monitorShutdown()
        If sHour = Now.Day And sMinute = Now.Minute Then
            Shell("Shutdown -s -t 0") ' Console command for restarting computer
        End If
    End SubEnd Class

There are several different approaches to doing what it is you would like to do...What I have displayed here should be sufficient enough to get your program working. I have added code for your requested :

I also want to add that the software will only enable after 12:00 AM.

Although I must make this clear, that this is absolutely unnecessary and not logical! I have provided you with the code in order to better understand the problem you were trying to solve. Please let me explain why you wanting to "

enable after 12:00 AM" is not logical. Technically speaking no matter what time of the day it is, it is always after 12:00AM. The only situation I could see this reasoning is if let's say it is 22:30 on the 2nd of June, then setting the shutdown time to 22:35 on the same day will not be executed in 5 minutes from the time set but instead executed on the 3rd of June at 22:35 (More than 24hrs later).

Anyways I hope this code helped you out and if you require anymore assistance I'm sure all of us will be glad to help. I do agree with Mucius though, "

I suggest you try to become a bit more acquainted with the language."

Peace Out Posted Image


  • 0

#5
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP

Hey Masterchief,

Here is some code that will not require you to use a timer control from the toolbox. Although you will still need to use your IDE to make your program look fancy and nice.

Public Class Form
    Private WithEvents myTimer As New Timer With {.Interval = 1000} ' The timer control with a set interval at 1 second

    Private sHour As Integer ' Shutdown hour (24 hour clock)
    Private sMinute As Integer ' Shutodwn minute
    Private appStartDay As Integer = Now.Day ' This will initialize the day to the current day the application was started
    Private appStartMonth As Integer = Now.Month ' This variable is to avoid logical errors at the end of the month

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the shutdown time (Eg. 01:10 AM) 
        sHour = 1
        sMinute = 10

        myTimer.Start() ' Start the timer
    End Sub

    Private Sub myTimer_Tick() Handles myTimer.Tick
        ' First check to make sure that it is passed 00:00 AM [This would assume a new day has started compared to the day the application was run]
        If (appStartDay < Now.Day) Then
            monitorShutdown()
        ElseIf (appStartDay > Now.Day And appStartMonth < Now.Month) Then ' This will ensure that if the new day is on a new month the program will still shutdown at the given time
            monitorShutdown()
        Else
            ' Program continues to monitor the time as normal until criteria of being enabled after 00:00 AM is met
        End If
    End Sub

    Private Sub monitorShutdown()
        If sHour = Now.Day And sMinute = Now.Minute Then
            Shell("Shutdown -s -t 0") ' Console command for restarting computer
        End If
    End SubEnd Class

There are several different approaches to doing what it is you would like to do...What I have displayed here should be sufficient enough to get your program working. I have added code for your requested :

I also want to add that the software will only enable after 12:00 AM.

Although I must make this clear, that this is absolutely unnecessary and not logical! I have provided you with the code in order to better understand the problem you were trying to solve. Please let me explain why you wanting to "

enable after 12:00 AM" is not logical. Technically speaking no matter what time of the day it is, it is always after 12:00AM. The only situation I could see this reasoning is if let's say it is 22:30 on the 2nd of June, then setting the shutdown time to 22:35 on the same day will not be executed in 5 minutes from the time set but instead executed on the 3rd of June at 22:35 (More than 24hrs later).

Anyways I hope this code helped you out and if you require anymore assistance I'm sure all of us will be glad to help. I do agree with Mucius though, "

I suggest you try to become a bit more acquainted with the language."

Peace Out Posted Image


It's essentially the same thing, the only difference is that you are manually handling the events for the System.Windows.Forms.Timer and creating an instance of that class dynamically.

Edited by AceInfinity, 30 July 2012 - 05:50 PM.

  • 0

#6
Masterchiefxx17

Masterchiefxx17

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
Wow! Thanks for the great reply! :thumbsup:

To make myself clear I wanted the timer to run around 12:00AM between 5:00AM. I should have added that in. Sorry about that :happy: .


I can figure it out from here! Thanks again!!!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP