-
Oct 24th, 2017, 07:37 PM
#1
Thread Starter
Addicted Member
Adding Dynamic controls to dynamic forms null reference exception
Adding Dynamic controls to dynamic forms null reference exception when i close the form.
Code executes and closing the "newFrm" results in a null reference exception.
Also the lstBox doesnt show
The bold text in the code shows when I get the error when i close the form
Code:
Private Sub btnGenerateLog_Click(sender As Object, e As EventArgs) Handles btnGenerateLog.Click
Dim newFrm As Form = func.genForm("Auto Generated Log File", Nothing)
Dim lst As New ListBox
lst = func.genLog(lst)
newFrm.ShowDialog()
newFrm.Container.Add(lst)
End Sub
Code:
''' <summary>
''' genLog() --
''' Accepts a listbox as a parameter and fills it with the log information
''' </summary>
''' <param name="lst"></param>
Public Function genLog(ByVal lst As ListBox) As ListBox
lst = New ListBox
stringStatistics(lst)
lst.Items.Insert(0, "")
lst.Items.Insert(0, "Time: " & Date.Now)
lst.Items.Insert(0, "")
lst.Items.Insert(0, "Autogenerated log file")
lst.Size = New Size(50, 50)
lst.Dock = DockStyle.Fill
Return lst
End Function
''' <summary>
''' genForm() --
''' Generates a basic form with the option to add context menu of your choice.
''' </summary>
''' <param name="ctx"></param>
''' <returns>Returns the newly created form.</returns>
Public Function genForm(ByVal title As String, ByVal ctx As ContextMenuStrip) As Form
Dim newFrm As New Form
newFrm.Text = title
newFrm.Size = New Size(400, 400)
newFrm.Icon = My.Forms.frmStringGen.Icon
newFrm.ContextMenuStrip = ctx
newFrm.AutoSize = AutoSizeMode.GrowAndShrink
newFrm.MaximizeBox = False
Return newFrm
End Function
It seems I am missing something whenever I try to add forms or controls programatically, I need the guidance.
-
Oct 24th, 2017, 08:02 PM
#2
Re: Adding Dynamic controls to dynamic forms null reference exception
That bold line has to happen before you show the form (and it has to be right, too, which it isn't). That's what puts the listbox onto the form and allows it to be shown, so the fact that the line is after the ShowDialog call is the reason the control isn't showing up. It may be the cause for both errors. After all, the line should be NewForm.Controls.Add(lst). I'm not entirely sure what Container does, since I've never used it, but you want the Controls collection rather than the Container.
When you get a Null Reference Exception, you need to examine all the objects in the line. One of them is Nothing, and you need to find out which it is. In this case, the candidates are:
1) NewFrm
2) NewFrm.Container
However, if you just move the line prior to the ShowDialog, and use Controls in place of Container, that is likely to fix the problem anyways.
My usual boring signature: Nothing
-
Oct 24th, 2017, 09:04 PM
#3
Re: Adding Dynamic controls to dynamic forms null reference exception
Yeah, something doesn't add up. Code only runs when you close a form if you have a Closed or Closing handler. Here's what I think is happening:
ShowDialog() shows a form Modally. That means it's on the screen, it locks its parent's message loop, and it has to be closed before the next line executes. Now, let's look at your code:
Code:
newFrm.ShowDialog()
newFrm.Container.Add(lst)
"newFrm" has to be closed before you can try to add the lst to its Container. But when it's closed, it's illegal to try and add controls to it. You maybe meant to call Show(), or you maybe meant to add lst to it beforehand.
But Shaggy Hiker is also right to point out no one mucks with Container. That's actually not even intended for controls IIRC, it's meant to host Components, or maybe it's part of the designer infrastructure, I can't recall. Either way, no one uses it. If you want to add a Control to a Form, use the Controls collection.
But you can't use it after a ShowDialog() because you've already closed the form. So you need to work on that first.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Oct 24th, 2017, 10:50 PM
#4
Thread Starter
Addicted Member
Re: Adding Dynamic controls to dynamic forms null reference exception
This is embarrassing. I should have used .Controls.Add() instead. Thank you for your speedy responses. I'm going to get to coding and update back if I encounter another error. Such a blessing to have you guys.
Tags for this Thread
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
|