Any way to access Parent Form From a class?
-
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 SubIs there any alternative without passing Parent Form as Argument to Invoke Parent's Thread?
-
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 SubIs there any alternative without passing Parent Form as Argument to Invoke Parent's Thread?
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.
-
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.
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.parentSub 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 -
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.parentSub 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 ClassYou 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.