Adding a control from within a class
-
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 SomeControlWith 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 = newHeightDim 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.
-
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 SomeControlWith 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 = newHeightDim 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.
-
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
-
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 SomeControlWith 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 = newHeightDim 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.
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 -
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 SomeControlWith 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 = newHeightDim 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.
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