65.9K
CodeProject 正在变化。 阅读更多。
Home

小型闹钟,由 VB.NET 编写

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.16/5 (15投票s)

2008年3月18日

CDDL

1分钟阅读

viewsIcon

62422

downloadIcon

4888

我的第一个程序。

引言

这是我的第一个使用 Visual Basic.Net 编写的程序,感觉自己在编程真是太棒了
一个程序,我的程序是一个小型闹钟,通过这个程序,你可以输入你想要播放声音的时间,你
也可以指定你想要使用的声音,它是一个为用户设计的小型闹钟,但对我来说它非常庞大而且困难。

我遇到的问题

在编程过程中,我遇到了很多关于这个程序的问题,但我可以通过思考解决所有问题,最大的
问题是我如何比较用户输入的时间和当前时间,所有开发人员
都知道我不能在任何子程序中使用公共变量,所以它
确实是个大问题,但我通过将变量设置为类的 friend 来解决它,在类中使用它非常困难,但
我做到了,另一个问题是
使用 DateTime 对象,但我通过创建自己的类来解决这个问题,该类包括三个字段、一个子程序和一个
函数,花了很长时间才找到解决方案
但这太棒了。

Form1 代码

Imports Microsoft.Win32
Imports System.TimeSpan
Imports NTime
Public Class Form1
    Public a, b As New NTime.NTime
    Public Bool As Boolean
    Public R As RegistryKey = Registry.CurrentUser
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Tray()
        NotifyIcon1.ShowBalloonTip(5, "Small Alarm", "Small Alarm wasn't closed", ToolTipIcon.Info)
        Timer1.Enabled = False
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NotifyIcon1.Visible = False
        Dim R As RegistryKey = Registry.CurrentUser
        R.CreateSubKey("SmallAlarm")
        Timer1.Interval = 1000
        Timer1.Enabled = True
        If R.OpenSubKey("SmallAlarm", True).GetValue("Tone") = "" Then
            MessageBox.Show("You must select tone via the options", "Tone Select", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Exit Sub
        End If
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
        NotifyIcon1.Visible = False
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub
    Friend Sub Tray()
        NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
        Me.Hide()
        NotifyIcon1.Icon = Me.Icon
        NotifyIcon1.Text = "Small Alarm"
        NotifyIcon1.Visible = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Tray()
        NotifyIcon1.ShowBalloonTip(5, "Small Alarm", "Click on the icon to show the options", ToolTipIcon.Info)
        Timer1.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.ShowDialog()
        Timer1.Enabled = False
    End Sub

    Private Sub TabPage1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage1.Enter
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub

    Private Sub TabPage1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage1.Leave
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Text = Date.Now
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim Time As Date = Date.Now
        a.Hour = Time.Hour
        a.Minute = Time.Minute
        a.Second = Time.Second
        Bool = b.Compare(a, b)
        If Bool = True Then
            W.URL = R.OpenSubKey("SmallAlarm", True).GetValue("Tone")
            Timer2.Enabled = False
            TextBox3.ReadOnly = False
            TextBox4.ReadOnly = False
            TextBox5.ReadOnly = False
            Button6.Enabled = False
            Button4.Enabled = True
        End If
    End Sub

    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If R.OpenSubKey("SmallAlarm", True).GetValue("Tone") = "" Then
            MessageBox.Show("You must select tone via the options", "Tone Select", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If
        If IsNumeric(TextBox3.Text) = True And IsNumeric(TextBox4.Text) = True And IsNumeric(TextBox5.Text) = True Then
            b.Hour = Convert.ToInt32(TextBox3.Text)
            b.Minute = Convert.ToInt32(TextBox4.Text)
            b.Second = Convert.ToInt32(TextBox5.Text)
            Timer2.Interval = 1000
            Timer2.Enabled = True
            TextBox3.ReadOnly = True
            TextBox4.ReadOnly = True
            TextBox5.ReadOnly = True
            Button6.Enabled = True
            Button4.Enabled = False
        Else
            MessageBox.Show("You entered wrong value(s)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        W.URL = ""
        Button4.Enabled = False
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Timer2.Enabled = False
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        TextBox5.ReadOnly = False
        Button6.Enabled = False
        Button4.Enabled = False
    End Sub

    Private Sub ShowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.Click
        Me.Show()
        NotifyIcon1.Visible = False
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub
End Class

Form2 代码

Imports Microsoft.Win32
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim R As RegistryKey = Registry.CurrentUser
        TextBox1.Text = R.OpenSubKey("SmallAlarm").GetValue("Tone")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim F As New OpenFileDialog()
        F.Filter = ("MusicFiles|*.WMV")
        F.FileName = ""
        F.ShowDialog()
        TextBox1.Text = F.FileName
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim R As RegistryKey = Registry.CurrentUser
        R.OpenSubKey("SmallAlarm", True).SetValue("Tone", TextBox1.Text)
        Form1.Timer1.Interval = 1000
        Form1.Timer1.Enabled = True
        Me.Close()
    End Sub
End Class


Features I Used

It was a problem to use a data base to store the URL of the tone, but I used the registry to store it, and I also used tray system.

The End

At the end of this article, I hope to see feedbacks and suggestions. Nabil Ismail El-Esawy

© . All rights reserved.