In this comprehensive guide, we’ll walk you through the process of developing an Auto Logoff Timer using Visual Basic in Microsoft Visual Studio 2022. This custom program enables you to configure a countdown timer that will automatically log out users after a specified period, making it particularly useful for public spaces with local accounts that lack domain logins.

Setting Up the Timer

Our program operates as a startup application, initiating the countdown as soon as a user logs in. The timer is displayed in the bottom right-hand corner of the screen, remaining on top of all open windows. It undergoes color changes when there are 10 minutes and 2 minutes remaining in the session, providing visual cues to the user. If the timer runs out and the user does not log out within thirty seconds, it gracefully logs them out to enforce the session time limit.

Creating the User Interface

To get started, you will need Microsoft Visual Studio 2022. If you’re using an older version, it should work as well. You can download a free trial of Visual Studio from Microsoft’s website or use a fully licensed copy if available through your organization. Students may be eligible for a free version upon verifying their student status.

Here’s how to set up your project:

  1. Open Visual Studio 2022.
  2. Create a new project by going to File > New > Project or pressing CTRL+SHIFT+N.
  3. Under the Templates header, choose Visual Basic and select Windows Forms App (.NET Framework).
  4. Give your project a name and specify a location to save it.

I will be saving all my files to the following location for the time being:


Check the box Create directory for solution and click OK.

Configuring the User Interface

After creating your solution, you’ll see a new tab at the top of your screen called Form1.vb [Design]. Click on the newly created design form to select it. Follow these steps to configure the user interface in the Properties box in the lower right hand corner:

  • Set FormBorderStyle to “FixedDialog“, Font to Calibri Light, 8pt in the Appearance section.
  • Clear the Text entry to remove the title bar or Enter your own text.
  • Adjust the form’s width to 272 and height to 124 under Layout > Size.
  • Disable ControlBox, MaximizeBox, MinimizeBox, ShowIcon, ShowInTaskbar, and set TopMost to True in the Window Style section.

Adding Labels and Timers

Now that we’ve successfully configured the primary form, it’s time to add the title and timer display. On the left-hand side of your screen, you’ll find three tabs: Server Explorer, Toolbox, and Data Source. Navigate to the Toolbox tab and expand the Common Controls section. From there, simply drag and drop a label control onto your form.

Returning to the Toolbox tab, you’ll now add three more labels to your form: one for displaying minutes, another for the divider between minutes and seconds, and the last one for seconds. Use the same font but set the size to 15pt for consistency. In the minutes label, input the desired duration for the timer in minutes. For this guide, we’ll set our timer to run for 120 minutes. Use a colon for the divider label and input “00” for the seconds label.

After configuring all your labels, revisit the Toolbox tab one more time. This time, expand the Components section and drag and drop a timer component onto your form. Aim to position the “Time Remaining,” minutes, divider, and seconds labels as centrally as possible for a clean and organized display.

Writing the Visual Basic Code

Now, let’s dive into the code that powers the timer. We’ll go over the key code sections:

  1. Variable Definitions: Define essential variables like wsh, min, sec, x, and y.
  2. Form Initialization: Set initial properties for the form, including its location and timer settings.
  3. Timer Tick Event: Update the labels and trigger color changes, pop-up messages, and logoff functions as the timer counts down.

Begin by double-clicking the Timer1 component at the bottom of the screen to access the tab containing the form’s code.

At this point, the code might appear quite minimal, but we’re about to enhance it. Within the Public Class Form1 section, insert the following code:

Public Class Form1
Dim wsh As Object
Dim min As Integer
Dim sec As Integer
Dim x As Integer
Dim y As Integer

In this section, we’ve defined the data types for several essential variables. This informs the form that we’ll be utilizing Windows Script Host as an object, min and sec as integer variables to manage time, and x and y as integers for handling screen measurements. Now, let’s proceed to assign the initial values to these variables within our form.

In the “Private Sub Form1_Load,” you should copy and paste the following code:

