2012-05-10 148 views
0

我正在一個項目中工作,其中一些表單將會有重複填充組合框的方法。在下面的代碼片段中,給定pc上安裝的字體將作爲項目添加到組合框中。我如何傳遞一個參數來填充實際的組合框?例如,AddFonts(組合框)傳遞一個控件作爲參數

Private Sub AddFonts() 
    'add the font names installed on this pc to the font name combo box 
    ' Get the installed fonts collection. 
    Dim allFonts As New InstalledFontCollection 
    ' Get an array of the system's font familiies. 
    Dim fontFamilies() As FontFamily = allFonts.Families 

    ' Display the font families. 
    For i As Integer = 0 To fontFamilies.Length - 1 
     'figure our how to make the textbox passable as a paramter 
     cbxTitleFonts.Items.Add(fontFamilies(i).Name) 
    Next 
End Sub 

回答

0

傳遞控制如控制數據類型和在函數的實際控制澆鑄它。

Public Sub mycallingfunc() 
    myfunc(textbox1) 
End Sub 

Public Shared Sub myfunc(ctrl As Control) 
    Dim txt As TextBox = DirectCast(ctrl, TextBox) 
End Sub 
+0

是的,我會使用DirectCast,但參數如何實際傳遞? – dinotom

+0

的名字?例如,如果組合框名稱是foo,那麼在上例中就是myfunc(foo)? – dinotom

+0

感謝羅米爾下面的代碼是調用子AddFonts(Me.combobox)時的解決方案, – dinotom