Click to See Complete Forum and Search --> : [RESOLVED] [2005] Panel container on UserControl
HanneSThEGreaT
July 4th, 2008, 03:27 AM
Hello everyone.
I have a usercontrol, and on the usercontrol I have a Panel.
What I want to do is to make the Panel the host for child controls, In other words, I want to add ( in design View ) controls to the panel on the usercontrol.
I did this in my layout event:
Private Sub HTG_Group_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
Try
If e.AffectedControl.Name <> Me.Name And e.AffectedControl.Name <> pnlGBBody.Name And e.AffectedControl.Name <> lblGBHead.Name Then
Select Case e.AffectedControl.Top
Case pnlGBBody.Top To pnlGBBody.Top + pnlGBBody.Height
Select Case e.AffectedControl.Left
Case pnlGBBody.Left To pnlGBBody.Left + pnlGBBody.Width
If e.AffectedProperty.ToString() = "Bounds" Then
'set the parent
e.AffectedControl.Parent = pnlGBBody
pnlGBBody.Controls.Add(e.AffectedControl)
'calculate the top & left to drop it where the user intended
e.AffectedControl.Top = e.AffectedControl.Top - pnlGBBody.Top
e.AffectedControl.Left = e.AffectedControl.Left - pnlGBBody.Left
e.AffectedControl.PerformLayout()
End If
End Select
End Select
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Yes, it does add the controls to the panel, but, the moment I move any of the child controls, I get an error that the control I've added is disabled because there is no reference set to an instance of an object.
How do I catch it, how can I get over it ¿
Any help will be greatly appreciated.
GremlinSA
July 4th, 2008, 05:11 AM
I'd say that there is code missing from the designer class of the form ...
check the designer code to see that there is full reference to each child control Friend WithEvents Button2 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.Button2 = New System.Windows.Forms.Button
Me.UserControl.Controls.Add(Me.Button2)
'
'Button2
'
Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Button2.Location = New System.Drawing.Point(6, 194)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(91, 28)
Me.Button2.TabIndex = 6
Me.Button2.Text = "Add"
Me.Button2.UseVisualStyleBackColor = True Above is just an example of all the Designer code required to add it to a object already on the form ...
Also your Usercontrol will need simular code in the designer..
Gremmy...
HanneSThEGreaT
July 4th, 2008, 05:24 AM
Thanx Gremmy, I get what you're saying - the child control needs to be initialised.
Problem is, when I drag a button ( for example ) from the Toolbox in Design view, onto the usercontrol, it works, but the moment I move the button ( still in design view ), it gives me that error.
Doesn't make sense to my thick head :p
GremlinSA
July 4th, 2008, 05:41 AM
One thing to remember, Designer code is called such, because it does run at design time. unfortunately you cannot trap it..
I'd check in your usercontrol how you are handeling the 'Controls.Add()' bit, i'm taking a guess that the problem may be there somewhere, and my guess is that your missing that one vital keyword , "NEW" ... ;) ..
Gremmy...
HanneSThEGreaT
July 4th, 2008, 06:11 AM
I am such an idiot!
Somebody please just come and knock some sense into me LOL!
Gremmy, this is what I did:
Private Sub HTG_Group_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
Try
Dim i As New Control
i = e.AffectedControl
If i.Name <> Me.Name And i.Name <> pnlGBBody.Name And i.Name <> lblGBHead.Name Then
Select Case i.Top
Case pnlGBBody.Top To pnlGBBody.Top + pnlGBBody.Height
Select Case i.Left
Case pnlGBBody.Left To pnlGBBody.Left + pnlGBBody.Width
If i.ToString() = "Bounds" Then
'set the parent
i.Parent = pnlGBBody
pnlGBBody.Controls.Add(i)
'calculate the top & left to drop it where the user intended
i.Top = e.AffectedControl.Top - pnlGBBody.Top
i.Left = e.AffectedControl.Left - pnlGBBody.Left
i.PerformLayout()
End If
End Select
End Select
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
It works.
Man, I can't wait for the weekend to begin.
Gremmy, I can't rate you yet :(
GremlinSA
July 4th, 2008, 06:32 AM
Hmm i see what happened there .. you can work with 'e' directly.. Have to use 'NEW' ..
Well i'm glad you got it all working again ...
As with the reps, in time ...
Cheers..
HanneSThEGreaT
July 4th, 2008, 06:34 AM
Spoke too soon.
If I do this, as it should be:
Private Sub HTG_Group_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
Try
Dim i As New Control
i = e.AffectedControl
If i.Name <> Me.Name And i.Name <> pnlGBBody.Name And i.Name <> lblGBHead.Name Then
Select Case i.Top
Case pnlGBBody.Top To pnlGBBody.Top + pnlGBBody.Height
Select Case i.Left
Case pnlGBBody.Left To pnlGBBody.Left + pnlGBBody.Width
If e.AffectedProperty.ToString() = "Bounds" Then
'set the parent
i.Parent = pnlGBBody
pnlGBBody.Controls.Add(i)
' 'calculate the top & left to drop it where the user intended
i.Top = e.AffectedControl.Top - pnlGBBody.Top
i.Left = e.AffectedControl.Left - pnlGBBody.Left
i.PerformLayout()
End If
End Select
End Select
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
It still happens :(
Let me rather make a separate Panel control, then add it to the main usercontrol - that should work
HanneSThEGreaT
July 8th, 2008, 02:23 AM
Ok, it seems that not enough sleep and just plain common stupidity ( mine ) has caused this :D
Having a panel with scrollbar on the usercontrol is overkill, the usercontrol already Inherits from Scrollable control, so meaning, I am able to set its AutoScroll to true with all the autoscrollmargins properties as well :)
So, all I ended up doing with the usercontrol was this :
Imports System.ComponentModel
Imports System.ComponentModel.Design
<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
Public Class Blah
This makes the usercontrol a container, meaning all controls added will be children :D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.