比如我希望在窗口中动态的的按照我的意愿添加新的控件,如按钮。。等,用代码怎么写?以前记得,现在忘了,呵呵

解决方案 »

  1.   

    Option ExplicitDim WithEvents cmdMyCommand As VB.CommandButtonDim WithEvents ctlCommand As VB.CommandButtonDim WithEvents ctlCommandDel As VB.CommandButtonDim WithEvents ctlText As VB.TextBoxDim WithEvents ctlDynamic As VBControlExtender   '--動態的控件
    Private Sub ctlCommandDel_Click()
     
      On Error Resume Next
      
      Dim i As Integer
      
      '--將控件許可証消息刪除
       Licenses.Remove "MSComctlLib.TreeCtrl"
        
       If MsgBox("是否刪除所有的控件", vbYesNo) = vbYes Then
          
          For i = 1 To Form1.Controls.Count
             
             Controls.Remove 0
          
          Next i
       End If
       
    End SubPrivate Sub ctlCommand_Click()
      
      ctlText.Text = "你點的是控制項按鈕鍵"End Sub
    Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
      
      '--點擊樹型控件的一個條目,在cltText中間顯示該條目   If Info.Name = "Click" Then
       
          ctlText.Text = "您的是 & ctlDynamic.object.selecteditem.Text"
       
       End If
       
    End Sub
                            
    Private Sub Form_Load()  Dim i As Integer
      
      '--將樹型控件的許可証信息加入許可集合中
      
      '--如果許可已經存在的話,則返回錯誤信息732
      
      Licenses.Add "MSComctlLib.TreeCtrl"
      
      ' 在Form中動態的加入一個樹形控制項,如果你想樹形控制項建立到不同的  '容器中,象一個Frame控制項或者PictureBox控制項,你只要將Controls.Add
      
      '函數的第三個參數改為特定的容器名就可以了  Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", "myCtl", Form1)
      
      '--設置樹型控件的尺寸,位置
      
      ctlDynamic.Move 1, 1, 2500, 3500
      
      '--在樹型控件中加入10個節點
      
      For i = 1 To 10
       
        ' ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), Text:="Test" & Str(i)    ' ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), Relationship:=4, Text:="TestChild" & Str(i)  Next i
      
     '--使樹型控件可見
     
      ctlDynamic.Visible = True
      
     '--加入一個Text
     
     Set ctlText = Controls.Add("VB.TextBox", "myText", Form1)
     
     '--設置Text的尺寸和位置
     
     ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), 1, 2500, 100
     
     '--將Text背景設置為藍色,前景設置為白色
     
     ctlText.BackColor = vbBlue
     
     ctlText.ForeColor = vbWhite
     
     '--使Text可見
     
     ctlText.Visible = True
     
     '--加入一個CommandButton
     
     Set ctlCommand = Controls.Add("VB.CommandButton", "ctlCommand1", Form1)
     
     '--設置CommandBtton的位置和大小
     
      ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), ctlText.Height + 50, 1500, 500
     
     '--設置CommandBtton的標題
      
      ctlCommand.Caption = "點擊"
      
     '--使CommandButton可見
      
      ctlCommand.Visible = True
      
      '--建立一個刪除的按鈕
      
      Set ctlCommandDel = Controls.Add("VB.CommandButton", "ctlCommand2", Form1)
      
      '--設置Button的位置和大小
      
      ctlCommandDel.Move (ctlDynamic.Left + ctlDynamic.Width + 50), ctlText.Height + 650, 1500, 500
     
      ctlCommandDel.Caption = "刪除所有控件"
     
      ctlCommandDel.Visible = True
      
    End Sub
      

  2.   

    Private Sub Form_Load()
    Dim cmdbtn As CommandButton
    Set cmdbtn = Controls.Add("vb.commandbutton", "cmdbtn", Me)
    cmdbtn.Visible = True
    cmdbtn.Move 100, 300
    End Sub
      

  3.   

    Option ExplicitPrivate WithEvents NewButton As CommandButton
    Private Sub Command1_Click()
        If NewButton Is Nothing Then
            Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Frame1)
            'NewButton.Move Command1.Left Command1.Width + 240,Command1.Top
            NewButton.Move 20, 20
            NewButton.Caption = "New Button"
            NewButton.Visible = True
        End If
    End Sub
    Private Sub Command2_Click()
    Form1.Controls.Remove NewButton
    End SubPrivate Sub NewButton_Click()
        MsgBox "New button clicked"
    End Sub