|
-
July 4th, 2008, 03:27 AM
#1
[RESOLVED] [2005] Panel container on UserControl
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:
Code:
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.
-
July 4th, 2008, 05:11 AM
#2
Re: [2005] Panel container on UserControl
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
Code:
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...
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
July 4th, 2008, 05:24 AM
#3
Re: [2005] Panel container on UserControl
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
-
July 4th, 2008, 05:41 AM
#4
Re: [2005] Panel container on UserControl
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...
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
July 4th, 2008, 06:11 AM
#5
Re: [2005] Panel container on UserControl
I am such an idiot!
Somebody please just come and knock some sense into me LOL!
Gremmy, this is what I did:
Code:
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
-
July 4th, 2008, 06:32 AM
#6
Re: [2005] Panel container on UserControl
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..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
July 4th, 2008, 06:34 AM
#7
Re: [2005] Panel container on UserControl
Spoke too soon.
If I do this, as it should be:
Code:
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
-
July 8th, 2008, 02:23 AM
#8
Re: [2005] Panel container on UserControl
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|