vb.net倒计时器(3.0,版本优化,最原始的配置文件)

我现在还没有去学习xml文档、json、ini配置,只能使用老土的txt文件保存配置,按行读取配置(这也是我之前开发计时器时想到的,没想到一个晚上就把程序搞定了)

简单介绍新功能:

1、倒计时界面、主界面,可以随窗体的大小改变始终保持在窗体中央(核心是一行代码和一个panel控件)

2、使用鼠标滑动label来调整数值大小(核心是鼠标MouseDown,MouseUp事件的利益)

3、配置文件(核心是:不熟练的配置文件,不要取笑……)

好吧:上图

dc731a61268f4080ac5b315695c7d634.png

panel是这个虚线框(注意,本代码通俗易懂,部分变量不是原装变量名称,比如Label1=L1)

Imports System.IO

Public Class Main
    Dim ms As String
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        L1.Text += 1
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        L2.Text += 1
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        L5.Text += 1
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        L4.Text += 1
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        L3.Text += 1
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        L6.Text += 1
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        L1.Text -= 1
    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        L5.Text -= 1
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        L4.Text -= 1
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        L3.Text -= 1
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        L2.Text -= 1
    End Sub

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        L6.Text -= 1
    End Sub

    Private Sub L1_TextChanged(sender As Object, e As EventArgs) Handles L1.TextChanged
        a(L1, 0, 2)
        If L1.Text = 2 Then
            If L2.Text > 3 Then
                L2.Text = 3
            End If
        End If
    End Sub
    Private Sub L2_TextChanged(sender As Object, e As EventArgs) Handles L2.TextChanged
        a(L2, 0, 9)
        If L1.Text = 2 Then
            If L2.Text > 3 Then
                L2.Text = 3
            End If
        End If
    End Sub
    Private Sub L3_TextChanged(sender As Object, e As EventArgs) Handles L3.TextChanged
        a(L3, 0, 5)
    End Sub
    Private Sub L4_TextChanged(sender As Object, e As EventArgs) Handles L4.TextChanged
        a(L4, 0, 9)
    End Sub
    Private Sub L5_TextChanged(sender As Object, e As EventArgs) Handles L5.TextChanged
        a(L5, 0, 5)
    End Sub
    Private Sub L6_TextChanged(sender As Object, e As EventArgs) Handles L6.TextChanged
        a(L6, 0, 9)
    End Sub
    Public Sub a(l As Label, min As Integer, max As Integer)
        If l.Text < min Then
            l.Text = max
        ElseIf l.Text > max Then
            l.Text = min
        End If
    End Sub
    Public Sub MoveMouseLabel(l As Label, p1 As Point, p2 As Point)
        Dim y1 As Integer = p1.Y
        Dim y2 As Integer = p2.Y
        If y1 < y2 Then
            l.Text -= 1
        ElseIf y1 > y2 Then
            l.Text += 1
        End If
    End Sub
    Dim l1y As Point
    Dim l2y As Point
    Dim l3y As Point
    Dim l4y As Point
    Dim l5y As Point
    Dim l6y As Point
    Private Sub L1_MouseDown(sender As Object, e As MouseEventArgs) Handles L1.MouseDown
        l1y = New Point(e.X, e.Y)
    End Sub

    Private Sub L1_MouseUp(sender As Object, e As MouseEventArgs) Handles L1.MouseUp
        MoveMouseLabel(L1, l1y, New Point(e.X, e.Y))
    End Sub

    Private Sub L2_MouseDown(sender As Object, e As MouseEventArgs) Handles L2.MouseDown
        l2y = New Point(e.X, e.Y)
    End Sub

    Private Sub L2_MouseUp(sender As Object, e As MouseEventArgs) Handles L2.MouseUp
        MoveMouseLabel(L2, l2y, New Point(e.X, e.Y))
    End Sub

    Private Sub L3_MouseDown(sender As Object, e As MouseEventArgs) Handles L3.MouseDown
        l3y = New Point(e.X, e.Y)
    End Sub

    Private Sub L3_MouseUp(sender As Object, e As MouseEventArgs) Handles L3.MouseUp
        MoveMouseLabel(L3, l3y, New Point(e.X, e.Y))
    End Sub

    Private Sub L4_MouseDown(sender As Object, e As MouseEventArgs) Handles L4.MouseDown
        l4y = New Point(e.X, e.Y)
    End Sub

    Private Sub L4_MouseUp(sender As Object, e As MouseEventArgs) Handles L4.MouseUp
        MoveMouseLabel(L4, l4y, New Point(e.X, e.Y))
    End Sub

    Private Sub L5_MouseDown(sender As Object, e As MouseEventArgs) Handles L5.MouseDown
        l5y = New Point(e.X, e.Y)
    End Sub

    Private Sub L5_MouseUp(sender As Object, e As MouseEventArgs) Handles L5.MouseUp
        MoveMouseLabel(L5, l5y, New Point(e.X, e.Y))
    End Sub

    Private Sub L6_MouseDown(sender As Object, e As MouseEventArgs) Handles L6.MouseDown
        l6y = New Point(e.X, e.Y)
    End Sub

    Private Sub L6_MouseUp(sender As Object, e As MouseEventArgs) Handles L6.MouseUp
        MoveMouseLabel(L6, l6y, New Point(e.X, e.Y))
    End Sub

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        Form.Show()
        Config.TextBox1.Text = CUInt(L1.Text & L2.Text)
        Config.TextBox2.Text = CUInt(L3.Text & L4.Text)
        Config.TextBox3.Text = CUInt(L5.Text & L6.Text)
        Config.c = BackColor
        Config.Close()
        Form.start(CUInt(L1.Text & L2.Text), CUInt(L3.Text & L4.Text), CUInt(L5.Text & L6.Text), Me.BackColor, ms)
    End Sub

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        If IO.File.Exists(Application.StartupPath & "/me.config") = True Then
            Dim txt As New TextBox
            txt.Text = IO.File.ReadAllText(Application.StartupPath & "/me.config")


            Me.BackColor = Color.FromArgb(txt.Lines(0).ToString, txt.Lines(1).ToString, txt.Lines(2).ToString)

            If txt.Lines(3).ToString < 10 Then
                L1.Text = 0
                L2.Text = txt.Lines(3).ToString
            Else
                L1.Text = Mid(txt.Lines(3).ToString, 1, 1)
                L2.Text = Mid(txt.Lines(3).ToString, 2, 2)
            End If

            If txt.Lines(4).ToString < 10 Then
                L3.Text = 0
                L4.Text = txt.Lines(4).ToString
            Else
                L3.Text = Mid(txt.Lines(4).ToString, 1, 1)
                L4.Text = Mid(txt.Lines(4).ToString, 2, 2)
            End If

            If txt.Lines(5).ToString < 10 Then
                L5.Text = 0
                L6.Text = txt.Lines(5).ToString
            Else
                L5.Text = Mid(txt.Lines(5).ToString, 1, 1)
                L6.Text = Mid(txt.Lines(5).ToString, 2, 2)
            End If

            ms = txt.Lines(6).ToString

        End If
    End Sub

    Private Sub butconfig_Click(sender As Object, e As EventArgs) Handles butconfig.Click
        Config.ShowDialog()
    End Sub

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If IO.File.Exists(Application.StartupPath & "/me.config") = True Then
            Dim txt As New TextBox
            txt.Text = IO.File.ReadAllText(Application.StartupPath & "/me.config")


            Config.c = Color.FromArgb(txt.Lines(0).ToString, txt.Lines(1).ToString, txt.Lines(2).ToString)
            Config.Pic.BackColor = Color.FromArgb(txt.Lines(0).ToString, txt.Lines(1).ToString, txt.Lines(2).ToString)
            Config.H = txt.Lines(3).ToString
            Config.M = txt.Lines(4).ToString
            Config.S = txt.Lines(5).ToString

            Config.TextBox1.Text = txt.Lines(3).ToString
            Config.TextBox2.Text = txt.Lines(4).ToString
            Config.TextBox3.Text = txt.Lines(5).ToString

            Config.fn = txt.Lines(6).ToString
            Config.TextBox4.Text = txt.Lines(6).ToString
            Call Button14_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        Dim col As New ColorDialog
        col.ShowDialog()
        BackColor = col.Color
    End Sub
    Dim MovBoll As Boolean
    Dim CurrX As Integer
    Dim CurrY As Integer
    Dim MousX As Integer
    Dim MousY As Integer
    Private Sub Main_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        MousX = e.X
        MousY = e.Y
        MovBoll = True
    End Sub

    Private Sub Main_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        If MovBoll = True Then
            CurrX = Me.Left - MousX + e.X
            CurrY = Me.Top - MousY + e.Y
            Me.Location = New Point(CurrX, CurrY)
        End If
    End Sub

    Private Sub Main_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        MovBoll = False
    End Sub
    Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
        MousX = e.X
        MousY = e.Y
        MovBoll = True
    End Sub

    Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove
        If MovBoll = True Then
            CurrX = Me.Left - MousX + e.X
            CurrY = Me.Top - MousY + e.Y
            Me.Location = New Point(CurrX, CurrY)
        End If
    End Sub

    Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
        MovBoll = False
    End Sub

    Private Sub Main_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Panel1.Location = New Point(Width / 2 - Panel1.Width / 2, Height / 2 - Panel1.Height / 2)
    End Sub
