검색 텍스트에 사용할 컨트롤에 대한 참조를 전달할 수 있습니다. 이렇게하면 SearchFields 메서드가보다 일반적으로 사용됩니다. 예를 들어 frmNew
이라는 양식과 Searching
이라는 모듈을 만들었습니다. 양식에서 나는 Ok
, TextBox 및 ComboBox라는 버튼을 배치했습니다.
Public Class frmNew
Private Sub Ok_Click(sender As Object, e As EventArgs) Handles Ok.Click
Searching.SearchFields(TextBox1)
Searching.SearchFields(ComboBox1)
Me.Close()
End Sub
End Class
는 모듈에 전달 된 제어 (I는 Searching
지명하는) 처리에 대해 취할 수있는 두 가지 방법이 있습니다. 먼저, 컨트롤의 종류를 확인하고 그 기반으로 작업을 수행 할 수 있습니다
Module Searching
Sub SearchFields(textSource As Control)
Dim str As String = ""
' just for invesigating, show the type of the control.
Console.WriteLine(TypeName(textSource))
If TypeOf textSource Is System.Windows.Forms.TextBox Then
str = textSource.Text
ElseIf TypeOf textSource Is System.Windows.Forms.ComboBox Then
Dim src = DirectCast(textSource, ComboBox)
If src.SelectedIndex >= 0 Then
str = src.SelectedItem.ToString()
Else
' nothing was selected. Do whatever is appropriate.
str = "NOTHING SELECTED!"
End If
End If
'TODO: the searching code.
Console.WriteLine(str)
End Sub
End Module
또는 당신이 인수에 대응하는 방법의 버전을 실행하는 방법 오버로드, 활용할 수 있습니다 (들) 너는 그것에 패스한다 :
Module Searching
Sub SearchFields(src As TextBox)
DoSearch(src.Text)
End Sub
Sub SearchFields(src As ComboBox)
'TODO: check an item is selected.
Dim txt = src.SelectedItem.ToString()
DoSearch(txt)
End Sub
Private Sub DoSearch(s As String)
' do the search
Console.WriteLine(s)
End Sub
End Module
어떤 부호를 첫째로 제공하십시오. – Neolisk
설명을 추가했습니다. – user3180163
'Dim frm As New frmNew'가 기존 양식을 참조하지 않습니다. 매개 변수로 참조를 전달해야합니다. .Net으로 이동하려면 프로그램의 구조를 재고해야합니다. 모듈은 일반적으로 클래스로 대체되어야합니다. 대부분 전역 변수를 피하십시오. – LarsTech