比如我先要查询数据库,然后根据查询出来的记录的个数来确定要添加image的个数,在代码里面怎么样实现动态添加阿?我想定义一个变量,dim img as new image 但是new 的时候没有image可选阿?

解决方案 »

  1.   

    给你个动态添加command的例子,你改改
    Option ExplicitDim WithEvents cmdSayHello As CommandButton
    Dim WithEvents cmdClose As CommandButtonPrivate Sub cmdClose_Click()
        Unload Me
    End SubPrivate Sub cmdSayHello_Click()
        MsgBox "Hello world!", vbInformation, Me.Caption
    End SubPrivate Sub Form_Load()
        
        Set cmdSayHello = Me.Controls.Add("VB.CommandButton", "cmdSayHello")
        With cmdSayHello
            .Left = 4000
            .Top = 250
            .Width = 1200
            .Height = 350
            .Caption = "&Say Hello"
            .Visible = True
        End With
        
        Set cmdClose = Me.Controls.Add("VB.CommandButton", "cmdClose")
        
        With cmdClose
            .Left = 4000
            .Top = 700
            .Width = 1200
            .Height = 350
            .Caption = "&Close"
            .Visible = True
        End With
    End Sub
      

  2.   

    Dim WithEvents imgShow As ImagePrivate Sub Form_Load()
        Set cmdSayHello = Me.Controls.Add("VB.Image", "imgShow")
        With cmdSayHello
            .Left = 1000
            .Top = 250
            .Width = 3600
            .Height = 3500
            .Visible = True
            .BorderStyle = 1
            '.BorderStyle = 0
        End With
    End Sub
      

  3.   

    '引用ADO(Microsoft ActiveX Data Objects 2.X Library)
    Dim myImg(1000) As Image'预定义1000个Image,如果你的记录数太多,那么你可以定义得更大一点
    Private Sub Command1_Click()
        On Error GoTo err
        Dim cn As New ADODB.Connection, rs
        '连接SQL:
        cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=登陆用户名;Password=登录密码;Initial Catalog=数据库名;Data Source=yourSERVICE"
        '连接Access:
        'cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Test.mdb;Jet OLEDB:DataBase password=12345;""
        cn.Open
        rs.CursorLocation=adUseClient'设置客户端游标
        rs.Open "select * from 表1", cn, 3, 2
        dim i%
        for i=0 to rs.RecordCount-1
            Set myImg(i) = Form1.Controls.Add("vb.Image", "img" & i & "")
            myImg.Width=20:myImg.Height=20
            myImg(i).Left = 100 + 100 * i
            myImg(i).Top = 100 + 500 * i
            myImg(i).Visible = True  
            'myImg.BorderStyle=1          
        next
        Exit Sub
    err:
        MsgBox err.Description
    End Sub
      

  4.   

    基本上三个步骤
    1.在form里面用image控件做个数组,也就是说在form里面的image控件为image(0)
    2.根据数据库里面image得数目拓展这个数组 load image(i)
    3.然后确定新image的大小位置没有具体代码,但这就是基本思路