End Class

这已经是优化过的第三次代码了,比我之前写的好太多了。

倒计时:

cd021d832ed345df830d0668078cf8c8.png

 较为基本的几样,添油加醋随你便

代码:

Imports System.Windows.Shell
Public Class Form
    Public H, M, S As Integer
    Dim ms As String
    Dim pros As Integer
    Dim taskpor As TaskbarItemProgressState
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        L3.Text -= 1
        Pro.Value += 1
    End Sub

    Private Sub Form_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Panel1.Location = New Point(Width / 2 - Panel1.Width / 2, Height / 2 - Panel1.Height / 2)
    End Sub

    Public Sub start(H As Integer, M As Integer, S As Integer, backcolor As Color, ms As String)
        L1.Text = H
        L2.Text = M
        L3.Text = S
        Me.H = H
        Me.M = M
        Me.S = S
        pros = S + M * 60 + H * 3600 + 1

        Pro.Minimum = 0
        Pro.Maximum = pros
        Timer1.Interval = 1000
        Timer1.Enabled = True
        Me.BackColor = backcolor
        Me.ms = ms
    End Sub

    Private Sub L1_TextChanged(sender As Object, e As EventArgs) Handles L1.TextChanged
        If L1.Text < 0 Then
            Timer1.Enabled = False
            L1.Text = 0
            L2.Text = 0
            L3.Text = 0
            Pro.Value = 0
            My.Computer.Audio.Play(ms)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If MsgBox("确定要重置计时器?", vbYesNo) = vbYes Then
            Timer1.Enabled = False
            L1.Text = H
            L2.Text = M
            L3.Text = S
            Pro.Value = 0
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If MsgBox("确定要结束计时器?", vbYesNo) = vbYes Then
            Timer1.Enabled = False
            L1.Text = 0
            L2.Text = 0
            L3.Text = 0
            Pro.Value = 0
        End If
    End Sub
    Private Sub L2_TextChanged(sender As Object, e As EventArgs) Handles L2.TextChanged
        If L2.Text < 0 Then
            L1.Text -= 1
            L2.Text = 59
            If Timer1.Enabled = False Then
                L2.Text = 0
            End If
        End If

    End Sub

    Private Sub L3_TextChanged(sender As Object, e As EventArgs) Handles L3.TextChanged
        If L3.Text < 0 Then
            L2.Text -= 1
            L3.Text = 59
            If Timer1.Enabled = False Then
                L3.Text = 0
            End If
        End If

    End Sub
