REF: http://msdn.microsoft.com/en-us/library/aa289500(v=vs.71).aspx (I added a RichTextBox to Form1)
The above reference is where I got the code below.
This code adds buttons to Form1. When a button is clicked a messagebox appears stating what button was clicked. I want to have the ClickHandler fill a RichTextBox on Form1 (I added a RTB to Form1). My problem is I can't do a Dim xForm As New Form1 to fill the RTB from the ButtonArray Class. I can place my file's text in a MessageBox so I know the code I added is correct. I also tried placing a Property method on Form1 and using a Accessor to fill the RTB, however it also requires Dim xForm As New Form1. How can I get my data from files to the RichTextBox on Form1 from the ClickHandler in the ButtonArray Class? See ButtonArray Class Below.
Imports System.IO
Imports System.Text
Imports System.Collections.CollectionBase
Public Class ButtonArray
Inherits System.Collections.CollectionBase
Dim objReader As StreamReader
Dim strRead As String
Private ReadOnly HostForm As System.Windows.Forms.Form
Public Function AddNewButton() As System.Windows.Forms.Button
'Create a new instance of the Button class.
Dim aButton As New System.Windows.Forms.Button
'Add the button to the collection's internal list.
Me.List.Add(aButton)
'Add the button to the controls collection of the form
'referenced by the HostForm field.
HostForm.Controls.Add(aButton)
'Set initial properties for the button object.
aButton.Height = 30 'I added this.
aButton.Width = 40 'I added this.
aButton.Top = Count * 30 '25 I changed this to 30 from 25.
aButton.Left = 50
aButton.Tag = Me.Count
aButton.Text = "Button" & Me.Count.ToString()
AddHandler aButton.Click, AddressOf ClickHandler
Return aButton
End Function
Public Sub New(ByVal host As System.Windows.Forms.Form)
HostForm = host
Me.AddNewButton()
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As System.Windows.Forms.Button
Get
Return CType(Me.List.Item(Index), System.Windows.Forms.Button)
End Get
End Property
Public Sub Remove()
'Check to be sure there is a button to remove.
If Me.Count > 0 Then
'Remove the last button added to the array from the host form
'controls collection. Note the use of the default property in
'accessing the array.
HostForm.Controls.Remove(Me(Me.Count - 1))
Me.List.RemoveAt(Me.Count - 1)
End If
End Sub
Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim key As String
Dim keyBuilder As New StringBuilder
'MessageBox.Show("You have clicked Button " & CType(CType(sender, System.Windows.Forms.Button).Tag, String))
'MessageBox.Show("You have clicked Button " & CType(CType(sender, System.Windows.Forms.Button).Text, String))
'I added this section. I want text read from file to be placed in the
'RichTextBox (rtbKeys) on Form1. Dim xForm As New Form1 doesn't work.
key = CType(CType(sender, System.Windows.Forms.Button).Text, String)
key = key.Substring(5, key.Length - 5)
If File.Exists("C:\Securitas\Keys\" & key & "\" & key & ".txt") Then
objReader = File.OpenText("C:\Securitas\Keys\" & key & "\" & key & ".txt")
Do Until objReader.Peek = -1
strRead = objReader.ReadLine
If strRead <> Nothing Then
keyBuilder.Append(strRead)
keyBuilder.Append(vbCrLf)
End If
Loop
objReader.Close()
End If
MsgBox(keyBuilder.ToString().TrimEnd) 'This works.
End Sub
End Class