Question Odd problem when jumping between forms

CharlieMay

Active member
Joined
Jul 17, 2009
Messages
25
Location
Indiana
Programming Experience
10+
I'm having an odd problem when making a form visible and not visible and it not continuing to function. Here is what I have happening.

I have a main form (frmMain) that is an MDIParent. On this form I have a menu to open a second form (frmCalculate) and populate a listview from Items in the database.

As I click or select items in the listview on frmCalculate the tag is read and data is pulled based on the id stored in that tag and fills in various fields on the form. This works great. However, I also have a context menu attached to the listview that allows me to perform a "what if" scenerio on the items I am calculating. When I select this menu, I hide the frmCalculate and open the frmWhatIf form where I can mess around with values on the item I had hilighted in the listview on frmCalculate. Nothing is pulled from or written back to the database here, all the information is filled in from fields on frmCalculate. It just allows me to look at rising costs and how they will affect my margins.... anyway. If I then close frmWhatIf, it brings my frmCalculate back by setting its visible propery to true (never closed frmCalculate, just hid it). At this point if I click an item in the listview I get an exception for a null reference. IF however I never hide frmCalculate, I can open and close frmWhatIF without ever having an issue. Why do I lose the functionality to select items just by hiding and unhiding the form? As a test I added two menus to the toolbar on frmMain one called hide and one called show. I then opened frmCalculate with original menu item to populate the listview and selected a few items to test that it was working and then using the hide menu I made the form invisible and the show button to bring it back. This yielded the same exception so without even opening frmWhatIf the problem still occurs.

Sorry for the length of the post I just wanted to give as much information as possible. Anyone have any ideas as to why this is happening.

I have no code in any of the frmCalculate events for hiding, closing, etc...
 
Would you care to show us the code that throws the exception? Have you checked what reference is actually Nothing? When a NullReferenceException is thrown it's because you are trying to access a member of an object via a variable that doesn't refer to an object. The first order of business is ALWAYS to determine exact what variable that is.
 
JM,
I have narrowed it down to lvwAssembly.FocusedItem.Tag throwing the exception. I'm assuming that it is because as the form becomes visible, the event it triggered and there is nothing selected. The event where the code is executed from is lvwAssembly_SelectedIndexChanged.

I have tried placing the code in lvwAssemblies_ItemSelectionChanged but receive the same results.

What I am doing is as I move through the listview, I update items on the form from the ID stored in the tag of the selected item.. This is all done with code and not data objects. eg...

sstmt = "SELECT * FROM Ingredients WHERE ID = " & lvwAssemblies.FocusedItem.Tag

the statement is applied to the database and the results are placed in various fields.

...
Is there another event where I can put this code. I tried it in Click but it doesn't trigger if the user uses the arrow keys to move through the list.

Sorry for the delay in responding, been out on a business trip.
 
So is lvwAssemblies.FocusedItem Nothing? If so then you obviously can't access it's Tag property. You'd have to use an If statement to test whether it's Nothing first and only try to use it if it's not.
 
Back
Top