Results 1 to 8 of 8

Thread: Use code on multiple forms without copying it? inheritance?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Use code on multiple forms without copying it? inheritance?

    Hey there, I'm working on a game set in a simulated computer environment. I've made a template forum which I use to copy and paste the code from to every single form. Here's a small bit of the code I use within the game that is copied to all the windows:

    Code:
     Public Sub setuptitlebar()
    
            titlebar.Height = ShiftOSDesktop.titlebarheight
            Me.Size = New Size(366, 167) 'put the default size of your window here
            Me.Size = New Size(Me.Width, Me.Height + ShiftOSDesktop.titlebarheight - 30)
    
            pgleft.Width = ShiftOSDesktop.windowbordersize
            pgright.Width = ShiftOSDesktop.windowbordersize
            pgbottom.Height = ShiftOSDesktop.windowbordersize
            Me.Size = New Size(Me.Width + ShiftOSDesktop.windowbordersize + ShiftOSDesktop.windowbordersize, Me.Height + ShiftOSDesktop.windowbordersize)
    
            If ShiftOSDesktop.showwindowcorners = True Then
                pgtoplcorner.Show()
                pgtoprcorner.Show()
            Else
                pgtoplcorner.Hide()
                pgtoprcorner.Hide()
            End If
    
            If ShiftOSDesktop.boughttitlebar = False Then
                titlebar.Hide()
                Me.Size = New Size(Me.Width, Me.Size.Height - titlebar.Height)
            End If
    
            If ShiftOSDesktop.boughttitletext = False Then
                lbtitletext.Hide()
            Else
                lbtitletext.Font = New Font(ShiftOSDesktop.titletextfont, ShiftOSDesktop.titletextsize, ShiftOSDesktop.titletextstyle)
                lbtitletext.Show()
            End If
    
            If ShiftOSDesktop.boughtclosebutton = False Then
                closebutton.Hide()
            Else
                closebutton.BackColor = ShiftOSDesktop.closebuttoncolour
                closebutton.Height = ShiftOSDesktop.closebuttonheight
                closebutton.Width = ShiftOSDesktop.closebuttonwidth
                closebutton.Show()
            End If
    
            If ShiftOSDesktop.boughtrollupbutton = False Then
                rollupbutton.Hide()
            Else
                rollupbutton.BackColor = ShiftOSDesktop.rollupbuttoncolour
                rollupbutton.Height = ShiftOSDesktop.rollupbuttonheight
                rollupbutton.Width = ShiftOSDesktop.rollupbuttonwidth
                rollupbutton.Show()
            End If
    
            If ShiftOSDesktop.boughtwindowborders = True Then
                closebutton.Location = New Point(titlebar.Size.Width - ShiftOSDesktop.closebuttonside - closebutton.Size.Width, ShiftOSDesktop.closebuttontop)
                rollupbutton.Location = New Point(titlebar.Size.Width - ShiftOSDesktop.rollupbuttonside - rollupbutton.Size.Width, ShiftOSDesktop.rollupbuttontop)
                Select Case ShiftOSDesktop.titletextposition
                    Case "Left"
                        lbtitletext.Location = New Point(ShiftOSDesktop.titletextside, ShiftOSDesktop.titletexttop)
                    Case "Centre"
                        lbtitletext.Location = New Point((titlebar.Width / 2) - lbtitletext.Width / 2, ShiftOSDesktop.titletexttop)
                End Select
                lbtitletext.ForeColor = ShiftOSDesktop.titletextcolour
            Else
                closebutton.Location = New Point(titlebar.Size.Width - ShiftOSDesktop.closebuttonside - pgtoplcorner.Width - pgtoprcorner.Width - closebutton.Size.Width, ShiftOSDesktop.closebuttontop)
                rollupbutton.Location = New Point(titlebar.Size.Width - ShiftOSDesktop.rollupbuttonside - pgtoplcorner.Width - pgtoprcorner.Width - rollupbutton.Size.Width, ShiftOSDesktop.rollupbuttontop)
                Select Case ShiftOSDesktop.titletextposition
                    Case "Left"
                        lbtitletext.Location = New Point(ShiftOSDesktop.titletextside + pgtoplcorner.Width, ShiftOSDesktop.titletexttop)
                    Case "Centre"
                        lbtitletext.Location = New Point((titlebar.Width / 2) - lbtitletext.Width / 2, ShiftOSDesktop.titletexttop)
                End Select
                lbtitletext.ForeColor = ShiftOSDesktop.titletextcolour
            End If
    
            If ShiftOSDesktop.boughtclockicon = True Then
                pnlicon.Visible = True
                pnlicon.Location = New Point(ShiftOSDesktop.titlebariconside, ShiftOSDesktop.titlebaricontop)
            End If
    
        End Sub
    Is there a way I can simply say "Inherit setuptitlebar" in each window or something so I don't have to copy over all the code to each window? I often update the code and copying it across to 20 windows again and again (soon to be more) drives me mad.

    So what's the simplest way to do this knowing that the controls on each window have the exact same names?

    P.S The graphical environment is highly customizable so don't freak out or tell me that I'm over-complicating the code... trust me, I'm not!

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Use code on multiple forms without copying it? inheritance?

    Put the code in a module (class?) and call it from each form, something like:

    (Module code)

    Code:
    Module Module1
        Public Sub formatForm(ByVal whichForm As Form)
            whichForm.BackColor = Color.Blue
            whichForm.Height = 100
            whichForm.Width = 200
            whichForm.StartPosition = FormStartPosition.Manual
            whichForm.Left = 0
            whichForm.Top = 0
        End Sub
    End Module
    (Form code)

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Call Module1.formatForm(Me)
        End Sub

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Use code on multiple forms without copying it? inheritance?

    You mentioned inheritance in your thread title. Why not use it? You simply design a form, put that method into it, along with any others that you want to share, and then have all your other forms inherit from it.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Use code on multiple forms without copying it? inheritance?

    FD, I don't have any experience along those lines. If he does it that way, when he changes the one that he inherits from, do the others get "updated automatically?"

    Bryce

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Use code on multiple forms without copying it? inheritance?

    Yes, that's the point of inheritance. All the derived forms share the behaviour of the forms they inherit from. If the behaviour of the inherited form changes then so does the behaviour of all the derived forms.

    It does introduce a bit of a potential problem though: what to do if you want to update the template for but have one or two forms whose behaviour does not change. In that case the individual forms whose behavious you want to differ need to 'override' or 'hide' the method(s) of the forms from which they inherit. Overriding is generally recommended. That means they can have their own specific implementation that supercedes the shared behaviour. All told inheritance is a very powerful technique for achieving code re-use.

    It's proabbly a little advanced if you're coming to this stuff green but you might also want to read up on the strategy pattern. Inheritance and overriding will only take you so far and starts to become inneficient when you have several different behaviours, each of which is shared by some but not all of your forms. Inheritance only allows for a single shared behaviour so this is difficult to implement using inheritance without creating a horribly complex structure. What you do instead is compose different behaviours, similar to calling a method from a module as you suggested. There's a few key differences that make it more robust though and it's important to understand them so it's worth coming to this via inheritance.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Use code on multiple forms without copying it? inheritance?

    Thanks for explaining!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Use code on multiple forms without copying it? inheritance?

    So lets say that code I shared in the first post is within a form called "template". I then want to use that in a form called "textpad" and "clock" and "Shifter" and 10 other forms... how do I do that? What do I type and where do I type it to get that code in the template to be used across all the forms?

    I work best when I see an example. I then modify and experiment with the example to learn.

    Thanks in advance for help Also note that I've never used inheritance before but my current project "ShiftOS" is screaming for me to use it.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Use code on multiple forms without copying it? inheritance?

    Right-click the project. Select Add -> New Item.
    In the Add New Item dialog that comes up, select Windows Forms on the left. On the right, find "Inherited Form".
    Click Add.
    WAIT...
    Eventually another dialog will come up asking for which form to inherit from... select your template form...
    Click OK.
    done... you've just added an inherited form to your project.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width