ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] °ÊºA·s¼W UserForm ¤Î CommandButton «á ¦p¦ó¼gclickªº°Ê§@¡H

[µo°Ý] °ÊºA·s¼W UserForm ¤Î CommandButton «á ¦p¦ó¼gclickªº°Ê§@¡H

§Ú¥H¤U¦Cµ{¦¡½X ·s¼W¤@­Ó UserForm ¡BCommandButton ¦A¥Î¤@­Ó Class ¨Ó°õ¦æ Button Click ªº°Ê§@¡A¦ý ¦ü¥G ¤@ª½µLªk ·ÓµÛ Class ¤ºªº°Ê§@¥h°õ¦æ ~~
§Ú¤£ª¾¬O­þ­Ó³¡¥÷¥X¤F°ÝÃD ~~

   
   Dim MyNewForm As Object
    Dim NewListBox1 As MSForms.ListBox
    Dim NewLabel1 As MSForms.Label
    Dim NewCommandButton1 As MSForms.CommandButton
   
   
    Dim Btn1 As New Class1
    Dim MyAddItemCode$
    Dim UF_Name$
   
'   Create the UserForm
    Set MyNewForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
   
    With MyNewForm
         .Properties("Caption") = "Test_Form1"
         .Properties("Width") = 540
         .Properties("Height") = 350
         '.Name = "Test_Form1"
    End With


      Set NewCommandButton1 = MyNewForm.Designer.Controls _
      .Add("forms.CommandButton.1")
      
    With NewCommandButton1
   
        .Name = "Add_ItemBtn"
        .Left = 235
        .Top = 36
        .Height = 22
        .Width = 50
        .Caption = "·s¼W"
        .Font.Name = "¼Ð·¢Åé"
        .Font.Size = 12
        
    End With
        
  
    Set Btn1.CmdBtn = NewCommandButton1

      '   Show the form
    VBA.UserForms.Add(UF_Name$).Show 'vbModeless

    'Delete the form
    ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=MyNewForm

    'ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=Test1
    Set MyNewForm = Nothing


Class ªº¤º®e


Option Explicit

Public WithEvents CmdBtn As MSForms.CommandButton
Private Sub CmdBtn_Click()
                MsgBox "Test O.K ! "
End Sub

¦^´_ 1# C.F


    §A­nªº¥Øªº¬O¤°»ò?
     ªÅ¥Õªí³æ ²£¥X CommandButton?
     µM«á ÂICommandButton ­n¦³¤°»ò°Ê§@?

  ¦h°µ¦h·Q¦h¾Ç²ß¡A¤Ö¬Ý¤Ö¿ù¤Ö°g³~

  ¦h°µ=¦h¦h½m²ß¡A¦h¦h½s¼g¡C
  ¦h·Q=·Q·Q¬°¤°»ò¤H®aµ{¦¡­n¨º¼Ë¼g¡A¦pªG´«¦¨¦Û¤v¡A¤S·|«ç¼g¡C
  ¦h¾Ç²ß=¾Ç²ß¤H®aªºµo°Ý¨Ã¸Ñµª¡A¾Ç²ß¤H®aªº¼gªk

  ¤Ö¬Ý=¥u¬Ý¤£°µ¤]ªPµM

TOP

¨ä¹ê§ÚÁÙ¦³¨â­Ó ListBox¡A§Ú¥D­n¬OÂI¤FButton «á ­n±N¥ªÃä ListBox ªº¤å¦r ±a¨ì¥kÃä¥h ~~
¤W¤è¬O§Ú¥ý´ú¸Õ¤@¤U¡Aµ²ªG´N¤£¦¨¥\¤F ~~

TOP

¦^´_ 3# C.F


ÂI¤U¥h ¥ªÃä¨ì¥kÃä ¬O½Æ»sÁÙ¬O³æ¯Â·h²¾?

  ¦h°µ¦h·Q¦h¾Ç²ß¡A¤Ö¬Ý¤Ö¿ù¤Ö°g³~

  ¦h°µ=¦h¦h½m²ß¡A¦h¦h½s¼g¡C
  ¦h·Q=·Q·Q¬°¤°»ò¤H®aµ{¦¡­n¨º¼Ë¼g¡A¦pªG´«¦¨¦Û¤v¡A¤S·|«ç¼g¡C
  ¦h¾Ç²ß=¾Ç²ß¤H®aªºµo°Ý¨Ã¸Ñµª¡A¾Ç²ß¤H®aªº¼gªk

  ¤Ö¬Ý=¥u¬Ý¤£°µ¤]ªPµM

TOP

¬O­n·h²¾ªº

TOP

  1. Sub Auto_Open()
  2. Dim MyForm As VBComponent
  3. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
  4. With MyForm
  5.     .Properties("Caption") = "Test_Form"
  6.     .Name = "Test_Form1"
  7.     With .Designer
  8.       With .Controls.Add("Forms.CommandButton.1")
  9.           .Top = 50
  10.           .Left = 100
  11.           .Height = 20
  12.           .Width = 20
  13.           .Caption = ">>"
  14.           .Name = "Move_Data"
  15.       End With
  16.       For i = 1 To 2
  17.       With .Controls.Add("Forms.Listbox.1")
  18.           .Top = 10
  19.           .Left = (i - 1) * 100 + 20
  20.           .Height = 120
  21.           .Width = 80
  22.           .Name = "MyList" & i
  23.       End With
  24.       Next
  25.     End With
  26.     With .CodeModule
  27.       .InsertLines 1, "Private Sub UserForm_Initialize()"
  28.       .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
  29.       .InsertLines 3, "End Sub"
  30.       .InsertLines 4, "Private Sub Move_Data_Click()"
  31.       .InsertLines 5, "x = MyList1.ListIndex"
  32.       .InsertLines 6, "MyList2.AddItem  MyList1.List(x)"
  33.       .InsertLines 7, "MyList1.RemoveItem x"
  34.       .InsertLines 8, "End Sub"
  35.     End With
  36. End With
  37. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
  38.   .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
  39. "With ThisWorkbook" & Chr(10) & _
  40. ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
  41. "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
  42. ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
  43. ".Save" & Chr(10) & _
  44. "End With" & Chr(10) & _
  45. "End Sub"
  46. End With
  47. 'OpenForm '¶}±ÒÀɮצ۰ʶ}±Òªí³æ
  48. End Sub
  49. Sub OpenForm()
  50. 'Test_Form1.Show 0 '¶}±Òªí³æ
  51. End Sub
½Æ»s¥N½X
¦^´_ 5# C.F


    ¬O³o¼Ëªº®ÄªG¶Ü?
play.gif
°ÊºA·s¼Wªí³æ.zip (13.91 KB)
¤@¯ë¼Ò²Õ
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

·PÁ±z ~~ ³oªº½T¬O§Ú­nªº~~~
¥u¬O¦]¬°§ÚÁÙ·|¥[¨ä¥¦¥\¯à ~~ ©Ò¥H§Ú¬Û±N «ö¶sªºµ{¦¡½X³¡¥÷©ñ¨ì class ¤¤
½Ð°Ý¥i¥H«ç»ò°µ¡H

TOP

¦^´_ 7# C.F
class1¼Ò²Õ
  1. Public WithEvents MyBut As MSForms.CommandButton

  2. Private Sub MyBut_Click()
  3. With Test_Form1
  4.   x = .MyList1.ListIndex
  5.   If x = -1 Then Exit Sub
  6.   .MyList2.AddItem .MyList1.List(x)
  7.   .MyList1.RemoveItem x
  8. End With
  9. End Sub
½Æ»s¥N½X
Module1¼Ò²Õ
  1. Public obj As New Class1
  2. Sub Auto_Open()
  3. Dim MyForm As VBComponent
  4. Set MyForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
  5. With MyForm
  6.     .Properties("Caption") = "Test_Form"
  7.     .Name = "Test_Form1"
  8.     With .Designer
  9.       With .Controls.Add("Forms.CommandButton.1")
  10.           .Top = 50
  11.           .Left = 100
  12.           .Height = 20
  13.           .Width = 20
  14.           .Caption = ">>"
  15.           .Name = "Move_Data"
  16.       End With
  17.       For i = 1 To 2
  18.       With .Controls.Add("Forms.Listbox.1")
  19.           .Top = 10
  20.           .Left = (i - 1) * 100 + 20
  21.           .Height = 120
  22.           .Width = 80
  23.           .Name = "MyList" & i
  24.       End With
  25.       Next
  26.     End With
  27.     With .CodeModule
  28.       .InsertLines 1, "Private Sub UserForm_Initialize()"
  29.       .InsertLines 2, "MyList1.List=array(1,2,3,4,5,6,7,8,9)"
  30.       .InsertLines 3, "Set obj.MyBut = Controls(""Move_Data"")"
  31.       .InsertLines 4, "End Sub"
  32.     End With
  33. End With
  34. With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
  35.   .InsertLines 1, "Private Sub Workbook_BeforeClose(Cancel As Boolean)" & Chr(10) & _
  36. "With ThisWorkbook" & Chr(10) & _
  37. ".VBProject.VBComponents.Remove .VBProject.VBComponents(""Test_Form1"")" & Chr(10) & _
  38. "n = .VBProject.VBComponents(""ThisWorkbook"").CodeModule.CountOfLines" & Chr(10) & _
  39. ".VBProject.VBComponents(""ThisWorkbook"").CodeModule.DeleteLines 1, n" & Chr(10) & _
  40. ".Save" & Chr(10) & _
  41. "End With" & Chr(10) & _
  42. "End Sub"
  43. End With
  44. 'OpenForm '¶}±ÒÀɮצ۰ʶ}±Òªí³æ
  45. End Sub
  46. Sub OpenForm()
  47. 'Test_Form1.Show 0 '¶}±Òªí³æ
  48. End Sub
½Æ»s¥N½X
°ÊºA·s¼Wªí³æ.rar (16.51 KB)
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

«D±`·PÁ±zªº«ü¾É ~~ ¦ý§Ú¹J¨ì¤@­Ó°ÝÃD¡A§Ú¤§«e´N¹J¨ì ¤F ~~

°õ¦æ³o¦æªº®É­Ô¦³®É·|µLªk°õ¦æ ~~ ¤£ª¾¬°¦ó µLªk·|¦p ¦¹~
.Name = "Test_Form1"

TOP

·|¥X²{ error 75 °T®§ ~~

TOP

        ÀR«ä¦Û¦b : ºw¤ô¦¨ªe¡C²É¦Ì¦¨ÅÚ¡A¤Å»´¤vÆF¡A¤Å¥Hµ½¤p¦Ó¤£¬°¡C
ªð¦^¦Cªí ¤W¤@¥DÃD