End Class

二次优化,比之前写的关于vb.net初学者,倒计时器的开发

真的是好太多了

配置:f5a10a90aac840b59a362fec54ce8cf8.png

代码:

Public Class Config
    Public c As Color
    Public fn As String
    Public H, M, S As Integer
    Dim str As String

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        H = TextBox1.Text
        M = TextBox2.Text
        S = TextBox3.Text
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim open As New OpenFileDialog
        open.Filter = "wav files|*.wav"
        open.Title = "选择音频(*.wav)"
        open.InitialDirectory = "C:/Windows/Media"
        open.ShowDialog()
        If IO.File.Exists(open.FileName) = True Then
            TextBox4.Text = open.FileName
            fn = open.FileName
        End If
    End Sub

    Private Sub Config_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.BackColor = Main.BackColor
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim col As New ColorDialog
        col.ShowDialog()
        Pic.BackColor = col.Color
        c = col.Color
    End Sub

    Private Sub Config_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        If c = Nothing Then
            c = Color.LightBlue
        End If
        Call Button2_Click(Nothing, Nothing)
        str &= c.R & vbCrLf
        str &= c.G & vbCrLf
        str &= c.B & vbCrLf
        str &= H & vbCrLf
        str &= M & vbCrLf
        str &= S & vbCrLf
        str &= TextBox4.Text & vbCrLf
        IO.File.WriteAllText(Application.StartupPath & "/me.config", str)
        str = ""
    End Sub
End Class

 额。它不配使用.config的后缀,你们可以自己随便改

最后,把使用方法介绍一下,这是初始界面,没有配置

aecd6fb84d214987b5a5cc3a0c997036.png

 这是扩大的:

35ceab58a83d43f3a017c29915fbd776.png

 配置:

486c62313dbb48ca941c8adce659e4ce.png

 点击关闭直接就保存配置,点击加载配置:

b4f37b7912f74aeeb4a345bf646bbb45.png

 e4f77236e7164c27a7f8140921d5d741.png

 这,似乎有的恐怖!!!

关闭所有,重新启动:

617b4aaca36d46399cb0ddd9d1b4b3f9.png

 完成了!

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值