Multiple TabPage classes?

cocotower

New member
Joined
May 27, 2009
Messages
4
Programming Experience
10+
Hello all,

I'm a long-time developer but semi-new to Visual Studio .NET and VB.NET. I've been working on an application for months, written in Visual Basic .NET. It's a rewrite of a popular IDE of mine that's originally written in assembly.

What I'm trying to do in VB.NET is have multiple TabPage classes that inherit from TabPage, ofcourse, but each having it's own methods for common functions like Cut, Copy, Paste, etc. so that when I call tab.Cut(), the TabPage class for the currently-selected tab is the one used. In each custom TabPage class, I'll have a New instance of the main control that goes in that tab.

I think there would be a problem when I loop through the TabPages since the Foreach routine can only use one object type, as far as I know (TabPage).

Currently I have only one custom TabPage class called Page that obviously inherits from TabPage, has additional properties and also methods that allow editing functions for whatever control is selected to be displayed for the tab. For example, when I call the New constructor, I supply the editor type as a parameter: dim tab as New Page(HEX_EDITOR). I have edit controls such as text, syntax editor, hex editor, bitmap editor, HTML editor, HTML browser. This means I get a new instance of all of those controls each time a new Page is added to the TabControl. I'm currently disposing of the unneeded editors depending on which editor is the one needed. The scheme is not preferred but it works great for now as I work on other areas of the IDE.

I'd like to use a HexPage, TextPage, SyntaxPage, etc. with the TabControl and be able to determine the class of that page later when enumerating through the pages, or when a tab is clicked, etc.

Actually, I'd like to have one base Page class that has my additional properties and some common methods, and all other page classes (hex editor, text editor, etc.) would inherit from that base class but only contain custom methods like Cut, Copy, Paste, Delete, SelectAll, etc. All other methods should drop down to the base class, I hope.

I experimented with all of this and ended up having to have those methods in the base class but with no code inside, and each custom Page used Overloads for the same method name and with code that used the editor control in that custom page class.

I hope I'm explaining the situation well enough, but I'm basically trying to simplify the chore of adding multiple editor types to the IDE, without having to have a single TabPage class that creates a new instance of all of the editor controls when the TabPage object is created, then I'm forced to either leave them be or dispose of the unused controls and just Show() the needed one. As of now my Cut, Copy, Paste routines check the DocType() property to determine which control is in use, then performs the needed edit operations on that control using a Select DocType, Case scheme.

It works like a charm, but it's not ideal, and I really need to have a separate class for each editor/TabPage.

Is this possible and is the TabControl.TabPages() collection compatible?
 
You would declare a single base class that inherits from TabPage. In that class you would declare all the common members. You would then derive each individual type of page from that base class and override the common members to provide a specific implementation. You would probably declare the base class MustInherit. The common members could then be declared MustOverride with no implementation, or you could declare them Overridable and also provide a default implementation. E.g.
VB.NET:
Public MustInherit Class TabPageBase
    Inherits TabPage

    Public MustOverride Sub MethodWithoutDefaultImplementation()

    Public Overridable Sub MethodWithDefaultImplementation()
        'Do something here.
    End Sub

End Class
You can then loop through the TabPages of a TabControl like so:
VB.NET:
For Each page As TabPageBase In myTabControl.TabPages
and call your common members on 'page' because they are all declared in the TabPageBase class.

Another option would be to define an interface that all your classes would implement. The main difference there is that you cannot provide a default implementation in an interface, which may be fine.
 
Thanks! Your suggestion is working. Besides trying to Overload instead of Override, I wasn't using Mustinherit and was also using End Sub in my "empty" methods in the base class.

I've successfully moved the hex editor and syntax editor out of the base class and into their own derived tab page class and confirmed that Cut, Copy, Paste, Select All, Delete, Undo, and Redo all work for each of those editors/tabs.

Much thanks!
 
I would LOVE it if you could post a basic set of the custom tab page code you use. My custom control won't draw the controls I have added to it.
 
Back
Top