Error Closing a Form Through a User Control - vb.net-2010

In the program I'm writing, there is just one form. I've made a user control visible and it contains several buttons. One of the buttons is supposed to close the form when clicked. I can't find any way to do this without getting the error: "Cannot access a disposed object. Object name: 'ShapeContainer.'" I'm pretty sure I understand the problem; after the form has been closed, the user control no longer exists, so there's an error when Form.Close() (I've tried Form.Dispose() too) has completed and it tries to go back to the code inside the button click event. Does anyone know how I could accomplish closing the form through this user control's button without getting the error?

Okay I'm pretty new to vb but I think if you get rid of the Form.Dispose() & Form.Close then try Me.Close() as you are referring to the current Form that the button control is located on.
You Should have got this error:
(Error 1 'NameSpace_.Form1' cannot refer to itself through its default instance; use 'Me' instead.)

Related

Access VBA - can’t figure out how to reference my subform control in the main form

When user updates textbox in subform want textbox in main form to update but I can’t seem to figure out how to call my subform textbox in the main form. I know it must be something simple...I just can’t seem to figure it out.
I tried
Me.subfrmAB.textbox1.Text
I tried
Me.subfrmAB.Form.textbox1.Text
Just not sure how to call it
Edit:
This is what I’m trying to do (on main form Form_Current event)
If IsNull(Me.subfrmAB.Form.Controls(subfrmtxtbx1)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx2)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx3))Then
Me.textboxAB.Text = “Phase 1”
End If
But it won’t trigger on the form current event.
I also tried
If IsNull(Me.subfrmAB.Form.Controls(subfrmtxtbx1)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx2)) Or IsNull(Me.subfrmAB.Form.Control(subfrmtxtbx3))Then
Me.textboxAB.Value= “Phase 1”
End If
But then I get an error stating that Me.textboxAB.Value =0
Try:
Me.subfrmAB.Form.Controls("textbox1").Text
Plus, Andre has also provided a great reference for Forms and Subforms - The Access Web by Dev Ashish. I've used that to track down some of these form/subform reference issues.

Proper way to reference a forms property that is two navigation subforms deep?

I have this statement that i want to execute after a button has been clicked in a pop up dialog box:
[Forms]![MainNavigationForm].[NavigationSubform].[Form]![SubNavigationForm].[NavigationSubform].Form![Form1].Refresh
I keep getting an error that Access cannot recognize SubNavigationForm and thinks I am referring to a field rather than a form.
Form1 is open and when i try and run this command
Try with:
[Forms]![MainNavigationForm]![NavigationSubform].[Form]![SubNavigationForm].Form![NavigationSubform].Form![Form1].Refresh

Application fail to exit when a runtime created object is used

I'm using vb.net 2013, and I have configured Shutdown mode to "When last form is closed".
On my main form, I have a menu item which has this code to close the application:
Application.Exit
Everything is working fine, except one case:
When I open a specific form, where a Combobox is created on runtime and I've used Addhandler to subscribe to several events.
The combobox is created when pressing a button.
When I open this form and I don't create the combobox, everything is working ok. If the combobox is created, when I close this form and try to close the application using the menu item, nothing happens. The application is not closed and no error message is displayed. (the same situation occurs when I try to close the main form with "x" button)
On the form's (where I have the combobox) close event , I tried to put a line of code:
MyCombobox.dispose()
But the situation is the same.
What can I do? Thank you!
What I do from my little experience is
1.) remove the MyCombobox from its parent control (I'm think this is in your combobox close event).
2.) set the MyCombobox to Nothing
3.) Dispose() it.
MyCombobox = Nothing
MyCombobox.Dispose()
It would be useful to see some part of your code for the close event so we can help you check. More power to you!
Update based on OP's comment:
I have read the following from MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx
From this, it is important to note that:
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Cannot access disposed form despite creating it immediately prior

I have a windows form (VB/.NET) that I want to open when I click a particular button. It opens correctly the first time, but after closing it and trying to re-open it I get this error:
"ObjectDisposedException was unhandle: Cannot access a disposed object. Object name: frm8100VI"
Now, I create a new instance of the form right before calling it's show() method, so I don't know how it's disposed:
Dim form as New frm8100VI
form.show()
This code works correctly the first time it's called, but after closing the form once it will not work correctly again, saying the object has been disposed. To close the form all I do is call me.close()
I'm guessing you have your "Dim form as new frm8100VI" located in a space inside another object or in a public space where it never goes out of scope.
Once you have said "form.show", the user has worked with the form, closes the form.
At this point you cannot call "form.Show" again on the same form object (it is disposed).
You MUST reassign "form = New frm8011VI" again before you can call "form.show" (to re-iterate, after it has been disposed show fails)
If your trying to persist the information inside the "form" object then you should not actually close the form; in the form_unload event you show cancel the closing of the form and simply hide the form. If you do that you can call "form.Show" again without issue.

How could my Application form in vb be closing by itself?

I have a form in a Pocket PC app that is set to be the Application form using:
Application.Run(New frmMain())
Somehow this form is getting closed for no reason while working on another form. No closing event is fired and nowhere in my code can you close the main form anyway. It is only closed by clicking the OK button at the top right of the window. When this is clicked, the user is prompted if he/she really wants to close the application. However, this is not occurring either.
Basically, I am on another form which adds a new record to the database. After adding the record the gotFocus event is somehow called for frmMain. When the code in the gotFocus event reaches a reference to a control on the form, there is an exception that states that the object is disposed.
When I bypass the code in the gotFocus event, the application just closes completely. I verify this by checking that it is not running in the device's memory.
I have been stepping through code for 2 hours and I have absolutely no idea why this could be occurring. Anyone possibly have a tip?
This sounds as an exception being raised anywhere. Maybe an exception on another thread? Look at the output window to see what it says.