回 帖 发 新 帖 刷新版面

主题:[原创]vb 编程的一点心得和体会 ,希望会给您带来一点帮助

[em1]
(1)
   消息框中按钮之定义

    MsgBox strMsg1, c1+c2+c3 , strMsg2

    其中  strMsg1   为提示信息

          strMsg2   为标题内容

          c1+c2+c3  定义按钮形式,具体如下:

          c1: 按钮的类型

            0    vbOkOnly             只有一个按钮“确定”
            1    vbOkCancel           两个按钮“确定”和“取消”
            2    vbAbortRetryIgnore   三个按钮“终止”、“重试”和“忽略”
            3    vbYesNoCancel        三个按钮“是”、“否”和“取消”
            4    vbYesNo              两个按钮“是”和“否”
            5    vbRetryCancel        两个按钮“重试”和“取消”

              返回值:  vbOk          1   确定
                        vbCancel      2   取消
                        vbAbort       3   终止
                        vbRetry       4   重试
                        vbIgnore      5   忽略
                        vbYes         6   是
                        vbNo          7   否

          c2: 图标的类型         

            16   vbCritical           ×
            32   vbQuesion            ?
            48   vbExclamation        !
            64   vbInformation        i

        c3: 默认焦点

            0    vbDefalaultButton1   左起第一个按钮自动获得焦点
            256  vbDefalaultButton2   左起第二个按钮自动获得焦点
            512  vbDefalaultButton3   左起第三个按钮自动获得焦点

        和为:   00  0000  0000 B
               c3   c2    c1

   例: 1.   i = MsgBox " 是否要删除该条记录 ? ", 1+32+0 , " 请确认"

        2.   MsgBox " 是否要删除 ! ", 0+32+0 , " 请...."


(2)
        判断表的存在

Function M_fucScanTable(strTName As String) As Integer ' 搜索表 strTableName
     On Error GoTo OpenErr
     Set MyRsm = New Recordset
   MyRsm.Open "Select * From " & strTName,Cn, adOpenKeyset, adLockOptimistic
        MyRsm.MoveLast
        M_fucScanTable = MyRsm.RecordCount        ' 返回记录数,0 为空表
        MyRsm.Close
        Set MyRsm = Nothin
        Exit Function
    OpenErr:
        M_fucScanTable = -1                                   ' 无表
    End Function   
(3)
    动态建立表
       strSQL = "CREATE TABLE " & strTName & _
        "( Xh char(3) Not Null Primary key,Mc char(10),Xb char(2)," & _
        "Csrq char(10),Zw char(20),Gz numeric(9,2),Bz char(30),Xp image )"
       cn.Execute strSQL, , adCmdText

       其中: Primary key 为设置主键(唯一)
(4)
    插入记录     Insert
  
       strSQL = "Insert Into A01(Xh,Mc,Xb,Csrq,Zw) " & _
       "Values ( '" & Xhp & "','" & Mcp & "','" & Xbp &"','" &Rqp& "','" & Zwp & "' "
       Cn.Execute strSQL
(5)
导出表格到excel
    Dim newxls As Excel.Application
    Dim newbook As Excel.Workbook
    Dim newsheet As Excel.Worksheet

    Set newxls = CreateObject("excel.application")
    newxls.Visible = True
    Set newbook = newxls.Workbooks.Add
    Set newsheet = newbook.Worksheets(1)
    For i = 0 To 7
        For j = 0 To 4
            MSFlexGrid1.Row = i
            MSFlexGrid1.col = j
            newsheet.Cells(1, 3) = Trim(Combo1.Text) & "班"
            newsheet.Cells(1, 4) = "第" & bytXq & "学期"
            newsheet.Cells(1, 5) = "课程表"
            newsheet.Cells(i + 3, j + 2) = Trim(MSFlexGrid1.TextMatrix(i, j))
        Next j
    Next i
    注意此项操作你先要 引用 excelctl type library 和 microsoft excel 9.0 object library

(6)   with 语句的应用
   With msflexgrid1
        .CellAlignment = 4
        .TextMatrix(1, 0) = "考试编号"
        .TextMatrix(1, 1) = "学号"
        .TextMatrix(1, 2) = "姓名"
        .TextMatrix(1, 3) = "班号"
        .TextMatrix(1, 4) = "课程名称"
        .TextMatrix(1, 5) = "分数"        
    End With
(7)
响应回车
Private Sub Text2_KeyPress(KeyAscii As Integer)                       ' Text2 响应回车键
    If KeyAscii = 13 Then
       Command1.SetFocus
    End If
End Sub
(8)
隐藏任务栏
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
            ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_Load()
        SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3
End Sub
希望能给您在编程时带来一点方便
同时我也只是起到一个抛砖引玉的 目的
希望您也能供稿 我方便大家努力
谢谢各位 vb 爱好者们
[em80]

