none
Moving Dynamic controls to a seperate file

    Question

  • I have a custom control set up like this

    [code]
    Public Class ctrlAddSitesMenu

        'Declares Buttons for Opening Websites
        Dim OpenTraker As New Button
        Dim OpenDynamics As New Button
        Dim OpenInetPortal As New Button
        Dim OpenFrontPortal As New Button
        Dim OpenLoopcare As New Button
        Dim OpenViryNet As New Button
        Dim OpenSmpl As New Button
        Dim OpenFosWiki As New Button
        Dim OpenLola As New Button

        'Declare Button Constant Varibles
        Dim LocX As Integer = 0
        Dim LocY As Integer = 20
        Dim Row As Integer = 25
        Dim H As Integer = 25
        Dim W As Integer = 115

        Public Sub LoadButtons()

            'Declares attributes for Invidual buttons
            'Declares Button names
            OpenTraker.Name = "btnOpenTracker"
            OpenDynamics.Name = "btnOpenDynamics"
            OpenInetPortal.Name = "btnInetPortal"
            OpenFrontPortal.Name = "btnFrontPortal"
            OpenLoopcare.Name = "btnOpenLoopcare"
            OpenViryNet.Name = "btnOpenVirynet"
            OpenSmpl.Name = "btnOpenSimple"
            OpenFosWiki.Name = "btnOpenFosWiki"
            OpenLola.Name = "btnOpenLola"

            'Declares Button text
            OpenTraker.Text = "Tracker"
            OpenDynamics.Text = "Dynamics"
            OpenInetPortal.Text = "Internet Portal"
            OpenFrontPortal.Text = "Frontier Portal"
            OpenLoopcare.Text = "Loopcare"
            OpenViryNet.Text = "Virynet"
            OpenSmpl.Text = "SIMPL"
            OpenFosWiki.Text = "FosWiki"
            OpenLola.Text = "Lola"

            'Declares Button Size
            OpenTraker.Size = New Size(W, H)
            OpenDynamics.Size = New Size(W, H)
            OpenInetPortal.Size = New Size(W, H)
            OpenFrontPortal.Size = New Size(W, H)
            OpenLoopcare.Size = New Size(W, H)
            OpenViryNet.Size = New Size(W, H)
            OpenSmpl.Size = New Size(W, H)
            OpenFosWiki.Size = New Size(W, H)
            OpenLola.Size = New Size(W, H)

            ' Declares Button Location
            OpenTraker.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenDynamics.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenInetPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFrontPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLoopcare.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenViryNet.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenSmpl.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFosWiki.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLola.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row


            'Adds Buttons to Menu
            Me.Controls.Add(OpenTraker)
            Me.Controls.Add(OpenDynamics)
            Me.Controls.Add(OpenInetPortal)
            Me.Controls.Add(OpenFrontPortal)
            Me.Controls.Add(OpenLoopcare)
            Me.Controls.Add(OpenViryNet)
            Me.Controls.Add(OpenSmpl)
            Me.Controls.Add(OpenFosWiki)
            Me.Controls.Add(OpenLola)


            'Adds Button Click Handlers
            AddHandler OpenTraker.Click, AddressOf OpenTrackerClick
            AddHandler OpenDynamics.Click, AddressOf OpenDynamicsClick
            AddHandler OpenInetPortal.Click, AddressOf OpenInetPortalClick
            AddHandler OpenFrontPortal.Click, AddressOf OpenFrontPortalClick
            AddHandler OpenLoopcare.Click, AddressOf OpenLoopcareClick
            AddHandler OpenViryNet.Click, AddressOf OpenViryNetClick
            AddHandler OpenSmpl.Click, AddressOf OpenSmplClick
            AddHandler OpenFosWiki.Click, AddressOf OpenFosWikiClick
            AddHandler OpenLola.Click, AddressOf OpenLolaClick

        End Sub
        Private Sub addSitesMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try

            Catch ex As Exception
            End Try
        End Sub

        Private Sub OpenTrackerClick(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show("OpenTracker Button Clicked")
        End Sub
    End Class
    [/code]

    IN order to clean thing up a bit, I want to move The "LoadButtons" Class to a different file, I assume it would be a class file.

    Two things happen I get an error in the Addhandler saying the Open*****Click ins not declared. I also cant seem to figure out how to properly call it.  

    I have tried 

    [code]
    Try
                LoadButtons()
            Catch ex As Exception
            End Try
    [/code]

    and 

    [code]
    Try
                classname.LoadButtons()
            Catch ex As Exception
            End Try
    [/code]

    I have also tried 

    [code]
    Imports Classname
    [/code]

    None of which is working. I do know the Addcontrols has to go on the main form. As for the rest, I am so lost
    Monday, April 17, 2017 9:03 PM

All replies

  • Hi

    Don't know if this is of any help, but it seems to work here.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim b As New ctrlAddSitesMenu
        End Sub
    
        Public Class ctrlAddSitesMenu
    
            'Declares Buttons for Opening Websites
            Dim OpenTraker As New Button
            Dim OpenDynamics As New Button
            Dim OpenInetPortal As New Button
            Dim OpenFrontPortal As New Button
            Dim OpenLoopcare As New Button
            Dim OpenViryNet As New Button
            Dim OpenSmpl As New Button
            Dim OpenFosWiki As New Button
            Dim OpenLola As New Button
    
            'Declare Button Constant Varibles
            Dim LocX As Integer = 0
            Dim LocY As Integer = 20
            Dim Row As Integer = 25
            Dim H As Integer = 25
            Dim W As Integer = 115
            Public Sub New()
                LoadButtons()
            End Sub
            Public Sub LoadButtons()
    
                'Declares attributes for Invidual buttons
                'Declares Button names
                OpenTraker.Name = "btnOpenTracker"
                OpenDynamics.Name = "btnOpenDynamics"
                OpenInetPortal.Name = "btnInetPortal"
                OpenFrontPortal.Name = "btnFrontPortal"
                OpenLoopcare.Name = "btnOpenLoopcare"
                OpenViryNet.Name = "btnOpenVirynet"
                OpenSmpl.Name = "btnOpenSimple"
                OpenFosWiki.Name = "btnOpenFosWiki"
                OpenLola.Name = "btnOpenLola"
    
                'Declares Button text
                OpenTraker.Text = "Tracker"
                OpenDynamics.Text = "Dynamics"
                OpenInetPortal.Text = "Internet Portal"
                OpenFrontPortal.Text = "Frontier Portal"
                OpenLoopcare.Text = "Loopcare"
                OpenViryNet.Text = "Virynet"
                OpenSmpl.Text = "SIMPL"
                OpenFosWiki.Text = "FosWiki"
                OpenLola.Text = "Lola"
    
                'Declares Button Size
                OpenTraker.Size = New Size(W, H)
                OpenDynamics.Size = New Size(W, H)
                OpenInetPortal.Size = New Size(W, H)
                OpenFrontPortal.Size = New Size(W, H)
                OpenLoopcare.Size = New Size(W, H)
                OpenViryNet.Size = New Size(W, H)
                OpenSmpl.Size = New Size(W, H)
                OpenFosWiki.Size = New Size(W, H)
                OpenLola.Size = New Size(W, H)
    
                ' Declares Button Location
                OpenTraker.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenDynamics.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenInetPortal.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenFrontPortal.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenLoopcare.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenViryNet.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenSmpl.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenFosWiki.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
                OpenLola.Location = New System.Drawing.Point(LocX, LocY)
                LocY += Row
    
    
                'Adds Buttons to Menu
                Form1.Controls.Add(OpenTraker)
                Form1.Controls.Add(OpenDynamics)
                Form1.Controls.Add(OpenInetPortal)
                Form1.Controls.Add(OpenFrontPortal)
                Form1.Controls.Add(OpenLoopcare)
                Form1.Controls.Add(OpenViryNet)
                Form1.Controls.Add(OpenSmpl)
                Form1.Controls.Add(OpenFosWiki)
                Form1.Controls.Add(OpenLola)
    
    
                'Adds Button Click Handlers
                AddHandler OpenTraker.Click, AddressOf OpenTrackerClick
                'AddHandler OpenDynamics.Click, AddressOf OpenDynamicsClick
                'AddHandler OpenInetPortal.Click, AddressOf OpenInetPortalClick
                'AddHandler OpenFrontPortal.Click, AddressOf OpenFrontPortalClick
                'AddHandler OpenLoopcare.Click, AddressOf OpenLoopcareClick
                'AddHandler OpenViryNet.Click, AddressOf OpenViryNetClick
                'AddHandler OpenSmpl.Click, AddressOf OpenSmplClick
                'AddHandler OpenFosWiki.Click, AddressOf OpenFosWikiClick
                'AddHandler OpenLola.Click, AddressOf OpenLolaClick
    
            End Sub
            Private Sub OpenTrackerClick(ByVal sender As Object, ByVal e As EventArgs)
                MessageBox.Show("OpenTracker Button Clicked")
            End Sub
    
        End Class
    End Class


    Regards Les, Livingston, Scotland

    Monday, April 17, 2017 9:22 PM
  • Les, Thanks for the Reply, 

    I think you misunderstood what I was asking. What I had works fine. What I want to do is move

    Public Class ctrlAddSitesMenu
    
        'Declares Buttons for Opening Websites
        Dim OpenTraker As New Button
        Dim OpenDynamics As New Button
        Dim OpenInetPortal As New Button
        Dim OpenFrontPortal As New Button
        Dim OpenLoopcare As New Button
        Dim OpenViryNet As New Button
        Dim OpenSmpl As New Button
        Dim OpenFosWiki As New Button
        Dim OpenLola As New Button
    
        'Declare Button Constant Varibles
        Dim LocX As Integer = 0
        Dim LocY As Integer = 20
        Dim Row As Integer = 25
        Dim H As Integer = 25
        Dim W As Integer = 115
    
        Public Sub LoadButtons()
    
            'Declares attributes for Invidual buttons
            'Declares Button names
            OpenTraker.Name = "btnOpenTracker"
            OpenDynamics.Name = "btnOpenDynamics"
            OpenInetPortal.Name = "btnInetPortal"
            OpenFrontPortal.Name = "btnFrontPortal"
            OpenLoopcare.Name = "btnOpenLoopcare"
            OpenViryNet.Name = "btnOpenVirynet"
            OpenSmpl.Name = "btnOpenSimple"
            OpenFosWiki.Name = "btnOpenFosWiki"
            OpenLola.Name = "btnOpenLola"
    
            'Declares Button text
            OpenTraker.Text = "Tracker"
            OpenDynamics.Text = "Dynamics"
            OpenInetPortal.Text = "Internet Portal"
            OpenFrontPortal.Text = "Frontier Portal"
            OpenLoopcare.Text = "Loopcare"
            OpenViryNet.Text = "Virynet"
            OpenSmpl.Text = "SIMPL"
            OpenFosWiki.Text = "FosWiki"
            OpenLola.Text = "Lola"
    
            'Declares Button Size
            OpenTraker.Size = New Size(W, H)
            OpenDynamics.Size = New Size(W, H)
            OpenInetPortal.Size = New Size(W, H)
            OpenFrontPortal.Size = New Size(W, H)
            OpenLoopcare.Size = New Size(W, H)
            OpenViryNet.Size = New Size(W, H)
            OpenSmpl.Size = New Size(W, H)
            OpenFosWiki.Size = New Size(W, H)
            OpenLola.Size = New Size(W, H)
    
            ' Declares Button Location
            OpenTraker.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenDynamics.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenInetPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFrontPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLoopcare.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenViryNet.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenSmpl.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFosWiki.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLola.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
    
    
            'Adds Buttons to Menu
            Me.Controls.Add(OpenTraker)
            Me.Controls.Add(OpenDynamics)
            Me.Controls.Add(OpenInetPortal)
            Me.Controls.Add(OpenFrontPortal)
            Me.Controls.Add(OpenLoopcare)
            Me.Controls.Add(OpenViryNet)
            Me.Controls.Add(OpenSmpl)
            Me.Controls.Add(OpenFosWiki)
            Me.Controls.Add(OpenLola)
    
    
            'Adds Button Click Handlers
            AddHandler OpenTraker.Click, AddressOf OpenTrackerClick
            AddHandler OpenDynamics.Click, AddressOf OpenDynamicsClick
            AddHandler OpenInetPortal.Click, AddressOf OpenInetPortalClick
            AddHandler OpenFrontPortal.Click, AddressOf OpenFrontPortalClick
            AddHandler OpenLoopcare.Click, AddressOf OpenLoopcareClick
            AddHandler OpenViryNet.Click, AddressOf OpenViryNetClick
            AddHandler OpenSmpl.Click, AddressOf OpenSmplClick
            AddHandler OpenFosWiki.Click, AddressOf OpenFosWikiClick
            AddHandler OpenLola.Click, AddressOf OpenLolaClick
    
        End Sub
    To a separate file and call it from the original one. 

    Monday, April 17, 2017 9:29 PM
  • Hi

    OK, sorry that was off track.

    I don't think I can help.

    Regards Les, Livingston, Scotland

    Monday, April 17, 2017 9:48 PM
  • IN order to clean thing up a bit, I want to move The "LoadButtons" Class to a different file, I assume it would be a class file.

    Do you mean that you want to put ctrlSitesMenu in a different file (there is no LoadButtons class)? 

    I would recommend you start over. Use a CustomControl template (within Components) as the template for your class.  Add all the above code except for the load event (which doesn't do anything) and call LoadButtons from the class constructor. It will end up like this:

    Public Class CustomControl1
    
        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            LoadButtons()
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            'Add your custom paint code here
        End Sub
    
        'Declares Buttons for Opening Websites
        Dim OpenTraker As New Button
        Dim OpenDynamics As New Button
        Dim OpenInetPortal As New Button
        Dim OpenFrontPortal As New Button
        ' etc

    Monday, April 17, 2017 10:13 PM
  • Hello,

    Here I created a class project which contains a slightly modified version of your code above along with a forms project for testing.

    Note, you don't need a separate class project but this allows if needed the use of the class in other projects.

    Important: LoadButtons(ByVal sender As Control), sender is the control to place the buttons on. In this case I placed them on the form but you can place them say on a panel for instance.

    Here is your modified class

    Imports System.Drawing
    Imports System.Windows.Forms
    
    Public Class ctrlAddSitesMenu
        'Declares Buttons for Opening Websites
        Public OpenTraker As New Button
        Public OpenDynamics As New Button
        Public OpenInetPortal As New Button
        Public OpenFrontPortal As New Button
        Public OpenLoopcare As New Button
        Public OpenViryNet As New Button
        Public OpenSmpl As New Button
        Public OpenFosWiki As New Button
        Public OpenLola As New Button
    
        'Declare Button Constant Varibles
        Dim LocX As Integer = 0
        Dim LocY As Integer = 20
        Dim Row As Integer = 25
        Dim H As Integer = 25
        Dim W As Integer = 115
    
        Public Sub LoadButtons(ByVal sender As Control)
    
            'Declares attributes for Invidual buttons
            'Declares Button names
            OpenTraker.Name = "btnOpenTracker"
            OpenDynamics.Name = "btnOpenDynamics"
            OpenInetPortal.Name = "btnInetPortal"
            OpenFrontPortal.Name = "btnFrontPortal"
            OpenLoopcare.Name = "btnOpenLoopcare"
            OpenViryNet.Name = "btnOpenVirynet"
            OpenSmpl.Name = "btnOpenSimple"
            OpenFosWiki.Name = "btnOpenFosWiki"
            OpenLola.Name = "btnOpenLola"
    
            'Declares Button text
            OpenTraker.Text = "Tracker"
            OpenDynamics.Text = "Dynamics"
            OpenInetPortal.Text = "Internet Portal"
            OpenFrontPortal.Text = "Frontier Portal"
            OpenLoopcare.Text = "Loopcare"
            OpenViryNet.Text = "Virynet"
            OpenSmpl.Text = "SIMPL"
            OpenFosWiki.Text = "FosWiki"
            OpenLola.Text = "Lola"
    
            'Declares Button Size
            OpenTraker.Size = New Size(W, H)
            OpenDynamics.Size = New Size(W, H)
            OpenInetPortal.Size = New Size(W, H)
            OpenFrontPortal.Size = New Size(W, H)
            OpenLoopcare.Size = New Size(W, H)
            OpenViryNet.Size = New Size(W, H)
            OpenSmpl.Size = New Size(W, H)
            OpenFosWiki.Size = New Size(W, H)
            OpenLola.Size = New Size(W, H)
    
            ' Declares Button Location
            OpenTraker.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenDynamics.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenInetPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFrontPortal.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLoopcare.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenViryNet.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenSmpl.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenFosWiki.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
            OpenLola.Location = New System.Drawing.Point(LocX, LocY)
            LocY += Row
    
    
            'Adds Buttons to Menu
            sender.Controls.Add(OpenTraker)
            sender.Controls.Add(OpenDynamics)
            sender.Controls.Add(OpenInetPortal)
            sender.Controls.Add(OpenFrontPortal)
            sender.Controls.Add(OpenLoopcare)
            sender.Controls.Add(OpenViryNet)
            sender.Controls.Add(OpenSmpl)
            sender.Controls.Add(OpenFosWiki)
            sender.Controls.Add(OpenLola)
    
    
            'Adds Button Click Handlers
    
    
        End Sub
    
    End Class
    

    In the form project I added a reference to the class project. Then in the form use an Import to reference the class.

    Imports MyControls
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ops As New ctrlAddSitesMenu
            ops.LoadButtons(Me)
            AddHandler ops.OpenTraker.Click, AddressOf OpenTrackerClick
            AddHandler ops.OpenDynamics.Click, AddressOf OpenDynamicsClick
            AddHandler ops.OpenInetPortal.Click, AddressOf OpenInetPortalClick
            AddHandler ops.OpenFrontPortal.Click, AddressOf OpenFrontPortalClick
            AddHandler ops.OpenLoopcare.Click, AddressOf OpenLoopcareClick
            AddHandler ops.OpenViryNet.Click, AddressOf OpenViryNetClick
            AddHandler ops.OpenSmpl.Click, AddressOf OpenSmplClick
            AddHandler ops.OpenFosWiki.Click, AddressOf OpenFosWikiClick
            AddHandler ops.OpenLola.Click, AddressOf OpenLolaClick
        End Sub
    
        Private Sub OpenLolaClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenFosWikiClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenSmplClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenViryNetClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenLoopcareClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenFrontPortalClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenInetPortalClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenDynamicsClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    
        Private Sub OpenTrackerClick(sender As Object, e As EventArgs)
            MessageBox.Show(CType(sender, Button).Text)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, April 17, 2017 10:43 PM
    Moderator
  • What I want to do is move to a separate file and call it from the original one. 

    Do you mean that you want to move the method LoadButtons to a separate file?

    What are you trying to achieve by doing that?  I suspect that it is the wrong solution to the problem.

    A method could go in a module or in a class library. But code that depends on other code needs to be able to reference that other code.  In this case it will need a reference to the class instance to which the buttons need to be added.  So the method signature will require a parameter that is the type of the class, and the method call will need an argument that is the instance to which the buttons are to be added. Then you need to choose between a module and a library, but for that you need to decide why this code should be in a separate file.

    Monday, April 17, 2017 10:52 PM