473,811 Members | 3,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I call a method from the base class

8 New Member
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
Jan 4 '11 #1
5 1842
Oralloy
988 Recognized Expert Contributor
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 Recognized Expert New Member
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
Audie7734
8 New Member
I tried the SET keyword, it caused a syntax error.
Jan 4 '11 #4
Joseph Martell
198 Recognized Expert New Member
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
Audie7734
8 New Member
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.
Jan 5 '11 #6

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

Similar topics

12
1640
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 work, while I think it should be possible: A *pA = new B; // One of... A *pA = new C; // ...these two
4
6082
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 you use the :: scoping operator to directly call the base class method. Like this: class BASE
1
14137
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 class, I don't know how to call its TextLength property. Any ideas? public override int TextLength { get { return base.TextLength; //Uses RichTextBox.TextLength, which I
1
2968
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 protected method in the parent class ... I have search all over for an example of how to do this but have failed. What I was wondering is about the syntax necessary to make such a call ..... Thx.. Bob Rock
10
1547
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 base class method is not to be abstract, and will be called if the instance was created as a 'generic' base class instance. It's sort of like I want the method to be abstract to children, but concrete at the base level. That way I can refer to...
5
3167
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
1
1476
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 the base class for an event like this? //class Form class Form:System.Windows.Forms.Form {
6
27818
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 was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
5
2302
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: ChildCheckBoxList : CheckBoxList
2
1816
by: RAVISEKHAR | last post by:
what happens if i call a base class virtual function in a derived class constructor?
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.