Loading a User Control onto a panel at runtime
-
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
-
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
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
-
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
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 callingmyUserControl.Validate()
andCreateControl()
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 -
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 callingmyUserControl.Validate()
andCreateControl()
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, HenryCan 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
-
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
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 -
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 formYou 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
-
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
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
-
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
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!