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. Any way to access Parent Form From a class?

Any way to access Parent Form From a class?

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 3 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.
  • S Offline
    S Offline
    slam Iqbal
    wrote on last edited by
    #1

    Purpose is to Invoke The Parent from a class. From 'User Control' Parent/Paren form can be accessed.

        'Me.Parent 'OR
        'Me.ParentForm
        'Me.Parent.Invoke(......)
    

    But I want to do above from a class. I did using following code:

    Private Parent As System.Windows.Forms.Control

    Public Sub New (ByVal Parent As System.Windows.Forms.Control)
    'Any way to access Parent Form From a class without passing Parent as Argument?'
    Me.Parent = Parent
    End Sub

    Is there any alternative without passing Parent Form as Argument to Invoke Parent's Thread?

    L 1 Reply Last reply
    0
    • S slam Iqbal

      Purpose is to Invoke The Parent from a class. From 'User Control' Parent/Paren form can be accessed.

          'Me.Parent 'OR
          'Me.ParentForm
          'Me.Parent.Invoke(......)
      

      But I want to do above from a class. I did using following code:

      Private Parent As System.Windows.Forms.Control

      Public Sub New (ByVal Parent As System.Windows.Forms.Control)
      'Any way to access Parent Form From a class without passing Parent as Argument?'
      Me.Parent = Parent
      End Sub

      Is there any alternative without passing Parent Form as Argument to Invoke Parent's Thread?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, each WinForms Control has a Parent property. It gets set when the Control is added to its parent's Controls collection, which cannot happen until after its constructor finishes. Hence, no you can't predict its future parent inside its constructor, but you will get it for free later on. If the Controls get added in Visual Designer, their parent will have been set at the time your Form loads (use the Form.Load event). If you don't know when the parent is going to be set (e.g. because all things happen dynamically), then you could use the Control.ParentChanged event to your advantage. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, each WinForms Control has a Parent property. It gets set when the Control is added to its parent's Controls collection, which cannot happen until after its constructor finishes. Hence, no you can't predict its future parent inside its constructor, but you will get it for free later on. If the Controls get added in Visual Designer, their parent will have been set at the time your Form loads (use the Form.Load event). If you don't know when the parent is going to be set (e.g. because all things happen dynamically), then you could use the Control.ParentChanged event to your advantage. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        S Offline
        S Offline
        slam Iqbal
        wrote on last edited by
        #3

        Plz read the following Example. You are right only for ctrl1 Class. But my Question was about Class1. Is there any way to access Parent from Class1?

        'Form1
        Public Class Form1
        Inherits Form
        End Class

        'Class1
        Public Class Class1
        'here I want to access form1. not using frm1.member and not usinig dim ff as new form1, ff.member
        'I just want to access that form using me.parent

        Sub ParentAccess()
            Me.Parent.Text = "It's a compile time Error"
            'Error:- Parent is not a member of Class1
        End Sub
        

        End Class

        'User Control1
        Public Class ctrl1
        Inherits ComboBox
        Sub ParentAccess()
        Me.Parent.Text = "Very easy to access but my class1 don't have the statment like Inherits ComboBox"
        'No Error:
        End Sub
        End Class

        D 1 Reply Last reply
        0
        • S slam Iqbal

          Plz read the following Example. You are right only for ctrl1 Class. But my Question was about Class1. Is there any way to access Parent from Class1?

          'Form1
          Public Class Form1
          Inherits Form
          End Class

          'Class1
          Public Class Class1
          'here I want to access form1. not using frm1.member and not usinig dim ff as new form1, ff.member
          'I just want to access that form using me.parent

          Sub ParentAccess()
              Me.Parent.Text = "It's a compile time Error"
              'Error:- Parent is not a member of Class1
          End Sub
          

          End Class

          'User Control1
          Public Class ctrl1
          Inherits ComboBox
          Sub ParentAccess()
          Me.Parent.Text = "Very easy to access but my class1 don't have the statment like Inherits ComboBox"
          'No Error:
          End Sub
          End Class

          D Offline
          D Offline
          darkelv
          wrote on last edited by
          #4

          You can if you set a reference to the form in Class1 but that is a very bad design, unless Class1 is strictly meant as a helper class for that form. Class1 is not an inherited Control class like ctrl1, so it doesn't have Parent property. You will have to define the Control type property and set the property accordingly. Beside you won't be able to access to the combobox of the form unless you set it to public, which is another bad design.

          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