vb代码编写(VB编写个代码)

本文目录
VB编写个代码
通常情况下,我们使用Msgbox函数来写提示框信息。
根据你的描述,推想你大概需要下面这样的代码:
Private Sub Command1_Click()
If MsgBox("是否使用?", vbOKCancel + vbExclamation) = vbCancel Then
’vbokcancel 表示提示框上显示确认和取消两个按钮
’vbexclamation 提示框上显示感叹号
Text1.Text = "" ’清空Text1
Else
Exit Sub
End If
End Sub
Private Sub Command2_Click()
If MsgBox("Text2文本框显示的内容是:" & Text2.Text & ",清除Text2里面的内容?", vbYesNo + vbInformation) = vbYes Then
’vbyesno 表示提示框上显示是和否两个按钮
’vbinformation 表示提示框想显示一个信息提示i符号
Text2.Text = ""
End If
End Sub
你可以在窗体上加入command1,command2,text1,text2四个控件进行测试!
怎样vb代码编写一个用星号组成的五角星
Sub ss()
x1 = 20: y1 = 0
x2 = 1: y2 = 7
x3 = 39: y3 = 7
x4 = 8: y4 = 18
x5 = 32: y5 = 18
For i = 0 To 6
For j = 0 To 40
If j - x4 》= (x1 - x4) * (i - y4) / (y1 - y4) And j - x5 《= (x1 - x5) * (i - y5) / (y1 - y5) Then
Debug.Print "*";
Else
Debug.Print " ";
End If
Next
Debug.Print
Next
For i = 7 To 18
For j = 0 To 40
If (j - x5 》= (x2 - x5) * (i - y5) / (y2 - y5) And j - x5 《= (x1 - x5) * (i - y5) / (y1 - y5)) Or (j - x4 》= (x1 - x4) * (i - y4) / (y1 - y4) And j - x4 《= (x3 - x4) * (i - y4) / (y3 - y4)) Then
Debug.Print "*";
Else
Debug.Print " ";
End If
Next
Debug.Print
Next
End Sub
简述VB可视化开发程序的步骤
VB可视化编程的方法与步骤
1、新建一个工程
2、添加控件
3、设置属性
4、编写代码
5、运行工程
6、修改工程
用VB编程s=1!+2!+3!+.+n!
用VB编程s=1!+2!+3!+.+n!
Private Sub Command1_Click()
Dim n as integer , i as integer, s as integer , l as integer, k as integer
n = val(InputBox(""))
For l = 1 To n
s = 1
For i = l To 1 Step -1
s = s * i
Next i
k = k + s
Next l
Print k
End Sub
用VB编程: s=1*1!+2*2!+3*3!+4*4!+.+10*10!
Private Sub CommandButton1_Click()
Dim s As Double, n As Double
s = 0
For i = 1 To 10
n = 1
For j = 1 To i
n = n * j
Next j
s = s + i * n
Next i
Print s print s可以换其他输出方法
End Sub
vb编程求1×3×5×。×2N-1=?
Dim s As Single
Dim n As Integer
Dim i As Integer
s = 1
n = Val(InputBox("输入N"))
For i = 1 To n
s = s * (2 * i - 1)
Next i
Print s
用VB编程求S=1!+2!+3!+……+99!的值
dim s,i as integer,j
s=0
j=1
for i=1 to 99
j=j*i
s=s+j
next i
print s
说明:因为数太大,所有有可能溢出。
p=1×2×3×…×n(n! vb编程语言)
参考代码如下:
Private Function 求阶乘(Byval n As Integer) As Long
Dim i As Integer
Dim Fac As Long
Fac = 1
For i = 1 To n
Fac = Fac * i
Next i
求阶乘 = Fac
End Function
在你需要求阶乘的地方,调用该函数:
例如:
Dim y As Long
y = 求阶乘(10) ’求阶乘(10)将用10调用上面的自定义函数,返回10的阶乘
1*2*3…*n.电脑vb编程
Private Sub Command2_Click()
Dim i As Integer, n As Integer
n = Val(Trim(InputBox("请输入数值", "输入", "")))
If n = 0 Then Exit Sub
*** = 1
For i = 1 To n
*** = *** * i
Next
Print "1 ~ " & n & " 的积是:" & ***
End Sub
vb中1!+2!+3!+.+n! 怎么编程
递归
用VB编程求s=x/2!+x^3/4!+.+x^(2n-1)/(2n)的值
Private Sub Form_Click()
Dim n As Integer, x As Integer
n = Val(InputBox("请输入n的值"))
x = Val(InputBox("请输入X的值"))
For i = 2 To 2 * n Step 2
jc = 1
For j = 1 To i
jc = jc * j
Next
s = s + x ^ (2 * n - 1) / jc
Next
Print s
End Sub
vb 设s=1^1*2^2*3^3*.N^n编程求s不大于400000时最大的n
Dim s As Long, i As Integer
s = 0
i = 0
Do
i = i + 1
s = s + i * i
Loop While s 《 400000
Print "s="; s - i * i
Print "i="; i - 1
VB编程n!+(n+1)!+(n+2)!+(n+3)!+……+(n+m)!
创建一个窗体,并在其上创建三个文本框和一个命令按钮,其中:
文本框NN和文本框MM输入N和M的值,文本框JG输出结果。
命令按钮Command1用于开始计算,在其单击事件中输入如下代码:
Private Sub Command1_Click()
N = Val(Me.nn)
M = Val(Me.mm)
HH = 1
MNH = 0
For I = 1 To N + M
HH = HH * I
MNH = MNH + HH
Next I
Me.jg = MNH
End Sub
用vb程序编写
告诉你个诀窍,新建一个VB应用程序向导程序。可以从中获取不少VB给出的标准代码,略作修改就能满足自己编写程序的代码用。实现拿来就能用。
菜单部分:
代码部分:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Const EM_UNDO = &HC7
Private Declare Function OSWinHelp% Lib "user32" Alias "WinHelpA" (ByVal hwnd&, ByVal HelpFile$, ByVal wCommand%, dwData As Any)
Private Sub MDIForm_Load()
Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
LoadNewDoc
End Sub
Private Sub LoadNewDoc()
Static lDocumentCount As Long
Dim frmD As frmDocument
lDocumentCount = lDocumentCount + 1
Set frmD = New frmDocument
frmD.Caption = "Document " & lDocumentCount
frmD.Show
End Sub
Private Sub MDIForm_Unload(Cancel As Integer)
If Me.WindowState 《》 vbMinimized Then
SaveSetting App.Title, "Settings", "MainLeft", Me.Left
SaveSetting App.Title, "Settings", "MainTop", Me.Top
SaveSetting App.Title, "Settings", "MainWidth", Me.Width
SaveSetting App.Title, "Settings", "MainHeight", Me.Height
End If
End Sub
Private Sub tbToolBar_ButtonClick(ByVal Button As MSComCtlLib.Button)
On Error Resume Next
Select Case Button.Key
Case "新建"
LoadNewDoc
Case "打开"
mnuFileOpen_Click
Case "保存"
mnuFileSave_Click
Case "打印"
mnuFilePrint_Click
Case "剪切"
mnuEditCut_Click
Case "复制"
mnuEditCopy_Click
Case "粘贴"
mnuEditPaste_Click
Case "粗体"
ActiveForm.rtfText.SelBold = Not ActiveForm.rtfText.SelBold
Button.Value = IIf(ActiveForm.rtfText.SelBold, tbrPressed, tbrUnpressed)
Case "斜体"
ActiveForm.rtfText.SelItalic = Not ActiveForm.rtfText.SelItalic
Button.Value = IIf(ActiveForm.rtfText.SelItalic, tbrPressed, tbrUnpressed)
Case "下划线"
ActiveForm.rtfText.SelUnderline = Not ActiveForm.rtfText.SelUnderline
Button.Value = IIf(ActiveForm.rtfText.SelUnderline, tbrPressed, tbrUnpressed)
Case "左对齐"
ActiveForm.rtfText.SelAlignment = rtfLeft
Case "置中"
ActiveForm.rtfText.SelAlignment = rtfCenter
Case "右对齐"
ActiveForm.rtfText.SelAlignment = rtfRight
End Select
End Sub
Private Sub mnuHelpAbout_Click()
MsgBox "版本 " & App.Major & "." & App.Minor & "." & App.Revision
End Sub
Private Sub mnuHelpSearchForHelpOn_Click()
Dim nRet As Integer
’如果这个工程没有帮助文件,显示消息给用户
’可以在“工程属性”对话框中为应用程序设置帮助文件
If Len(App.HelpFile) = 0 Then
MsgBox "无法显示帮助目录,该工程没有相关联的帮助。", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 261, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuHelpContents_Click()
Dim nRet As Integer
’如果这个工程没有帮助文件,显示消息给用户
’可以在“工程属性”对话框中为应用程序设置帮助文件
If Len(App.HelpFile) = 0 Then
MsgBox "无法显示帮助目录,该工程没有相关联的帮助。", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 3, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuWindowArrangeIcons_Click()
Me.Arrange vbArrangeIcons
End Sub
Private Sub mnuWindowTileVertical_Click()
Me.Arrange vbTileVertical
End Sub
Private Sub mnuWindowTileHorizontal_Click()
Me.Arrange vbTileHorizontal
End Sub
Private Sub mnuWindowCascade_Click()
Me.Arrange vbCascade
End Sub
Private Sub mnuWindowNewWindow_Click()
LoadNewDoc
End Sub
Private Sub mnuViewWebBrowser_Click()
’应做:添加 ’mnuViewWebBrowser_Click’ 代码。
MsgBox "添加 ’mnuViewWebBrowser_Click’ 代码。"
End Sub
Private Sub mnuViewOptions_Click()
’应做:添加 ’mnuViewOptions_Click’ 代码。
MsgBox "添加 ’mnuViewOptions_Click’ 代码。"
End Sub
Private Sub mnuViewRefresh_Click()
’应做:添加 ’mnuViewRefresh_Click’ 代码。
MsgBox "添加 ’mnuViewRefresh_Click’ 代码。"
End Sub
Private Sub mnuViewStatusBar_Click()
mnuViewStatusBar.Checked = Not mnuViewStatusBar.Checked
sbStatusBar.Visible = mnuViewStatusBar.Checked
End Sub
Private Sub mnuViewToolbar_Click()
mnuViewToolbar.Checked = Not mnuViewToolbar.Checked
tbToolBar.Visible = mnuViewToolbar.Checked
End Sub
Private Sub mnuEditPasteSpecial_Click()
’应做:添加 ’mnuEditPasteSpecial_Click’ 代码。
MsgBox "添加 ’mnuEditPasteSpecial_Click’ 代码。"
End Sub
Private Sub mnuEditPaste_Click()
On Error Resume Next
ActiveForm.rtfText.SelRTF = Clipboard.GetText
End Sub
Private Sub mnuEditCopy_Click()
On Error Resume Next
Clipboard.SetText ActiveForm.rtfText.SelRTF
End Sub
Private Sub mnuEditCut_Click()
On Error Resume Next
Clipboard.SetText ActiveForm.rtfText.SelRTF
ActiveForm.rtfText.SelText = vbNullString
End Sub
Private Sub mnuEditUndo_Click()
’应做:添加 ’mnuEditUndo_Click’ 代码。
MsgBox "添加 ’mnuEditUndo_Click’ 代码。"
End Sub
Private Sub mnuFileExit_Click()
’卸载窗体
Unload Me
End Sub
Private Sub mnuFileSend_Click()
’应做:添加 ’mnuFileSend_Click’ 代码。
MsgBox "添加 ’mnuFileSend_Click’ 代码。"
End Sub
Private Sub mnuFilePrint_Click()
On Error Resume Next
If ActiveForm Is Nothing Then Exit Sub
With dlgCommonDialog
.DialogTitle = "Print"
.CancelError = True
.Flags = cdlPDReturnDC + cdlPDNoPageNums
If ActiveForm.rtfText.SelLength = 0 Then
.Flags = .Flags + cdlPDAllPages
Else
.Flags = .Flags + cdlPDSelection
End If
.ShowPrinter
If Err 《》 MSComDlg.cdlCancel Then
ActiveForm.rtfText.SelPrint .hDC
End If
End With
End Sub
Private Sub mnuFilePrintPreview_Click()
’应做:添加 ’mnuFilePrintPreview_Click’ 代码。
MsgBox "添加 ’mnuFilePrintPreview_Click’ 代码。"
End Sub
Private Sub mnuFilePageSetup_Click()
On Error Resume Next
With dlgCommonDialog
.DialogTitle = "页面设置"
.CancelError = True
.ShowPrinter
End With
End Sub
Private Sub mnuFileProperties_Click()
’应做:添加 ’mnuFileProperties_Click’ 代码。
MsgBox "添加 ’mnuFileProperties_Click’ 代码。"
End Sub
Private Sub mnuFileSaveAll_Click()
’应做:添加 ’mnuFileSaveAll_Click’ 代码。
MsgBox "添加 ’mnuFileSaveAll_Click’ 代码。"
End Sub
Private Sub mnuFileSaveAs_Click()
Dim sFile As String
If ActiveForm Is Nothing Then Exit Sub
With dlgCommonDialog
.DialogTitle = "另存为"
.CancelError = False
’ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.*"
.ShowSave
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
ActiveForm.Caption = sFile
ActiveForm.rtfText.SaveFile sFile
End Sub
Private Sub mnuFileSave_Click()
Dim sFile As String
If Left$(ActiveForm.Caption, 8) = "Document" Then
With dlgCommonDialog
.DialogTitle = "保存"
.CancelError = False
’ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.*"
.ShowSave
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
ActiveForm.rtfText.SaveFile sFile
Else
sFile = ActiveForm.Caption
ActiveForm.rtfText.SaveFile sFile
End If
End Sub
Private Sub mnuFileClose_Click()
’应做:添加 ’mnuFileClose_Click’ 代码。
MsgBox "添加 ’mnuFileClose_Click’ 代码。"
End Sub
Private Sub mnuFileOpen_Click()
Dim sFile As String
If ActiveForm Is Nothing Then LoadNewDoc
With dlgCommonDialog
.DialogTitle = "打开"
.CancelError = False
’ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.*"
.ShowOpen
If Len(.FileName) = 0 Then
Exit Sub
End If
sFile = .FileName
End With
ActiveForm.rtfText.LoadFile sFile
ActiveForm.Caption = sFile
End Sub
Private Sub mnuFileNew_Click()
LoadNewDoc
End Sub

