Я создаю систему аренды компьютеров и хочу, чтобы произошло следующее: пользователь выбирает использовать P C 1, триггеры таймера и P C 1 будет использоваться, другой пользователь входит в систему и выбирает P C, система уведомляет пользователя, что P C 1 используется, и не принимает их ввод.
Вот код для таймера:
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
Вот код для страницы аренды :
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
Сначала я попробовал такой код:
If ComoBox.SelectedItem Is "1" And Time1.Enabled Then
MessageBox.Show("This PC is already in use!")
End If
Но окно сообщения выскакивает, даже когда пользователь впервые выбирает P C 1 В любом случае система может уведомить пользователя только тогда, когда указанный элемент c в моем поле со списком выбран во второй раз, а таймер все еще работает для него? (О да, прежде чем кто-нибудь спросит, Time1, Time2 и т. Д. c. - отдельные таймеры для отдельных часов с 1 по 12. Я думаю, что есть более быстрый и эффективный способ сделать это, но я не знаю как)