回复列表 (共29个回复)

11 楼

好贴一定要顶的

12 楼

谢谢楼上的兄弟

13 楼

我编写的 彩票投注模拟终端
代码如下:
Dim intt()
Dim inta, intb, i, j, k As Integer
Dim stra As String

Private Sub Command1_Click()
Printer.FontSize = 30
Printer.Print "   " & "你的辽宁风采电脑福利彩票" & Combo1.Text & "号码"
Printer.Print "  "
Printer.FontSize = 20
Printer.Print "          " & "投注时间为:" & Label1(0).Caption
Printer.Print "  "
Printer.Print "          " & "您共投" & Text1.Text & "注 "
Printer.Print "  "
Printer.FontSize = 15
For i = 0 To Val(Trim(Text1)) - 1
  stra = "     "
For j = 0 To intb - 1
stra = stra & MSFlexGrid1.TextMatrix(i, j) & "    "
Next j
Printer.Print "           " & stra
Next i
Printer.EndDoc

End Sub

Private Sub Command2_Click()

If Val(Text1.Text) < 1 Then MsgBox "请输入数字", 16, "提示"
MSFlexGrid1.Visible = True
MSFlexGrid1.Rows = Val(Trim(Text1))
Call reboot
Call writ
If Val(Trim(Text1)) >= 7 Then
   MSFlexGrid1.Width = 4200
Else
   MSFlexGrid1.Width = 4020
End If
End Sub

Private Sub Command3_Click()
MSFlexGrid1.Clear
MSFlexGrid1.Visible = False
End Sub

Private Sub Form_Load()
Label1(0).Caption = Date & "  " & Time
Call addtext
Call began
End Sub
Private Sub Combo1_Click()
inta = Val(Left(Trim(Combo1.Text), 2))
intb = Val(Right(Trim(Combo1.Text), 1))
Text1.Enabled = True
Text1.SetFocus
End Sub

Private Sub addtext()
Combo1.Clear
Combo1.AddItem "35选7"
Combo1.AddItem "25选4"
Combo1.AddItem "29选7"
End Sub

Private Sub Text1_Change()
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = True
End Sub

Private Sub Text1_LostFocus()
If Len(Trim(Text1)) <= 0 Then MsgBox "请输入投注数量", 48, "系统提示"
End Sub

Private Sub Timer1_Timer()
Label1(0).Caption = Date & "  " & Time
End Sub
Private Sub ml()
Label3.Left = Label3.Left - 20
If Label3.Left <= -3490 Then Label3.Left = 7560

End Sub

Private Sub Timer2_Timer()
Call ml
End Sub

Private Sub began()
With MSFlexGrid1
        .Cols = 7
        .Row = 0:  .ColWidth(0) = 570
                   .ColWidth(1) = 570
                   .ColWidth(2) = 570
                   .ColWidth(3) = 570
                   .ColWidth(4) = 570
                   .ColWidth(5) = 570
                   .ColWidth(6) = 570
                   
    End With
End Sub

Private Sub reboot()
ReDim intt(Val(Trim(Text1)), intb) '重定义数组的 行和列
For i = 1 To Val(Trim(Text1))
Randomize ' 初始化随机数生成器
For j = 1 To intb
intt(i, j) = Int(inta * Rnd) + 1
For k = 1 To j - 1
If intt(i, k) = intt(i, j) Then intt(i, j) = Int(inta * Rnd) + 1
Next k
Next j
Next i
End Sub
Private Sub writ()
        With MSFlexGrid1
        .Rows = Val(Text1)
           For i = 0 To Val(Text1) - 1
           For j = 0 To intb - 1
             .TextMatrix(i, j) = intt(i + 1, j + 1) & " "
             If Len(Trim(.TextMatrix(i, j))) < 2 Then .TextMatrix(i, j) = "0" & .TextMatrix(i, j)
             Next j
             Next i
        End With
End Sub

14 楼

我也来发一个

'本程序无需任何控件和引用,直接即可运行。鼠标左键为旋转,中键为平移
'
'本程序用来理解三维空间编程思路
'1、构建一个三维坐标系
'2、对三维坐标系进行旋转和移动计算
'3、将三维坐标系的坐标根据视觉角度来计算平面位置
'   (由于本人没上过几天学,没学过几何,更别说坐标系。因此,这个计算公式始终没有做出来)
'
'备注:注解中带有“*”的为视觉角度计算公式,虽然可以模拟出三维环境,但是公式算法不正确
'
'



Option Explicit

Dim ZxX As Long '中心X
Dim ZxY As Long '中心Y
Dim ZxZ As Long '中心Z
Dim DownX(2) As Long '鼠标按下的坐标位置
Dim DownY(2) As Long '鼠标按下的坐标位置
Dim DownZ(2) As Long '鼠标按下的坐标位置
Dim Xs(2) As Long '向心距离
Dim Ys(2) As Long '向心距离
Dim Zs(2) As Long '向心距离
Dim X1 As Variant 'Lines的X1坐标
Dim X2 As Variant 'Lines的X2坐标
Dim Y1 As Variant 'Lines的Y1坐标
Dim Y2 As Variant 'Lines的Y2坐标
Dim Z1 As Variant 'Lines的Z1坐标
Dim Z2 As Variant 'Lines的Z2坐标

Dim Jj As Double '*焦距
Dim Bj As Double '*变焦
Dim Jd As Double '角度
Dim Pi As Double '派值
Dim Hx As Double 'X弧度
Dim Hy As Double 'Y弧度
Dim Mlmd As Integer '鼠标灵敏度
Dim RGBs As Long '颜色
Dim FH As Integer '窗口高度差
Dim FW As Integer '窗口宽度差

Private Sub Form_Load()
'参数初始化
    ZxX = 3000
    ZxY = 3000
    ZxZ = 3000
    Pi = 3.1415926
    Jj = 1000   '*
    Bj = 0.6    '*
    Mlmd = 30
    
'绘制坐标系
'正方体由12条线组成,另外增加3条坐标轴线
'以下是15条线的坐标,其中X1\Y1\Z1为线的起始点,X2\Y2\Z2为线的终止点
    X1 = Split("0,1680,1680,1680,4320,1680,4320,1680,4320,1680,1680,1680,4320,3000,3000,3000", ",")
    X2 = Split("0,4320,1680,4320,4320,1680,4320,1680,4320,4320,1680,4320,4320,4200,3000,3000", ",")
    Y1 = Split("0,1680,1680,4320,1680,1680,1680,4320,4320,1680,1680,4320,1680,3000,3000,3000", ",")
    Y2 = Split("0,1680,4320,4320,4320,1680,1680,4320,4320,1680,4320,4320,4320,3000,1800,3000", ",")
    Z1 = Split("0,1680,1680,1680,1680,1680,1680,1680,1680,4320,4320,4320,4320,3000,3000,3000", ",")
    Z2 = Split("0,1680,1680,1680,1680,4320,4320,4320,4320,4320,4320,4320,4320,3000,3000,4200", ",")
    
'屏幕初始化
    FH = Me.Height - Me.ScaleHeight
    FW = Me.Width - Me.ScaleWidth
    Me.Height = ZxY * 2 + FH
    Me.Width = ZxX * 2 + FW
    Me.AutoRedraw = True
    FillStyle = 0
    Circle (ZxX, ZxY), 30
    FillStyle = 1
    Circle (ZxX, ZxY), 2500, RGB(120, 120, 120)
    Dim i As Integer
    For i = 1 To UBound(X1)
        Xs(1) = X1(i) - ZxX
        Ys(1) = Y1(i) - ZxY
        Zs(1) = Z1(i) - ZxZ
        Xs(2) = X2(i) - ZxX
        Ys(2) = Y2(i) - ZxY
        Zs(2) = Z2(i) - ZxZ
        RGBs = RGB(0, 0, 0)
        If i = 13 Then RGBs = RGB(160, 0, 0)
        If i = 14 Then RGBs = RGB(0, 160, 0)
        If i = 15 Then RGBs = RGB(0, 0, 160)
        Line (Xs(1) * (Jj / Z1(i) + Bj) + ZxX, Ys(1) * (Jj / Z1(i) + Bj) + ZxY)-(Xs(2) * (Jj / Z2(i) + Bj) + ZxX, Ys(2) * (Jj / Z2(i) + Bj) + ZxY), RGBs
        '*
    Next
    Me.ForeColor = RGB(160, 0, 0)
    CurrentY = Y2(13)
    CurrentX = X2(13)
    Print "X"
    Me.ForeColor = RGB(0, 160, 0)
    CurrentY = Y2(14)
    CurrentX = X2(14)
    Print "Y"
    Me.ForeColor = RGB(0, 0, 160)
    CurrentY = Y2(15)
    CurrentX = X2(15)
    Print "Z"
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DownX(0) = X
DownY(0) = Y
DownX(1) = X
DownY(1) = Y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Integer
If Button = 1 Then  '旋转计算
    Me.Cls
    Hx = (X - DownX(0)) / Mlmd * Pi / 180
    Hy = (Y - DownY(0)) / Mlmd * Pi / 180
    For i = 1 To UBound(X1)
        Xs(1) = X1(i) - ZxX
        Ys(1) = Y1(i) - ZxY
        Zs(1) = Z1(i) - ZxZ
        Xs(2) = X2(i) - ZxX
        Ys(2) = Y2(i) - ZxY
        Zs(2) = Z2(i) - ZxZ
        X1(i) = Xs(1) * Cos(-Hx) + Zs(1) * Sin(-Hx) + ZxX
        Z1(i) = Zs(1) * Cos(-Hx) - Xs(1) * Sin(-Hx) + ZxZ
        Zs(1) = Z1(i) - ZxZ
        Y1(i) = Ys(1) * Cos(-Hy) + Zs(1) * Sin(-Hy) + ZxY
        Z1(i) = Zs(1) * Cos(-Hy) - Ys(1) * Sin(-Hy) + ZxZ
        X2(i) = Xs(2) * Cos(-Hx) + Zs(2) * Sin(-Hx) + ZxX
        Z2(i) = Zs(2) * Cos(-Hx) - Xs(2) * Sin(-Hx) + ZxZ
        Zs(2) = Z2(i) - ZxZ
        Y2(i) = Ys(2) * Cos(-Hy) + Zs(2) * Sin(-Hy) + ZxY
        Z2(i) = Zs(2) * Cos(-Hy) - Ys(2) * Sin(-Hy) + ZxZ
        RGBs = RGB(0, 0, 0)
        If i = 13 Then RGBs = RGB(160, 0, 0)
        If i = 14 Then RGBs = RGB(0, 160, 0)
        If i = 15 Then RGBs = RGB(0, 0, 160)
        Line ((X1(i) - ZxX) * (Jj / Z1(i) + Bj) + ZxX, (Y1(i) - ZxY) * (Jj / Z1(i) + Bj) + ZxY)-((X2(i) - ZxX) * (Jj / Z2(i) + Bj) + ZxX, (Y2(i) - ZxY) * (Jj / Z2(i) + Bj) + ZxY), RGBs
    Next
    DownX(0) = X
    DownY(0) = Y
    Me.ForeColor = RGB(160, 0, 0)
    CurrentY = 0
    CurrentX = 0
    Print "X:" & (X - DownX(1)) / Mlmd
    CurrentY = Y2(13)
    CurrentX = X2(13)
    Print "X"
    Me.ForeColor = RGB(0, 160, 0)
    CurrentY = 180
    CurrentX = 0
    Print "Y:" & (Y - DownY(1)) / Mlmd
    CurrentY = Y2(14)
    CurrentX = X2(14)
    Print "Y"
    CurrentY = 360
    CurrentX = 0
    Me.ForeColor = RGB(0, 0, 160)
    Print "Z:0"
    CurrentY = Y2(15)
    CurrentX = X2(15)
    Print "Z"
ElseIf Button = 4 Then  '平移计算
    Me.Cls
    For i = 1 To UBound(X1)
        X1(i) = X1(i) + X - DownX(0)
        Y1(i) = Y1(i) + Y - DownY(0)
        X2(i) = X2(i) + X - DownX(0)
        Y2(i) = Y2(i) + Y - DownY(0)
        RGBs = RGB(0, 0, 0)
        If i = 13 Then RGBs = RGB(160, 0, 0)
        If i = 14 Then RGBs = RGB(0, 160, 0)
        If i = 15 Then RGBs = RGB(0, 0, 160)
        Line ((X1(i) - ZxX) * (Jj / Z1(i) + Bj) + ZxX, (Y1(i) - ZxY) * (Jj / Z1(i) + Bj) + ZxY)-((X2(i) - ZxX) * (Jj / Z2(i) + Bj) + ZxX, (Y2(i) - ZxY) * (Jj / Z2(i) + Bj) + ZxY), RGBs
        '*
    Next
    DownX(0) = X
    DownY(0) = Y
    Me.ForeColor = RGB(160, 0, 0)
    CurrentY = Y2(13)
    CurrentX = X2(13)
    Print "X"
    Me.ForeColor = RGB(0, 160, 0)
    CurrentY = Y2(14)
    CurrentX = X2(14)
    Print "Y"
    Me.ForeColor = RGB(0, 0, 160)
    CurrentY = Y2(15)
    CurrentX = X2(15)
    Print "Z"
End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    FillStyle = 0
    Circle (ZxX, ZxY), 30
    FillStyle = 1
    Circle (ZxX, ZxY), 2500, RGB(120, 120, 120)
End Sub

15 楼

好帖
我珍藏了 加分

16 楼

受益匪浅  多谢多谢

17 楼

以上楼主,不知你们会不会觉的,这样的信息发布至这个网页上,会不会觉的我们的常识太浅了, 这可是编程爱好者的网页啊~~~~~~~~,不是电脑爱好者的网页~~~深思啊~~

18 楼

我们实在 用 vb进行交流啊
我们喜欢编程 所以才?????

19 楼

20 楼

up

我来回复

您尚未登录,请登录后再回复。点此登录或注册