CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    [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.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: [2005] Panel container on UserControl

    Ok, it seems that not enough sleep and just plain common stupidity ( mine ) has caused this

    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 :
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured