Imports System.IO
Imports System.ComponentModel

'<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class FileButton
    Inherits System.Windows.Forms.Button
    ' members...
    Private _buddyBoxName As String

    ' event...
    Public Event OpenFile(ByVal sender As Object, _
                 ByVal e As OpenFileEventArgs)


    ' BuddyBoxName property
    '<EditorBrowsableAttribute(EditorBrowsableState.Always)> _
    Property BuddyBoxName() As String
        Get
            Return _buddyBoxName
        End Get
        Set(ByVal Value As String)            ' Perhaps add validation code here
            _buddyBoxName = Value
        End Set
    End Property

    ' GetBuddyBox - return the actual TextBox control by looking through
    ' the parent's Controls property...
    Public Function GetBuddyBox() As TextBox
        ' search name...
        Dim searchFor As String = BuddyBoxName.ToLower

        ' look through each control...
        Dim control As Control
        For Each control In Parent.Controls

            ' does the name match?
            If control.Name.ToLower = searchFor Then

                ' we have a match...  now, cast the control
                ' to a text box and return...
                Return CType(control, TextBox)

            End If

        Next

    End Function


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Private Sub FileButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        ' try and get the buddy box...
        Dim buddyBox As TextBox = GetBuddyBox()
        If buddyBox Is Nothing Then
            MsgBox("The buddy box could not be found.")
        Else
            ' open the file and return the results...
            Dim stream As Stream = File.Open(buddyBox.Text, FileMode.Open)
            Dim reader As New StreamReader(stream)

            ' load the entire file...
            Dim contents As String = reader.ReadToEnd
            ' close the stream and the reader...
            stream.Close()
            reader.Close()

            ' raise the OpenFile event
            Dim args As New OpenFileEventArgs()
            args.FileName = buddyBox.Text
            args.FileText = contents
            RaiseEvent OpenFile(Me, args)
        End If

    End Sub
End Class
