将控件作为参数传递

时间:2012-05-10 07:39:47

标签: winforms vb.net-2010

我正在开发一个项目,其中某些表单将有重复的方法来填充组合框。在下面的代码片段中,安装在给定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

1 个答案:

答案 0 :(得分:0)

将控件作为控件数据类型传递,并将其转换为函数中的实际控件。

Public  Sub mycallingfunc()
    myfunc(textbox1)
End Sub

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