更多文章:
html网页模板(网页制作设计模板-旅游网页该如何设计模板)
2025年9月29日 04:45
this love歌词taylor(求Taylor Swift的Love Story的歌词的中文翻译~)
2025年6月19日 16:30
span标签有内容却不显示(定义一个CSS,但是只有DIV可以显示出来,span等都无法显示)
2026年7月29日 21:00
void sort是什么意思(请各位说说void sort里的算法是什么意思)
2025年6月20日 00:15
学电脑软件开发哪个学校好(电脑软件开发去什么学校学习比较好)
2026年8月16日 18:45
subject都有什么意思(“subject ”是什么意思)
2026年8月15日 06:00
莫奈的网络解释莫奈的网络解释是什么?莫奈的解释莫奈的解释是什么
2025年7月29日 02:00
网页模板停用不能上报怎么办(请教一下,关于网页模板显示不出来的问题~~)
2025年10月22日 22:45
dateadd函数的语法参数(在VB6.0中,DateAdd函数中,用“w“,“y“与“d“,我怎么感觉都一样呀,都是天)
2026年2月21日 15:30
springfestival用in oron(the Spring Festival前用in还是on)
2026年8月28日 02:45
acquaintance歌曲(新年快乐英文歌除了happy new year还有那些)
2026年4月19日 03:15
constantly continuously(constantly怎么读)
2025年8月8日 18:15











