Me.controls.Item on a VB.net Form is returning System.nullReferenceException 'object reference not set...' - vb.net

I've created a vb net form that uses an array list to load data into various controls. (ultimately this is to interface with the Autodesk Inventor API, but I'm not failing the API part)
My first form(1) works correctly. I needed to break my form into multiple forms due to too much information. In Visual Studio 2022 I copied my Form1, pasted it and renamed it to Form2 then changed the references to Form1 located in Form2.designer.vb. To switch between Forms, I created a 'MainMenu' form to call either Form1 or Form2
By all manner of double checking, my method of calling my Form1 or Form2 are a copy paste (other than form names)
On my new form2, I cannot access any controls using the Me.Controls.Item method. I use a button to run the population of my textbox controls, so they exist.
works:
Me.textbox1.text = "some string"
Me.text = "form name string"
doesn't work:
Me.Controls.Item("textbox1").text = "any string"
In debug, mousing over the error line I get:
Me = {Projectname.Form1...} Me.Controls = {System.Windows.Forms.Form.ControlCollection} Me.Controls.Item("textbox1") = Nothing
In the Autos window, I can see my textbox1 in Me.Controls > Owner > textbox1 (and my 'some string' value)
I can also see my control in the Locals Me > textbox1
I guess I did something wrong when Copy/Pasting my Form1 to Form2
I created a new Form3 and copy pasted the controls and the Code from my Form2 and I have the same issue.
I have crated a new Form4 and created a new control (textbox) without a copy paste and things seem to work on that form, so far.
I'm sure somebody could fix this in seconds, but I'm tapped out. My Form2 (i could have sworn it worked before) has a lot of time into formatting multiple layers of tablelayoutpanels and would really like to not re-create my form2 from scratch.

If Me.Controls.Item("textbox1") is Nothing then there is no control whose Name property is "textbox1" and whose direct Parent is the form. As you can see, there are two conditions that could cause that to be the case. The control you want does not have its Name property set to "textbox1" and/or its direct Parent is some other control, e.g. a Panel.
Personally, I would scrap that form and create a new one the conventional way. Obviously you have done something wrong so who knows how far-reaching the issue(s) is?

Related

Add item to listbox from another form

I'm trying to add items to listbox from another form but the listbox doesn't seem to update.
I have 3 form : frm1, frm2, frm3
this my code in frm1 to open frm2:
Using frm As New frm2
frm.ShowDialog()
End Using
frm2 has a listbox named list_xxx
code in frm3:
Private Sub add_item()
frm2.list_xxx.add("aaaa")
End Sub
i want to add item to frm2 from frm3. but no success the listbox still empty.
how to fix it?
You are almost certainly referring to the default instance of frm2 in that second code snippet but you aren't displaying the default instance in the first code snippet. If you display one form and then add items to a ListBox on another form, there should be no surprise that you can't see those items. Either use the default instance everywhere or not at all. The more correct option is not at all, but that would require other changes too. The easier option is everywhere, which means changing the first code snippet to this:
frm2.ShowDialog()
No Using statement because you're not creating an object.

Send Parameter via new() or pre-set properties before calling the new form?

I would really appreciate your advice on the following:
I'm working on Windows forms using VB.NET (even though the language is irrelevant to the question at hand).
I've got a main form and wish to call out another one, however depending on a given variable I need the text on some of the new form's elements to change as well as disable some of its controls.
There are two ways I see of doing it:
Send a parameter from the main form and have some logic on the second form to deal with everything on load.
Main Form:
dim newform as new frmcalculate(byval type as string)
New Form:
public sub getexplanation(byval type as string)
select type
case "Sum"
lblexplanation.text = "this is a sum"
case "Subtraction"
lblexplanation.text = "this is a subtraction"
End sub
Set exactly what I want on the main form before calling the new form.
i.e:
dim newform as new frmcalculate()
newform.lblexplanation.text = "This is a sum"
I hope I've managed to explain it correctly.
I'm still new at this especially getting the formats right on Stackoverflow.
In the first approach the code is best managed and organized for further editing. So each form has it own code.
It is not best practice to use second approach. (Editing a form designer from another one)

Access Subform Source object

What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value

Visual Studio 2005 offers 2 of each column-type control in my DataGridView

I'm using Visual Studio 2005 on Windows XP.
I'm not sure if these problems are related or not.
When I drop a new DataGridView into my Windows Forms, and try to make 1 of the columns a checkBox, VS2005 gives me TWO of each columnType choice:
TextBox
TextBox
CheckBox
CheckBox
ComboBox
ComboBox
etc
etc
Is that normal? Should I use "CheckBox type 1" or "type 2"?
Also,
When I make 1 of my columns a "checkBox", and then try to use it, the checkBox seems keep turning itself back ON.
I turn it ON, and then click on a different column or row, and the checkbox stays ON, like it should.
But if I turn the checkBox OFF, and then click on a different column or row, the checkbox magically turns itself back ON. Huh?
I can't find anything in my code that says "turn it back on". And I've never connected it directly to any data source.
Am I missing something here?
Both of those issue are very strange. I think that there is either a third party control or a user control that has inherited from DataGridView or your visual studio installation is corrupt.
I would recommend looking at the form designer.vb file to see what data type the DataGridView is or if something doesn't look quite right.
To view the designer.vb, select Show All Files from the Project menu, then expand the form in question and double-click on the designer.vb file.
You can also get to this file by right-clicking on a reference to the DataGridView (or any control) in your form code (not designer) and selecting Go To Definition; this will take you to the designer file.
If this project was migrated from an earlier version of VB.Net, there is a chance that the form elements are stored directly in the form code file in the region " Windows Form Designer generated code ". If this is the case, the second method above for getting to the control definition will work or you can just search for the name of the control starting from the top of the file.
At any rate, once you get to the control definition, it should look similar to this:
Private Sub InitializeComponent()
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.Column1 = New System.Windows.Forms.DataGridViewCheckBoxColumn
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2})
Me.DataGridView1.Location = New System.Drawing.Point(68, 84)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.Size = New System.Drawing.Size(240, 150)
Me.DataGridView1.TabIndex = 0
'
'Column1
'
Me.Column1.HeaderText = "Column1"
Me.Column1.Name = "Column1"
'
'Column2
'
Me.Column2.HeaderText = "Column2"
Me.Column2.Name = "Column2"
... some additional stuff
Me.Controls.Add(Me.DataGridView1)
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewCheckBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
If the data types are different than System.Windows.Forms, then the problem is with that component.
If the data types are the same, then you can try a couple of things:
1) Close the solution and manually clear the bin directories to remove any potential confusing references. Then open the project again.
2) If 1 doesn't work, painful as it is, I would suggest uninstalling and reinstalling visual studio (I just had to do this yesterday for VS2010 due to strange behavior).

How to make a form always stay on top of another form

How to make a form always stay on top of another form.
Also both form's enabled property must be true
I don't wanna make use of topmost property.
Edit 1 :
Another similar question in C# says you can use Form.Owner Property to do the trick , how to make use of this property ?
Edit 2 : The Owner Property works fine untill I try to open it the second time.
This is the error message I get
I believe you need the frm.ShowDialog() instead of frm.Show()
frm is the other form you need to show over your current form and instead of using Show, this will make it as a dialog form over your current form (however you won't be able to select the parent form or the form behind it unless you close the frm form
EDIT
To enable edit on both forms
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();
Hope this helps you out.