SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

[2005] Panel container on UserControl

Last post 07-04-2008, 13:56 by HanneSThEGreaT. 2 replies.
Sort Posts: Previous Next
  •  07-04-2008, 10:30 13174

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


    It's Not Answers That Change The World, It's Questions
  •  07-04-2008, 13:15 13210 in reply to 13174

    Re: [2005] Panel container on UserControl

    Hey, I'm an idiot.

    Forgot to initialise the control being added HAHA!

    This works :

     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's Not Answers That Change The World, It's Questions
  •  07-04-2008, 13:56 13212 in reply to 13210

    Re: [2005] Panel container on UserControl

    Spoke too soon.

    If I change it to what 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 doesn't work, and still gives the same problem.

    Let me rather make a separate usrcontrol for the panel and do all the containment there instead.


    It's Not Answers That Change The World, It's Questions
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems