2017-09-02 16 views
0

我有一個用戶窗體的代碼與列表框,但是當我顯示用戶窗體的列表框是空的,但當我不顯示我可以看到的項目。我有一個用戶窗體的列表框,但當我顯示用戶窗體的列表框是空的,但是當我不顯示我可以看到項目

顯式的選項

Dim Tableau(0) As String 

Dim myForm As Object ' or use 'As VBComponent' 
Dim Ctrl As Control 

Dim NewButton1 As MSForms.CommandButton 
Dim NewButton2 As MSForms.CommandButton 

Sub創建()

Tableau(0) = "1" 

Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3) 
With myForm 
    .Properties("Caption") = "Choisir" 
    .Properties("Width") = 200 
    .Properties("Height") = 140 

    .CodeModule.InsertLines 2, "Public sub userform_initialize()" '<--| start writing your "UserForm_Initialize" sub code 
    With .Designer.Controls.Add("forms.listbox.1", Name:="ListBox1") 

     .Width = 110 
     .Enabled = True 

     .ListIndex = -1 
     .AddItem "Test N x M", 0 
     .AddItem "Test Wet strength", 1 
     .AddItem "Test Pressure Loss with Fine", 2 
     .AddItem "Test Pressure Loss with Coarse", 3 

    End With 
    .CodeModule.InsertLines 4, "End sub" '<--| finish writing your "UserForm_Initialize" sub code 
    Set NewButton1 = myForm.Designer.Controls.Add("Forms.commandbutton.1") 
    With NewButton1 
     .Name = "OK" 
     .Caption = "OK" 
     .Accelerator = "M" 
     .Top = 20 
     .Left = 120 
     .Width = 40 
     .Height = 20 
     .Font.Size = 8 
     .Font.Name = "Tahoma" 
     .BackStyle = fmBackStyleOpaque 
    End With 

    Set NewButton2 = myForm.Designer.Controls.Add("Forms.commandbutton.1") 
    With NewButton2 
     .Name = "CANCEL" 
     .Caption = "CANCEL" 
     .Accelerator = "M" 
     .Top = NewButton1.Top + 20 
     .Left = 120 
     .Width = 40 
     .Height = 20 
     .Font.Size = 8 
     .Font.Name = "Tahoma" 
     .BackStyle = fmBackStyleOpaque 

    End With 


End With 




myForm.CodeModule.InsertLines 20, "Private Sub ListBox1_Click()" 
myForm.CodeModule.InsertLines 21, "Me.ListBox1.Show" 
myForm.CodeModule.InsertLines 22, "If Me.ListBox1.ListIndex = 0 Then" 
myForm.CodeModule.InsertLines 23, " Me.ListBox1.Selected(0) = True" 
myForm.CodeModule.InsertLines 24, "End If" 
myForm.CodeModule.InsertLines 25, "If Me.ListBox1.ListIndex = 1 Then" 
myForm.CodeModule.InsertLines 26, " Me.ListBox1.Selected(1) = True" 
myForm.CodeModule.InsertLines 27, "End If" 

myForm.CodeModule.InsertLines 28, "If Me.ListBox1.ListIndex = 2 Then" 
myForm.CodeModule.InsertLines 29, " Me.ListBox1.Selected(2) = True" 
myForm.CodeModule.InsertLines 30, "End If" 
myForm.CodeModule.InsertLines 31, "If Me.ListBox1.ListIndex = 3 Then" 
myForm.CodeModule.InsertLines 32, " Me.ListBox1.Selected(3) = True" 
myForm.CodeModule.InsertLines 33, "End If" 
myForm.CodeModule.InsertLines 34, "" 
myForm.CodeModule.InsertLines 35, "End Sub" 

myForm.CodeModule.InsertLines 36, "Private Sub OK_Click()" 

myForm.CodeModule.InsertLines 37, "If Me.ListBox1.Selected(0) = True Then" 
myForm.CodeModule.InsertLines 38, " Msgbox(""Item 1 selected"" & vbLf)" 
myForm.CodeModule.InsertLines 39, " Call testNxM" 
myForm.CodeModule.InsertLines 40, "End If" 
myForm.CodeModule.InsertLines 41, "If Me.ListBox1.Selected(1) = True Then" 
myForm.CodeModule.InsertLines 42, " Msgbox(""Item 2 selected"" & vbLf)" 
myForm.CodeModule.InsertLines 43, " Call testWet" 
myForm.CodeModule.InsertLines 44, "End If" 
myForm.CodeModule.InsertLines 45, "If Me.ListBox1.Selected(2) = True Then" 
myForm.CodeModule.InsertLines 46, " Msgbox(""Item 3 selected"" & vbLf)" 
myForm.CodeModule.InsertLines 47, " Call testPLFine" 
myForm.CodeModule.InsertLines 48, "End If" 
myForm.CodeModule.InsertLines 49, "If Me.ListBox1.Selected(3) = True Then" 
myForm.CodeModule.InsertLines 50, " Msgbox(""Item 4 selected"" & vbLf)" 
myForm.CodeModule.InsertLines 51, " Call testPLCoarse" 
myForm.CodeModule.InsertLines 52, "End If" 
myForm.CodeModule.InsertLines 53, "Unload me" 
myForm.CodeModule.InsertLines 54, "End Sub" 
myForm.CodeModule.InsertLines 55, "Private Sub CANCEL_Click()" 
myForm.CodeModule.InsertLines 56, "Unload Me" 
myForm.CodeModule.InsertLines 57, "End Sub" 

VBA.UserForms.Add(myForm.Name).Show 


'ThisWorkbook.VBProject.VBComponents.Remove myForm 

末次

時,我想驗證控制,所有的,我不顯示用戶窗體,我可以看到所有的項目,並可以點擊它們,但是當我展示時,它只是一個帶有2個按鈕和空列表框的用戶表單

回答

0

我不能完全回答你爲什麼不工作,但我想這是與轉換到運行時間有關。但是一個簡單的解決方案是增加在運行時該列表IE添加

MyForm.codemodule.insertlines 58, "Private Sub UserForm_Activate()" 
MyForm.codemodule.insertlines 59, "with me.listbox1" 
MyForm.codemodule.insertlines 60, " .additem ""blah blah""" 
MyForm.codemodule.insertlines 61, "end with" 
MyForm.codemodule.insertlines 62, "End Sub" 

你的代碼的底部,它應該工作...

相關問題