x = Screen.PrimaryScreen.WorkingArea.Width - Width
y = Screen.PrimaryScreen.WorkingArea.Height - Height
Location = New Point(x, y)
Timer1.Enabled = True
Timer1.Interval = 1000
sec = 0
min = 120
wsh = CreateObject("WScript.Shell")

The only values you may need to adjust here is the “min” and “sec” variables, which should correspond to the desired timer duration in minutes and seconds.

For the “Private Sub Timer1_Tick,” please copy and paste the following code:

Label2.Text = Format(min, "0#")
Label5.Text = Format(sec, "0#")
sec = sec - 1

If sec < 0 Then
sec = 59
min = min - 1

If Label2.Text = "10" And Label5.Text = "00" Then
    Label1.ForeColor = Color.Black
    Label2.ForeColor = Color.Gold
    Label3.ForeColor = Color.Gold
    Label5.ForeColor = Color.Gold

    Dim userChoice = MessageBox.Show("You have 10 minutes of your session remaining. Press OK to continue with your session.", "Time Expiring", MessageBoxButtons.OK, MessageBoxIcon.Warning)

End If

If Label2.Text = "02" And Label5.Text = "00" Then
    Label1.ForeColor = Color.Black
    Label2.ForeColor = Color.Red
    Label3.ForeColor = Color.Red
    Label5.ForeColor = Color.Red

    Dim userChoice = MessageBox.Show("You have 2 minutes of your session remaining. Please save your work now. Press OK to continue with your session.", "Time Expiring", MessageBoxButtons.OK, MessageBoxIcon Warning)

End If

If Label2.Text = "00" And Label5.Text = "00" Then
    Timer1.Enabled = False
    System.Diagnostics.Process.Start("ShutDown.exe", "/f /r /t 30")

    Dim messageBoxResult = MessageBox.Show("Session time over! You will be automatically logged off in 30 seconds.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If

End If

This code manages the timer’s behavior, updates the display labels, and triggers messages and actions at specific time intervals. Make sure to customize the timer’s duration, label colors, and message text according to your preferences.

It’s important to note a few key details. The labels Label1 (representing minutes), Label2 (the colon separator), and Label3 (for seconds) correspond to the labels added to the form. Please ensure that the label names in your code match those found in the Design section of the Properties box.

Additionally, it’s worth mentioning that while the timer stops at 0:0 after 120 minutes, users are given a 30-second grace period to save their work and log out. Beyond this grace period, the system will automatically log users out. To adjust the length of this grace period, locate the line “System.Diagnostics.Process.Start(“ShutDown.exe”, “/f /r /t 30”)” and modify the value “30” to your desired grace period in seconds. You also have the flexibility to modify the timer’s colors and the content of the popup messages as needed.

This completes the necessary code for this application.

Building the Application

Now, it’s time to complete the application and save it as an executable. Follow these steps:

  1. In the Solution Explorer pane, right-click on “Visual Basic” and select “Properties.”
  2. To personalize your application, click the “Browse” button and choose an icon of your preference. You can search for a suitable icon online or select one that best suits your application.
  3. With the application coded and deployment information set, proceed to build the application. This step is distinct from publishing the application, as we intend to export it as an executable rather than an online installer.
  4. In the menu bar, navigate to the “Build” menu and choose the “Build Solution” option. The solution should build promptly, and you can monitor the progress in the “Output” window at the bottom.
  5. After the build process is complete, you can find your solution saved as an executable in the output directory, along with other configurations and files.

Once you’ve completed the code, add an icon and build the solution. The resulting executable found under the debug directory can be copied to your desired location on the C drive. The application directory is located at: C:\Users\user\source\repos\LogOnTimer\LogOnTimer\bin\Debug\net6.0-windows


By following this guide, you can create a user-friendly Auto Logoff Timer that enhances security and user session control, making it a valuable tool for managing shared computer systems in public spaces.