2012-05-10 7 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

ありがとうRomil下のコードは、AddFonts(Me.combobox)のようなサブメニューを呼び出すときの解決策です。 – dinotom

関連する問題