Results 1 to 9 of 9

Thread: Accessing class in class library

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2021
    Posts
    4

    Accessing class in class library

    I am trying to create a class for dynamic navigation buttons. I don't seem to get it work. At the moment I am not getting any errors, luckily, but I don't see my button on the form either. Can you help me get back on track and understand what went wrong?

    Form:

    Code:
    Public Class Form2
    
        Public Sub GetNavigationbuttons()
            Dim GetNavigationbuttons As NavigationButtons() = New NavigationButtons() {}
        End Sub
    
            Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Me.BackColor = Color.FromArgb(56, 67, 87)
                MaximizeBox = False
                MinimizeBox = True
                dashboard.BackColor = Color.FromArgb(65, 77, 99)
                panel_customerinfo.Visible = False
            GetNavigationbuttons()
        End Sub
        End Class
    And the class library:

    Code:
    Public Class NavigationButtons
    
        Inherits Control.ControlCollection
        Protected Sub New(owner)
            MyBase.New(owner)
        End Sub
    
        Friend Class Form1
    
        End Class
    
        Public Property GetNavigationbuttons As Object
    
        Public Sub CreateNext(ByVal e As System.EventArgs)
    
            ' Create a Button object 
            Dim NextButton As New Button
            ' Set Button properties
            NextButton.Location = New Point(206, 745)
            NextButton.Height = 57
            NextButton.Width = 64
            NextButton.Text = ""
            NextButton.Name = "btn_next"
            NextButton.BackColor = Color.Transparent
            NextButton.ForeColor = Color.Transparent
            NextButton.BackgroundImage = My.Resources.next_disabled
            NextButton.BackgroundImageLayout = ImageLayout.Stretch
            AddHandler NextButton.Click,
            GetNavigationbuttons.Add(NextButton)
            AddHandler NextButton.MouseEnter, Sub(sender, eventargs)
                                                  NextButton.BackgroundImage = My.Resources.next_enabled
                                              End Sub
        End Sub
    
    End Class

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2021
    Posts
    4

    Re: Accessing class in class library

    I went little bit further and thought why not create a new panel as well with the navigation buttons. I've tried to search how to do so, and this is how far I got this morning. I don't have an idea how to call for the code in the form.

    Code:
    Public Class NewUIPanel
    
        Inherits Control.ControlCollection
        Protected Sub New(owner)
            MyBase.New(owner)
        End Sub
    
        Dim PanelName As String
    
        Private Sub CreateUIPanel()
    
            Dim UIPanel As New Panel
    
            UIPanel.Width = 1378
            UIPanel.Height = 805
            UIPanel.Location = New Point(206, 0)
            UIPanel.BackColor = Color.FromArgb(56, 67, 87)
            UIPanel.Name = "UIPanel" & PanelName
    
            Me.Add(UIPanel)
    
        End Sub
    
        Public Class Navigationbuttons
            Private Property create As Object
    
            Private Sub CreateNext()
    
                ' Create a Button object 
                Dim NextButton As New Button
                ' Set Button properties
                NextButton.Width = 64
                NextButton.Height = 57
                NextButton.Location = New Point(0, 748)
                NextButton.Text = ""
                NextButton.Name = "btn_next"
                NextButton.BackColor = Color.Transparent
                NextButton.ForeColor = Color.Transparent
                NextButton.BackgroundImage = My.Resources.next_disabled
                NextButton.BackgroundImageLayout = ImageLayout.Stretch
                AddHandler NextButton.Click,
                create.Add(NextButton)
                AddHandler NextButton.MouseEnter, Sub(sender, eventargs)
                                                      NextButton.BackgroundImage = My.Resources.next_enabled
                                                  End Sub
            End Sub
    
            Private Sub CreatePrevious()
    
                ' Create a Button object 
                Dim PreviousButton As New Button
                ' Set Button properties
                PreviousButton.Width = 64
                PreviousButton.Height = 57
                PreviousButton.Location = New Point(1314, 748)
                PreviousButton.Text = ""
                PreviousButton.Name = "btn_previous"
                PreviousButton.BackColor = Color.Transparent
                PreviousButton.ForeColor = Color.Transparent
                PreviousButton.BackgroundImage = My.Resources.previous_disabled
                PreviousButton.BackgroundImageLayout = ImageLayout.Stretch
                AddHandler PreviousButton.Click,
                create.Add(PreviousButton)
                AddHandler PreviousButton.MouseEnter, Sub(sender, eventargs)
                                                          PreviousButton.BackgroundImage = My.Resources.previous_enabled
                                                      End Sub
            End Sub
    
        End Class
    
    End Class
    Code above is in a class library called WinFormsLibrary1 which I have a reference for in my form properties. Also
    Code:
    Imports WinFormsLibrary1
    is included. Now I would like to know, how I can create the panel from the class library on my form with a btn click.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Accessing class in class library

    Why are you inheriting Control.ControlCollection? You need to inherit Control, either directly or indirectly via some existing control. Control.ControlCollection is the type that a control's Controls property is, which is a collection containing the child controls. If you want a custom control that contains children then you inherit Control and then add those children to the Controls collection property.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: Accessing class in class library

    You need to turn Option Strict ON. By leaving it off, it is allowing you to be too fuzzy in your thinking about what you're doing, which is causing you some minor troubles. Eventually, those troubles will likely grow.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2021
    Posts
    4

    Re: Accessing class in class library

    I modified the code, but still cannot get it to work.

    Class library:

    Code:
    Public Class Navigationbuttons
    
        Inherits Button
        Private Sub CreateNextbutton()
    
            ' Create a Button object 
            Dim NextButton As New Button
            ' Set Button properties
            NextButton.Width = 64
            NextButton.Height = 57
            NextButton.Location = New Point(0, 748)
            NextButton.Text = ""
            NextButton.Name = "btn_next"
            NextButton.BackColor = Color.Transparent
            NextButton.ForeColor = Color.Transparent
            NextButton.BackgroundImage = My.Resources.next_disabled
            NextButton.BackgroundImageLayout = ImageLayout.Stretch
            AddHandler NextButton.Click, AddressOf Me.CreateNextbutton
            Controls.Add(NextButton)
            AddHandler NextButton.MouseEnter, Sub(sender, eventargs)
                                                  NextButton.BackgroundImage = My.Resources.next_enabled
                                              End Sub
            CreateNextbutton()
    
        End Sub
    
        Private Sub CreatePreviousbutton()
    
            ' Create a Button object 
            Dim PreviousButton As New Button
            ' Set Button properties
            PreviousButton.Width = 64
            PreviousButton.Height = 57
            PreviousButton.Location = New Point(1314, 748)
            PreviousButton.Text = ""
            PreviousButton.Name = "btn_previous"
            PreviousButton.BackColor = Color.Transparent
            PreviousButton.ForeColor = Color.Transparent
            PreviousButton.BackgroundImage = My.Resources.previous_disabled
            PreviousButton.BackgroundImageLayout = ImageLayout.Stretch
            AddHandler PreviousButton.Click, AddressOf Me.CreatePreviousbutton
            Controls.Add(PreviousButton)
            AddHandler PreviousButton.MouseEnter, Sub(sender, eventargs)
                                                      PreviousButton.BackgroundImage = My.Resources.previous_enabled
                                                  End Sub
            CreatePreviousbutton()
    
        End Sub
    
    End Class
    Form:

    Code:
    Public Class Form2
    
        Private Sub CreateNavigationbuttons()
    
            Dim NewNavigationbuttons As New Navigationbuttons
    
        End Sub
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Me.BackColor = Color.FromArgb(56, 67, 87)
            MaximizeBox = False
            MinimizeBox = True
            dashboard.BackColor = Color.FromArgb(65, 77, 99)
            CreateNavigationbuttons()
    
        End Sub
    End Class
    Last edited by vaemps; Aug 29th, 2021 at 04:12 AM.

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,606

    Re: Accessing class in class library

    I'm baffled by up down left rights of the code.
    I would also like to know from the Gurus here if that is a proper way to create a control.
    Personally I wouldn't have inherited a control if I'm not really doing anything special to it, I would just created buttons and put them to a list - collection in order to use them.
    Most importantly I would at some point added them to the form I want them to show.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2021
    Posts
    4

    Re: Accessing class in class library

    Quote Originally Posted by sapator View Post
    I'm baffled by up down left rights of the code.
    I would also like to know from the Gurus here if that is a proper way to create a control.
    Personally I wouldn't have inherited a control if I'm not really doing anything special to it, I would just created buttons and put them to a list - collection in order to use them.
    Most importantly I would at some point added them to the form I want them to show.
    I have just recently started coding so I am quite novice. What I want is to create a user interface panel layout with navigation buttons (next and previous) that I can use at any point and form necessary in my project so it will be the same anywhere I use it. I assume creating a class library is not the way to go..?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: Accessing class in class library

    Well, it is A way to go, but probably not THE way to go. Take a look at this link:

    https://www.thoughtco.com/user-contr...-vbnet-3424337

    The first paragraph sounds a whole lot like what it is you are trying to accomplish.
    My usual boring signature: Nothing

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Accessing class in class library

    An alternative is to create a new class which inherits BindingNavigator, create buttons there, setup properties, assign images then in your form setup events and conditions such as enabling and disabling buttons. This gets you the next and Previous buttons to display,

    Not that we could go farther with this while I see no reason as if you look at WireUpButton in BindingNavigator source and the code for assertion in properties for movement it's not worth it.

    See the following for documentation done in C# with instructions to use with VB.NET. Images are in the link above.

    For VB.NET this is untested, just did a quick conversion from C#
    Code:
    Public Class DataGridToolStrip
    	Inherits BindingNavigator
    
    	Public Property PreviousButton() As ToolStripButton
    	Public Property NextButton() As ToolStripButton
    	Public Property EditButton() As ToolStripButton
    
    	Public Property Separator() As ToolStripSeparator
    
    	Public Overrides Sub AddStandardItems()
    		'base.AddStandardItems();
    
    		PreviousButton = New ToolStripButton With {
    			.Name = "PreviousButton",
    			.Image = Resources.ASX_Previous_blue_16x,
    			.Enabled = False
    		}
    		NextButton = New ToolStripButton With {
    			.Name = "NextButton",
    			.Image = Resources.ASX_Next_blue_16x
    		}
    		Separator = New ToolStripSeparator()
    		EditButton = New ToolStripButton With {
    			.Name = "EditButton",
    			.Image = Resources.ASX_Edit_blue_16x
    		}
    
    		Items.AddRange(New ToolStripItem() {PreviousButton, NextButton, Separator, EditButton })
    
    	End Sub
    	
    End Cl

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