How do I call a method from the base class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Audie7734
    New Member
    • Jul 2008
    • 8

    #1

    How do I call a method from the base class

    REF: http://msdn.microsoft. com/en-us/library/aa289500(v=vs.7 1).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.Collecti ons.CollectionB ase

    Public Class ButtonArray
    Inherits System.Collecti ons.CollectionB ase

    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(aBu tton)
    'Add the button to the controls collection of the form
    'referenced by the HostForm field.
    HostForm.Contro ls.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.ToStri ng()
    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.I tem(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.Contro ls.Remove(Me(Me .Count - 1))
    Me.List.RemoveA t(Me.Count - 1)
    End If
    End Sub

    Public Sub ClickHandler(By Val sender As Object, ByVal e As System.EventArg s)
    Dim key As String
    Dim keyBuilder As New StringBuilder

    'MessageBox.Sho w("You have clicked Button " & CType(CType(sen der, System.Windows. Forms.Button).T ag, String))

    'MessageBox.Sho w("You have clicked Button " & CType(CType(sen der, System.Windows. Forms.Button).T ext, 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(sen der, System.Windows. Forms.Button).T ext, String)
    key = key.Substring(5 , key.Length - 5)

    If File.Exists("C: \Securitas\Keys \" & key & "\" & key & ".txt") Then
    objReader = File.OpenText(" C:\Securitas\Ke ys\" & key & "\" & key & ".txt")
    Do Until objReader.Peek = -1
    strRead = objReader.ReadL ine
    If strRead <> Nothing Then
    keyBuilder.Appe nd(strRead)
    keyBuilder.Appe nd(vbCrLf)
    End If
    Loop
    objReader.Close ()
    End If
    MsgBox(keyBuild er.ToString().T rimEnd) 'This works.
    End Sub
    End Class
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    I'm working with VBA in access, so this is just a guess:
    Code:
    Dim xForm As Form1 
    Set xForm = new Form1
    Merry New Year!
    Oralloy

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      Remember that a form is just a class like any other class in VB/C#. The only difference between a form, like Form1 in your case, and a string is that the Visual Studio IDE treats the form class in a very special way. It gives you a visual form editor. When you write:
      Code:
      Dim xForm as New Form1()
      you are creating a new instance of Form1. Remember that when you created an instance of your ButtonArray class, you passed a reference to your, already running, instance of Form1. Your ButtonArray is holding a reference to Form1 in the HostForm variable.

      However, HostForm is of type Form, which means that whatever extra methods or properties you added to Form1 are inaccessible through HostForm because the reference has been up-cast to a more general type. So close, yet so far away.

      One solution is to change the type of HostForm to Form1 and to change the type of the parameter passed in the ButtonArray constructor to Form1 as well. This will preserve the type of the reference (no up-casting) and allow you to access the rich text box and any methods that you have added to your Form1 class.

      I would be remiss if I didn't point out that the downside of this course of action is that you are "tightly coupling" your button array to your particular Form1 class. This means that your ButtonArray class can only be used with your particular form or its descendants. If the ButtonArray class is meant to be a distributable class that multiple people will use in different contexts, then you have a problem. If you are just using this as an exercise to teach yourself something, then there really is no problem with tightly coupling your class to your form.

      I know that this is a pretty complicated explanation, but just remember that "New" keyword creates a new object every time you use it. If you want a reference to an existing object, then you will not get it by using the "New" keyword.

      Comment

      • Audie7734
        New Member
        • Jul 2008
        • 8

        #4
        I tried the SET keyword, it caused a syntax error.

        Comment

        • Joseph Martell
          Recognized Expert New Member
          • Jan 2010
          • 198

          #5
          The SET keyword is deprecated in modern VB. In VBA it is still used, but that is because VBA is based on VB6. VB6 handles objects in a very strange, and rather clunky way.

          Comment

          • Audie7734
            New Member
            • Jul 2008
            • 8

            #6
            I tried changing the HostForm to Form1. It still didn't work. The problem arises when I tried to make a new instance of Form1. However I tried something that works but I don't think it is very productive. I made a global varible in the ButtonArray Class and a Property Method to access the varible.

            Dim xKey As String

            Public Property AddKeys()
            Get
            Return xKey
            End Get
            Set(ByVal Value)

            End Set
            End Property

            Then I used the Timer Function to update the Text in my RichTextBox.

            Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick
            rtbKeys.Text = MyControlArray. AddKeys()
            End Sub

            Is there a way to do this by:
            (1) reacting to the clickEvent in the ButtonArray Class? although I don't think this works between classes.
            (2) reacting to a change of a varible in another class.

            Comment

            Working...