Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding a control from within a class

Adding a control from within a class

Scheduled Pinned Locked Moved Visual Basic
questiondockertutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Hasler
    wrote on last edited by
    #1

    This is a bit of a Newbie question... :-O I am trying to write a class which adds a control to it's host, where the host could be any suitable container such as a Panel, GroupBox, or a Form. I know I can add a control directly to a Form for example in the following way:

    Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)\_
    

    Handles MyBase.Load
    Dim myControl As New SomeControl

        With myControl
            .Left = myLeft
            .Top = myTop
            .Width = myWidth
            .Height = myHeight
            .Visible = True
        End With
    
        Me.Controls.Add(myControl)
    
    End Sub
    

    But I'm having trouble doing it from within a class.

    Public Class MyNewClass
     .
     .
     .
     Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,\_
    

    ByVal newHeight As Short)
    myLeft = newLeft
    myRight = newRight
    myWidth = newWidth
    myHeight = newHeight

        Dim myControl As New _SomeControl_
    
        With myControl
            .Left = myLeft
            .Top = myTop
            .Width = myWidth
            .Height = myHeight
            .Visible = True
        End With
    
        Me.Controls.Add(myControl)
    
     End Sub
    

    Clearly "Me" now refers to the instance of MyNewClass and not the Container or Form in which it is intended to go, so it's not going to work. :( How should I do it? Thanks in advance.

    J D P 3 Replies Last reply
    0
    • P Paul Hasler

      This is a bit of a Newbie question... :-O I am trying to write a class which adds a control to it's host, where the host could be any suitable container such as a Panel, GroupBox, or a Form. I know I can add a control directly to a Form for example in the following way:

      Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)\_
      

      Handles MyBase.Load
      Dim myControl As New SomeControl

          With myControl
              .Left = myLeft
              .Top = myTop
              .Width = myWidth
              .Height = myHeight
              .Visible = True
          End With
      
          Me.Controls.Add(myControl)
      
      End Sub
      

      But I'm having trouble doing it from within a class.

      Public Class MyNewClass
       .
       .
       .
       Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,\_
      

      ByVal newHeight As Short)
      myLeft = newLeft
      myRight = newRight
      myWidth = newWidth
      myHeight = newHeight

          Dim myControl As New _SomeControl_
      
          With myControl
              .Left = myLeft
              .Top = myTop
              .Width = myWidth
              .Height = myHeight
              .Visible = True
          End With
      
          Me.Controls.Add(myControl)
      
       End Sub
      

      Clearly "Me" now refers to the instance of MyNewClass and not the Container or Form in which it is intended to go, so it's not going to work. :( How should I do it? Thanks in advance.

      J Offline
      J Offline
      JoeSharp
      wrote on last edited by
      #2

      hi pass a reference of the form to your class. Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,_ ByVal newHeight As Short, ByVal form As Form1) regards

      J 1 Reply Last reply
      0
      • J JoeSharp

        hi pass a reference of the form to your class. Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,_ ByVal newHeight As Short, ByVal form As Form1) regards

        J Offline
        J Offline
        Jon_Boy
        wrote on last edited by
        #3

        Your suggestion would work, but IMO the form should be adding the control not vice-versa.

        Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

        1 Reply Last reply
        0
        • P Paul Hasler

          This is a bit of a Newbie question... :-O I am trying to write a class which adds a control to it's host, where the host could be any suitable container such as a Panel, GroupBox, or a Form. I know I can add a control directly to a Form for example in the following way:

          Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)\_
          

          Handles MyBase.Load
          Dim myControl As New SomeControl

              With myControl
                  .Left = myLeft
                  .Top = myTop
                  .Width = myWidth
                  .Height = myHeight
                  .Visible = True
              End With
          
              Me.Controls.Add(myControl)
          
          End Sub
          

          But I'm having trouble doing it from within a class.

          Public Class MyNewClass
           .
           .
           .
           Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,\_
          

          ByVal newHeight As Short)
          myLeft = newLeft
          myRight = newRight
          myWidth = newWidth
          myHeight = newHeight

              Dim myControl As New _SomeControl_
          
              With myControl
                  .Left = myLeft
                  .Top = myTop
                  .Width = myWidth
                  .Height = myHeight
                  .Visible = True
              End With
          
              Me.Controls.Add(myControl)
          
           End Sub
          

          Clearly "Me" now refers to the instance of MyNewClass and not the Container or Form in which it is intended to go, so it's not going to work. :( How should I do it? Thanks in advance.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          It's bad form to do what you're attempting. The child should know nothing of it's parent, nor should it modify the environment of the parent. The parent tells the child what to do, not the other way around. Normally, the parent container would add the child controls to the form, with the child having implemented a common interface that the parent expects it to.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          1 Reply Last reply
          0
          • P Paul Hasler

            This is a bit of a Newbie question... :-O I am trying to write a class which adds a control to it's host, where the host could be any suitable container such as a Panel, GroupBox, or a Form. I know I can add a control directly to a Form for example in the following way:

            Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)\_
            

            Handles MyBase.Load
            Dim myControl As New SomeControl

                With myControl
                    .Left = myLeft
                    .Top = myTop
                    .Width = myWidth
                    .Height = myHeight
                    .Visible = True
                End With
            
                Me.Controls.Add(myControl)
            
            End Sub
            

            But I'm having trouble doing it from within a class.

            Public Class MyNewClass
             .
             .
             .
             Public Sub New(ByVal newLeft As Short, ByVal newRight As Short, ByVal newWidth As Short,\_
            

            ByVal newHeight As Short)
            myLeft = newLeft
            myRight = newRight
            myWidth = newWidth
            myHeight = newHeight

                Dim myControl As New _SomeControl_
            
                With myControl
                    .Left = myLeft
                    .Top = myTop
                    .Width = myWidth
                    .Height = myHeight
                    .Visible = True
                End With
            
                Me.Controls.Add(myControl)
            
             End Sub
            

            Clearly "Me" now refers to the instance of MyNewClass and not the Container or Form in which it is intended to go, so it's not going to work. :( How should I do it? Thanks in advance.

            P Offline
            P Offline
            Paul Hasler
            wrote on last edited by
            #5

            Thanks for your comments. It got me thinking about my whole approach. :) With a little more reading I realised what I'm actually trying to do is create a User Control. I can stick all the controls I want in it, and handle events from the "sender". Problem is now solved and working fine. :-D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups