Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Loading a User Control onto a panel at runtime

Loading a User Control onto a panel at runtime

Scheduled Pinned Locked Moved Windows Forms
debugginghelp
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Henry Venn
    wrote on last edited by
    #1

    Hello All, I am fairly new to VB and I am trying to load a user control (with a few text boxes - nothing special) using the built in snippet: Dim myAssets As New UserControlAssets() With myAssets .Location = New Point(64, 40) .Size = New Size(668, 488) .TabIndex = 0 End With Try Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) Catch nrx As NullReferenceException Debug.Print("Apparently its NULL.") End Try This is the exception. I have tried variations of different controls, even a listview. I am missing something vital, and I've spent so much time on this. If you cant see the problem from the code but have a clue please respond and I will provide additional data. Regards, H. Venn

    T 1 Reply Last reply
    0
    • H Henry Venn

      Hello All, I am fairly new to VB and I am trying to load a user control (with a few text boxes - nothing special) using the built in snippet: Dim myAssets As New UserControlAssets() With myAssets .Location = New Point(64, 40) .Size = New Size(668, 488) .TabIndex = 0 End With Try Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) Catch nrx As NullReferenceException Debug.Print("Apparently its NULL.") End Try This is the exception. I have tried variations of different controls, even a listview. I am missing something vital, and I've spent so much time on this. If you cant see the problem from the code but have a clue please respond and I will provide additional data. Regards, H. Venn

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      You will need to narrow down what part of the following statement is null: Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) If ParentForm is null, then that's your problem. The other (and more likely) location would be Item("panelMiddle"). Where is this code being executed? If Me is the form that contains the panelMiddle control, then you don't need the ParentForm portion of that code.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      H 1 Reply Last reply
      0
      • T TJoe

        You will need to narrow down what part of the following statement is null: Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) If ParentForm is null, then that's your problem. The other (and more likely) location would be Item("panelMiddle"). Where is this code being executed? If Me is the form that contains the panelMiddle control, then you don't need the ParentForm portion of that code.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        H Offline
        H Offline
        Henry Venn
        wrote on last edited by
        #3

        Thankyou, Tom. The code indeed references the panel incorrectly. I have altered it to Try Me.Controls.Item("panelMiddle").Controls.Add(myAssets) which does work better - it doesn't throw an exception. However, my user control flashes in the viewport for a few milliseconds and is gone. It is also inside the area of the parent form, which seems strange because when I use the code with a stock control it works. I am guessing that because it is a user control I may need some code in its class, however I cannot find any clues. I tried calling myUserControl.Validate() and CreateControl() to no avail. However the information you gave me was crucial. Looking at the code I assumed it was obtaining the reference for panelMiddle from the ParentForm - however the "Me" is like this in C++ so I see why this confuses it. Cheers, Henry

        T 1 Reply Last reply
        0
        • H Henry Venn

          Thankyou, Tom. The code indeed references the panel incorrectly. I have altered it to Try Me.Controls.Item("panelMiddle").Controls.Add(myAssets) which does work better - it doesn't throw an exception. However, my user control flashes in the viewport for a few milliseconds and is gone. It is also inside the area of the parent form, which seems strange because when I use the code with a stock control it works. I am guessing that because it is a user control I may need some code in its class, however I cannot find any clues. I tried calling myUserControl.Validate() and CreateControl() to no avail. However the information you gave me was crucial. Looking at the code I assumed it was obtaining the reference for panelMiddle from the ParentForm - however the "Me" is like this in C++ so I see why this confuses it. Cheers, Henry

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Can you please include more code? It's really hard to tell what is going on from a single line of code.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          H 1 Reply Last reply
          0
          • T TJoe

            Can you please include more code? It's really hard to tell what is going on from a single line of code.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            H Offline
            H Offline
            Henry Venn
            wrote on last edited by
            #5

            Hey Tom Sorry there's not much to it really - here: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim myAssets As New UserControlIncome() With myAssets .Location = New Point(64, 40) .Size = New Size(100, 20) .TabIndex = 0 End With Try ' Original Code causing error: ' Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) Me.Controls.Item("panelMiddle").Controls.Add(myAssets) ' this doesn't work: Me.Controls.Item("panelMiddle").Show() Catch nrx As NullReferenceException Debug.Print(" NULL ref putting user control on panel") Catch ex4 As Exception Debug.Print("Exception - plian vanilla") End Try The user control I created using VS8 and even if it is a blank "form" it only flashes on screen for a few seconds. Like I said, if I use a text control, or listview or something, it stays. I've looked at what I can on user controls but all the doc'n I can find deals with the designer - i.e. compile time. (note: the controls work perfectly if I place them on a form, make it invisible and then show it when I need to, but this is a kludge I do not need I feel). Thanks for your help so far - I am in a position where I am finishing uni and really need to find a team I can work with so I don't get bogged down for so long on these kinds of errors. Regards, Henry ps When I use a textbox or another pre-packaged MS control it is placed correctly on the panel, however when the user control briefly flashes on screen it is out of bounds of the panel, I think at the Point (64,40) of the parent form

            T 1 Reply Last reply
            0
            • H Henry Venn

              Hey Tom Sorry there's not much to it really - here: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim myAssets As New UserControlIncome() With myAssets .Location = New Point(64, 40) .Size = New Size(100, 20) .TabIndex = 0 End With Try ' Original Code causing error: ' Me.ParentForm.Controls.Item("panelMiddle").Controls.Add(myAssets) Me.Controls.Item("panelMiddle").Controls.Add(myAssets) ' this doesn't work: Me.Controls.Item("panelMiddle").Show() Catch nrx As NullReferenceException Debug.Print(" NULL ref putting user control on panel") Catch ex4 As Exception Debug.Print("Exception - plian vanilla") End Try The user control I created using VS8 and even if it is a blank "form" it only flashes on screen for a few seconds. Like I said, if I use a text control, or listview or something, it stays. I've looked at what I can on user controls but all the doc'n I can find deals with the designer - i.e. compile time. (note: the controls work perfectly if I place them on a form, make it invisible and then show it when I need to, but this is a kludge I do not need I feel). Thanks for your help so far - I am in a position where I am finishing uni and really need to find a team I can work with so I don't get bogged down for so long on these kinds of errors. Regards, Henry ps When I use a textbox or another pre-packaged MS control it is placed correctly on the panel, however when the user control briefly flashes on screen it is out of bounds of the panel, I think at the Point (64,40) of the parent form

              T Offline
              T Offline
              TJoe
              wrote on last edited by
              #6

              You may need to add the user control to the panel first then update it's Location/Size. If I create a Form in Visual Studio with a Panel that contains a single text box then the designer code loooks like this:

              /// /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              ///
              private void InitializeComponent() {
              this.panel1 = new System.Windows.Forms.Panel();
              this.textBox1 = new System.Windows.Forms.TextBox();
              this.panel1.SuspendLayout();
              this.SuspendLayout();
              //
              // panel1
              //
              this.panel1.Controls.Add(this.textBox1);
              this.panel1.Location = new System.Drawing.Point(39, 58);
              this.panel1.Name = "panel1";
              this.panel1.Size = new System.Drawing.Size(200, 100);
              this.panel1.TabIndex = 0;
              //
              // textBox1
              //
              this.textBox1.Location = new System.Drawing.Point(35, 37);
              this.textBox1.Name = "textBox1";
              this.textBox1.Size = new System.Drawing.Size(100, 20);
              this.textBox1.TabIndex = 0;
              //
              // Form1
              //
              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
              this.ClientSize = new System.Drawing.Size(292, 266);
              this.Controls.Add(this.panel1);
              this.Name = "Form1";
              this.Text = "Form1";
              this.panel1.ResumeLayout(false);
              this.panel1.PerformLayout();
              this.ResumeLayout(false);

              }

              As you can see the TextBox is added to the panel before it's properties are set. Also, make sure to assign a name to your user control. I can't remember off the top of my head, but there are some problems if you don't. Assuming this doesn't help, if you can put together a small sample project and email it over then I'll be able to figure out the problem a lot quicker.

              Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

              H 1 Reply Last reply
              0
              • T TJoe

                You may need to add the user control to the panel first then update it's Location/Size. If I create a Form in Visual Studio with a Panel that contains a single text box then the designer code loooks like this:

                /// /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                ///
                private void InitializeComponent() {
                this.panel1 = new System.Windows.Forms.Panel();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.panel1.SuspendLayout();
                this.SuspendLayout();
                //
                // panel1
                //
                this.panel1.Controls.Add(this.textBox1);
                this.panel1.Location = new System.Drawing.Point(39, 58);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(200, 100);
                this.panel1.TabIndex = 0;
                //
                // textBox1
                //
                this.textBox1.Location = new System.Drawing.Point(35, 37);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 20);
                this.textBox1.TabIndex = 0;
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.panel1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.panel1.ResumeLayout(false);
                this.panel1.PerformLayout();
                this.ResumeLayout(false);

                }

                As you can see the TextBox is added to the panel before it's properties are set. Also, make sure to assign a name to your user control. I can't remember off the top of my head, but there are some problems if you don't. Assuming this doesn't help, if you can put together a small sample project and email it over then I'll be able to figure out the problem a lot quicker.

                Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                H Offline
                H Offline
                Henry Venn
                wrote on last edited by
                #7

                Hmmm... thanks again Tom. I built a project to send to you, and it has helped - I think that the user control IS there, however it is hidden beneath a flow layout panel, which I used to structure the entire GUI. Still having the same problem however: the user control sticks to the main window, while a Windows control goes where I ask it, and stays on the panel... Will experiment a bit more. I can send you the project if a solution doesn't present itself, however I feel that its something to do with addressing the panel - but that does not explain the placement. Hold the press! I solved it - here's how, and also what surprised me: It was the sizing, like you said, because the user control would only stay on the screen if I chopped off the "With Events" clause. So I reproduced the problem in a brand new project with a main form, containing 2 panels and 2 buttons, and a user control with a label on it. Embarrassingly, the reason the control was being placed on the panel was some old code in a "Finally" clause - silly me. What surprised me is that a call to clear the panel is only required for a flow layout panel - if not, it will place the next control at the right of any existing user control. Flow layout...maybe I should not be surprised! Thanks once again - it took awhile but I got there! Henry

                H 1 Reply Last reply
                0
                • H Henry Venn

                  Hmmm... thanks again Tom. I built a project to send to you, and it has helped - I think that the user control IS there, however it is hidden beneath a flow layout panel, which I used to structure the entire GUI. Still having the same problem however: the user control sticks to the main window, while a Windows control goes where I ask it, and stays on the panel... Will experiment a bit more. I can send you the project if a solution doesn't present itself, however I feel that its something to do with addressing the panel - but that does not explain the placement. Hold the press! I solved it - here's how, and also what surprised me: It was the sizing, like you said, because the user control would only stay on the screen if I chopped off the "With Events" clause. So I reproduced the problem in a brand new project with a main form, containing 2 panels and 2 buttons, and a user control with a label on it. Embarrassingly, the reason the control was being placed on the panel was some old code in a "Finally" clause - silly me. What surprised me is that a call to clear the panel is only required for a flow layout panel - if not, it will place the next control at the right of any existing user control. Flow layout...maybe I should not be surprised! Thanks once again - it took awhile but I got there! Henry

                  H Offline
                  H Offline
                  Henry Venn
                  wrote on last edited by
                  #8

                  Er...no I didn't. It worked in the test project, but the real application has a regular panel placed inside a flow layout panel, and it doesn't throw an error.... ....10 minutes later.... panelFlowLeft.Controls.Item("panelRight").Controls.Add(myUC) That works. The confusion was the flow layout control - the panel could not be referenced as an item of the main form, because it was 3 levels deep. Pretty simple huh? Well - its all learning, and the idea to create a new project is not a new one, but perhaps I can make a rule: if I take more than 20 minutes on an apparently simple problem that should work, isolate, and recreate! Happy Coding!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups