常用vb的编程代码

常用vb的编程代码,第1张

先在程序中加一个Label,名字应该符合VB程序控件命名规范:

lbl<名称>

再加一个Timer控件,名字也要符合命名规范

在窗体代码对话框中打入

Private Sub Form_Load()

On Error Resume Next

Me.Font = "Arial"

Me.FontSize = 24

Me.FontBold = True

<Timer控件名>.Enabled = True

<Timer控件名>.Interval = 1000

End Sub

'下列函数只能二选其一

'是时间显示

Private Sub <Timer控件名>_Timer()

On Error Resume Next

<Label控件名>.Caption = Time

End Sub

'是日期与时间显示

Private Sub <Timer控件名>_Timer()

On Error Resume Next

<Label控件名>.Caption = Now

End Sub

'万花筒程序

'粘贴下面代码即可, 不用添加任何控件

Private WithEvents Timer1 As Timer

Dim r&, r1&, t&, a1!, a2!, xb!, yb!, s!, b#

Private Sub Form_Load()

      Me.Width = 4500: Me.Height = 4500

      Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

      Me.AutoRedraw = True

      Me.Caption = "CBM666的万花筒"

      Set Timer1 = Controls.Add("vb.timer", "Timer1")

      Timer1.Interval = 10

End Sub

Private Sub Timer1_Timer()

      Randomize

      r = 340 * Rnd

      If r <> 0 Then

         r1 = 500

         s = r * Rnd

         b = RGB(256 * Rnd, 256 * Rnd, 256 * Rnd)

         For t = 1 To 10000

            a1 = t * 3.1415926 / 180

            a2 = (r1 / r) * a1

            xb = 500 + (-(r1 - r) * Cos(a1) - s * Cos(a2 - a1) + 420) * 4

            yb = 500 + ((r1 - r) * Sin(a1) - s * Sin(a2 - a1) + 380) * 4

            Me.PSet (xb, yb), b

         Next t

      End If

End Sub

分在多点,这些都给你搞定。

第一题

Dim k As Integer

Print "100-300的素数为"

For i = 100 To 300

k = 0

For j = 2 To i - 1

If i Mod j = 0 Then k = 1: Exit For

Next j

If k = 0 Then

Print i

p = p + 1

If p Mod 6 = 0 Then Print

End If

Next i

第二题

Private Sub Command1_Click()

Dim m As Integer, n As Integer, n1 As Integer, m1 As Integer, p As Integer

m = Val(Text1.Text)

n = Val(Text2.Text)

n1 = n: m1 = m

If m = 0 Or n = 0 Then

MsgBox "数据错误"

Else

If n >m Then t = n: n = m: m = t

p = m Mod n

Do While p <>0

m = n

n = p

p = m Mod n

Loop

Print "最大公约数"n"最小公倍数"n1 * m1 / n

End If

End Sub

Private Sub Form_Activate()

Text1.Text = ""

Text2.Text = ""

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii <48 Or KeyAscii >57 Then MsgBox "数据错误": KeyAscii = 0: Text1.Text = ""

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

If KeyAscii <48 Or KeyAscii >57 Then MsgBox "数据错误": KeyAscii = 0: Text1.Text = ""

End Sub

第三题

Dim a(1 To 10) As Integer, t As Integer

Print "排序前"

For i = 1 To 10

a(i) = Val(InputBox("请输入第" &i &"个数据", "数据输入"))

Print a(i)

If i Mod 6 = 0 Then Print

Next i

Print

For i = 1 To 9

For j = 1 To 10 - i

If a(j) >a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t

Next j

Next i

Print "排序后"

For i = 1 To 10

Print a(i)

If i Mod 6 = 0 Then Print

Next i

第四题

Dim a(10) As Integer, b(10) As Integer

a(1) = 1

a(2) = 1

Print Tab(8 * 2 + 1)" "Format(a(1), "00")" "

Print Tab(7 * 2 + 1)" "Format(a(1), "00")" "" "Format(a(2), "00")""

For i = 3 To 9

b(1) = 1: b(i) = 1

For j = 2 To i - 1

b(j) = a(j - 1) + a(j)

Next j

Print Tab((9 - i) * 2 + 1)

For j = 1 To i

Print " "Format(b(j), "00")" "

Next j

Print

For j = 1 To i

a(j) = b(j)

Next j

Next i


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11100189.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存