VB程序学习代码记录20160721

本文提供了多个VBScript编程实例,包括清理字符串、实现抽奖功能、判断闰年、复制文件、调用与关闭外部程序等,通过这些实例展示了VBScript在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

清理字符串

Private Sub Command1_Click()
    Dim i As Integer
    Dim tex As String
    If Text1.Text = "" Then
        MsgBox "请输入字符串", vbInformation, "信息提示"
    Else
        tex = Trim(Text1.Text)
        i = 1
        Do While (Len(tex) - i) > 0
            tex = Left(tex, i) & Trim(Right(tex, Len(tex) - i))
            i = i + 1
        Loop
    End If
    Text2.Text = tex
End Sub

抽奖

Private Sub Command1_Click()
    Timer1.Enabled = True
    Command1.Enabled = False
    Command2.Enabled = True
End Sub

Private Sub Command2_Click()
    Timer1.Enabled = False
    Text6.Text = Text1.Text & Text2.Text & Text3.Text & Text4.Text & Text5.Text
    Command1.Enabled = True
    Command2.Enabled = flase
    Command3.Enabled = True
End Sub

Private Sub Command3_Click()
    End
End Sub

Private Sub Timer1_Timer()
    Text1.Text = Format(Int(Rnd * 9))
    Text2.Text = Format(Int(Rnd * 9))
    Text3.Text = Format(Int(Rnd * 9))
    Text4.Text = Format(Int(Rnd * 9))
    Text5.Text = Format(Int(Rnd * 9))
End Sub

判断闰年

Private Sub Command1_Click()
    If dates(Text1.Text) = 1 Then
        MsgBox "您输入的年份是闰年!", vbInformation, "信息提示"
    Else
        MsgBox "您输入的年份不是闰年!", vbInformation, "信息提示"
    End If
End Sub

Private Function dates(dat As Integer)
    If Val(Text1.Text) Mod 400 = 0 Then
        dates = 1
    End If
    If Val(Text1.Text) Mod 4 = 0 And Val(Text1.Text) Mod 100 <> 0 Then
        dates = 1
    End If
End Function

使用API函数copyfile进行复制文件

Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Form_Load()
    CopyFile "c:\Users\Qi\desktop\1.txt", "c:\Users\Qi\\desktop\12.txt", 0
    MsgBox "成功复制文件!", , "提示信息"
End Sub

关闭、打开exe程序,使用WinExec函数

Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long

Private Sub Command1_Click()
    WinExec ("mspaint.exe"), 1
End Sub

Private Sub Form_Load()
    Command1.Caption = "调用画图"
    Command2.Caption = "关闭画图"
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值