Is there anyway to alert the user when a timer is already running?

Viewed 12

I'm creating a computer rental system and what I want to happen is: User chooses to use PC 1, Timer triggers and PC 1 will be in use, Another user logs in and selects PC, System notifies user that PC 1 is in use and wont accept their input.

Here's the code for the timer:

Public Class Time1
Private DT As DateTime
Private Countdown As TimeSpan = TimeSpan.FromHours(1)
Dim ans As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Button3.Enabled = False
    Timer1.Interval = 1000
    DT = DateTime.Now.Add(Countdown)
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim ts As TimeSpan = DT.Subtract(DateTime.Now)
    If ts.TotalMilliseconds > 0 Then
        Label2.Text = ts.ToString("hh\:mm\:ss")
        Button3.Enabled = False
    Else
        Label2.Text = "00:00:00"
        Timer1.Stop()
        MessageBox.Show("Time's up! You can now extend your time or end your session.")
        Button3.Enabled = True
    End If
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If Timer1.Enabled Then
        Dim result As DialogResult = MessageBox.Show("You still have some time left. Are you sure you want to end your session now? Payment will not be reducted!", _
                           "End Time?", _
                           MessageBoxButtons.YesNo)

        If (result = DialogResult.Yes) Then
            Timer1.Stop()
            Close()
        Else
            Return
        End If


    End If
End Sub
End Class 

Here's the code for the rental page:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Me.Hide()
    Home1.Show()
End Sub

Private Sub Computer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'Comp_RentalDataSet.tblcomp' table. You can move, or remove it, as needed.
    Me.TblcompTableAdapter.Fill(Me.Comp_RentalDataSet.tblcomp)
    conn.ConnectionString = dbProvider & dbSource
    conn.Open()
    GetRecords()
    Me.Label3.Text = DateTime.Now.ToLongDateString

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ds = New DataSet
    adapter = New OleDbDataAdapter("insert into [tblcomp] ([Comp_ID], [User_ID], [Date_Today],[No_of_Hours], [Total_Payment]) VALUES " & "('" & ComboBox1.Text & "','" & TextBox2.Text & "','" & Label3.Text & "','" & ComboBox2.Text & "','" & Label2.Text & "')", conn)

    strsql = ("SELECT User_ID FROM tblusers WHERE User_ID='" + TextBox2.Text + "'")
    Dim cmd As New OleDbCommand(strsql, conn)
    reader = cmd.ExecuteReader
    If (reader.HasRows) Then

        adapter.Fill(ds, "tblcomp")
        TextBox2.Clear()
        ComboBox1.ResetText()
        ComboBox2.ResetText()
        Label2.ResetText()
        GetRecords()
        MessageBox.Show("Computer has been rented!")


    ElseIf (ComboBox1.Text = "" Or ComboBox2.Text = "" Or TextBox2.Text = "") Then
        MessageBox.Show("Please fill in the empty fields!")

    Else
        MessageBox.Show("The User ID is not recognized! Check your input for errors.")

    End If

    Label2.ResetText()

    If ComboBox2.SelectedItem Is "1" Then

        Time1.Show()

    ElseIf ComboBox2.SelectedItem Is "2" Then
        Time2.Show()


    ElseIf ComboBox2.SelectedItem Is "3" Then
        Time3.Show()


    ElseIf ComboBox2.SelectedItem Is "4" Then
        Time4.Show()


    ElseIf ComboBox2.SelectedItem Is "5" Then
        Time5.Show()


    ElseIf ComboBox2.SelectedItem Is "6" Then
        Time6.Show()


    ElseIf ComboBox2.SelectedItem Is "7" Then
        Time7.Show()


    ElseIf ComboBox2.SelectedItem Is "8" Then
        Time8.Show()


    ElseIf ComboBox2.SelectedItem Is "9" Then
        Time9.Show()


    ElseIf ComboBox2.SelectedItem Is "10" Then
        Time10.Show()


    ElseIf ComboBox2.SelectedItem Is "11" Then
        Time11.Show()


    ElseIf ComboBox2.SelectedItem Is "12" Then
        Time12.Show()
    End If

    If ComboBox2.SelectedItem Is "1" And Time1.Enabled Or Time2.Enabled Or Time3.Enabled Or Time4.Enabled Or Time5.Enabled Or Time6.Enabled Or Time7.Enabled Or Time8.Enabled Or Time9.Enabled Or Time10.Enabled Or Time11.Enabled Or Time12.Enabled Then

        MessageBox.Show("This PC is already in use!")
    End If
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    price = Convert.ToInt32(ComboBox2.SelectedItem)
    Label2.Text = CStr(price * 15)
End Sub

Public Sub GetRecords()
    ds = New DataSet
    adapter = New OleDbDataAdapter("select * from [tblcomp]", conn)
    adapter.Fill(ds, "tblcomp")

    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "tblcomp"

End Sub

End Class

At first, I tried a code like this:

   If ComoBox.SelectedItem Is "1" And Time1.Enabled Then
           MessageBox.Show("This PC is already in use!")
   End If

But the message box pops up, even when its the first time the user selects PC 1. Is there anyway the system can notify the user only when a specific item in my combo box is selected the second time and timer is still running for it? (Oh yeah, before anyone asks, Time1, Time2, etc. are seperate timers for the individual hours of 1-12. I think there's a faster and more efficient way of doing this, but I dont know how)

0 Answers
Related