Partial Public Class Form2 Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim human1 As New BarcodeReader() human1.DataReceived(TextBox1.Text) End Sub End Class Public NotInheritable Class BarcodeReader Public Sub DataReceived(arg As String) Dim g_Data As String = arg MsgBox(g_Data) End Sub End Class
Dim human1 As New BarcodeReader(TextBox1) ' Class BarcodeReader ' Public Sub New(txt As TextBox) ''' g_Data = txt.Text Dim human1 As New BarcodeReader(New Func(Of String)(Function() TextBox1.Text)) ' Public NotInheritable Class BarcodeReader ' Public Sub New(getText As Func(Of String)) ''' g_Data = getText.Invoke()
human1.RequireText = Function() TextBox1.Text ' Public NotInheritable Class BarcodeReader ' Public Property RequireText As Func(Of String) ''' g_Data = RequireText.Invoke()
human1.DataReceived(Me.TextBox1) ' Public NotInheritable Class BarcodeReader ' Public Sub DataReceived(owner As Control) ''' g_Data = owner.Text human1.DataReceived(Me) ' Class BarcodeReader ' ' この方法だと、TextBox1 の Modifiers プロパティを Private にできなくなるので ' ' 実際は Form2 ではなく、自作 Interface を渡した方が望ましいと思います ' Public Sub DataReceived(owner As Form2) ''' g_Data = owner.TextBox1.Text
Partial Public Class Form2 Private WithEvents human As BarcodeReader Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click human = New BarcodeReader() human.DataReceived() End Sub Private Sub human_Require(sender As Object, e As BarcodeReader.RequireEventArgs) Handles human.Require e.Data = TextBox1.Text End Sub End Class ' Form2 以外からも呼び出せるようになるので、より汎用的な実装になる Public NotInheritable Class BarcodeReader Public Event Require As EventHandler(Of RequireEventArgs) Public Sub DataReceived() Dim arg As New RequireEventArgs() RaiseEvent Require(Me, arg) Dim g_Data As String = arg.Data MsgBox(g_Data) End Sub Public Class RequireEventArgs Inherits EventArgs Public Property Data As String End Class End Class