中易网

下面是我用vb写的闹钟代码,想加个限制响铃时间为30秒的限制,希望大家帮帮忙

答案:1  悬赏:80  
解决时间 2021-04-27 22:36
  • 提问者网友:無奈小影
  • 2021-04-27 15:22

Public Class Form1
    Private dt As DateTime
    Private sound As String
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        TextBox2.Text = TextBox1.Text
        TextBox3.Text = TextBox1.Text
        TextBox4.Text = TextBox1.Text
        TextBox5.Text = TextBox1.Text
        Timer1.Interval = 1000
        Timer2.Interval = 1000
        Timer3.Interval = 1000
        Timer4.Interval = 1000
        sound = Application.StartupPath & "\dongshi.mp3"
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If IsTime(TextBox2.Text) = False Then
            MsgBox("不是正确时间格式")
            TextBox2.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "停止" Then
            Button1.Text = "启动"
            Call mciSendString("stop MySound", "", 0, 0)
            Call mciSendString("close MySound", "", 0, 0)
        Else
            Timer1.Start()
            Timer2.Start()
            Timer3.Start()
            Timer4.Start()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        Me.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        dt = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        If dt = TextBox2.Text Then
            Button1.Text = "停止"
            Call mciSendString(String.Format("open ""{0}"" alias MySound", sound), "", 0, 0)
            Call mciSendString("play MySound", "", 0, 0)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ofd As New OpenFileDialog()

        ofd.FileName = ""

        'ofd.InitialDirectory = "C:\"

        ofd.Filter = _
          "mp3文件(*.mp3)|*.mp3|wave文件(*.wave)|*.wave|mid文件(*.mid)|*.mid"

        ofd.FilterIndex = 1

        ofd.Title = "请选择音乐"

        ofd.RestoreDirectory = True

        ofd.CheckFileExists = True

        ofd.CheckPathExists = True

        If ofd.ShowDialog() = DialogResult.OK Then

            sound = ofd.FileName

        End If
    End Sub

    Public Function IsTime(ByVal S As String) As Boolean
        'On Error GoTo Err
        Dim i As Date
        Try
            i = CDate(S)
        Catch ex As Exception
            IsTime = False
            Exit Function
        End Try


        If InStr(S, "/") <> 0 Then

            IsTime = False
        ElseIf InStr(S, ":") = 0 Then

            IsTime = False
            'ElseIf Int(i) <> 0 Then
            '    IsTime = False
        Else
            IsTime = True
        End If
        'Exit Function
        'Err:
        '        IsTime = False
    End Function


    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        TextBox1.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        Me.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        dt = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        If dt = TextBox3.Text Then
            Button1.Text = "停止"
            Call mciSendString(String.Format("open ""{0}"" alias MySound", sound), "", 0, 0)
            Call mciSendString("play MySound", "", 0, 0)
        End If
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        TextBox1.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        Me.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        dt = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        If dt = TextBox4.Text Then
            Button1.Text = "停止"
            Call mciSendString(String.Format("open ""{0}"" alias MySound", sound), "", 0, 0)
            Call mciSendString("play MySound", "", 0, 0)
        End If
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        TextBox1.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        Me.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        dt = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        If dt = TextBox5.Text Then
            Button1.Text = "停止"
            Call mciSendString(String.Format("open ""{0}"" alias MySound", sound), "", 0, 0)
            Call mciSendString("play MySound", "", 0, 0)
        End If
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If IsTime(TextBox3.Text) = False Then
            MsgBox("不是正确时间格式")
            TextBox3.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        End If

    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        If IsTime(TextBox4.Text) = False Then
            MsgBox("不是正确时间格式")
            TextBox4.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        End If

    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If IsTime(TextBox5.Text) = False Then
            MsgBox("不是正确时间格式")
            TextBox5.Text = Format("ToLongTimeString :{0}", DateTime.Now.ToLongTimeString())
        End If

    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
      
    End Sub

    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
       
    End Sub
End Class

最佳答案
  • 二级知识专家网友:社会水太深
  • 2021-04-27 16:30

响铃前加个if  看看是不是星期几就行了嘛


'星期 1,2,3,4  ,5 响铃  Weekday ()=1 是星期天  Weekday ()=7 是星期6 


if Weekday (Date)=2 or Weekday (Date)=3 or Weekday (Date)=4 or Weekday (Date)=5 or Weekday (Date)=6 then


     '响铃


end if

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息