473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I call a method from the base class

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
Jan 4 '11 #1
5 1831
Oralloy
988 Expert 512MB
I'm working with VBA in access, so this is just a guess:
Expand|Select|Wrap|Line Numbers
  1. Dim xForm As Form1 
  2. Set xForm = new Form1
Merry New Year!
Oralloy
Jan 4 '11 #2
Joseph Martell
198 Expert 128KB
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 4 '11 #3
I tried the SET keyword, it caused a syntax error.
Jan 4 '11 #4
Joseph Martell
198 Expert 128KB
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.
Jan 4 '11 #5
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(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.
Jan 5 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: ectoplasm | last post by:
Abstract base class A. Derived from A are class B and C, both concrete. I hold a pointer to an object of class A, an instance which is either B or C. I have trouble with making the following...
4
by: Amy Matlock | last post by:
Hi all: How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class member? Do you still hit the v-table dynamically at run time? Suppose you have a derived class method, but...
1
by: Bill Menees | last post by:
I've got a RichTextBoxEx inherited from RichTextBox, and I want to overload the TextLength property to use TextBoxBase's implementation of TextLength. But since TextBoxBase isn't my immediate base...
1
by: Bob Rock | last post by:
Hello, I have read that the base keyword can be used not only to control a base class instantiation (thus calling one of the base class constructors) but also to access any other public or...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
1
by: Martijn Mulder | last post by:
Documentation suggests that I should make a call to the base-class method for some methods. When I try to do so for the class below, I get wrong results. What is the correct way to make a call to...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
5
by: macap.usenet | last post by:
Hello, I´ve a problem which maybe sounds a bit easy, but it is hard do ask google for the problem. I have a CheckBoxList and a class derived from this checkboxlist Let´s say: ...
2
by: RAVISEKHAR | last post by:
what happens if i call a base class virtual function in a derived class constructor?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.