Cannot access a disposed object in VB.NET - vb.net

Having a small issue.
I am receiving a Cannot access a disposed object error for Form1
Upon clicking a menu item on the main form - the below Sub is called, which opens another form Form1
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
If (Not Form1.Visible) Then
Form1.Show(Me)
End If
End Sub
Within Form1, there is a Try block. If it isn't passed, Form1 should show a message box, before closing down. The message appears, but it's then that I receive the error (where it says Form1.Show(Me))
Private Sub Form1_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
'DO STUFF
Catch
MsgBox("Error loading in data. Please contact an administrator")
Me.Close()
Return
End Try
End Sub
I am quite new to this type of programming, and struggling to fix the problem even after searching similar problems. Could someone please assist or point me in the right direction?
EDIT: So looks like this is due to trying to close the form during the Load event. So my question now is, are there any simple alternatives? I've found ways of doing this in C#, but not a lot for vb.net

Here is one option:
Private loadFailed As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'...
Catch ex As Exception
loadFailed = True
End Try
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If loadFailed Then
MessageBox.Show("Load failed")
Close()
End If
End Sub
In that case, the form will show with the message over it, then it will close when the message is dismissed. Here's an option that will not display the form:
Friend Module Form1Manager
Public Sub ShowForm1(owner As Form)
If Not Form1.Visible Then
Try
'...
'Pass data to Form1 here.
Form1.Show(owner)
Catch ex As Exception
MessageBox.Show("Load failed")
End Try
End If
End Sub
End Module
and, to use that:
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Form1Manager.ShowForm1(Me)
End Sub

Related

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
I appreciate any help you can offer!
From my comments above:
When you create your instance of your second form called "formNew",
use the AddHandler command to wire up its FormClosed event to a method
in the main form. Then in the second form you can just call Me.Close()
and the method in the main form will fire where you can call
Me.Show()..
In main menu:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

Expression is not a method

Hello Coders Im trying to code a Anti virus and the following problem was here!
Im trying to add MsgBoxStyle.YesNo and will the problem in this...
So im trying to add a Question for delete file or no with MsgBox.
My code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If My.Settings.ifTmpR = True Then
For Each aF In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp)
Try
MsgBoxStyle.YesNo()
MsgBox("Biztosan törli: " + aF)
IO.File.Delete(aF)
MsgBox("Temp kiürítése: " + aF)
MsgBox(aF + "kitötrölve")
Catch ex As Exception
End Try
Next
End If
If My.Settings.ifTmpR = False Then
Try
MsgBox("Kérlek kapcsold be a TEMP Queryt!")
Catch ex As Exception
End Try
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
My.Settings.ifTmpR = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = False Then
My.Settings.ifTmpR = False
End If
End Sub
End Class
And what the problem?
Help?
So you're coding an Anti-Virus application but you seem to have no basic knowledge about how to use methods and parameters.
You are supposed to include MsgBoxStyle.YesNo as one of the parameters to the MsgBox() method. You cannot call it like you do since it's simply an Integer value.
As Steve suggested you should use MessageBox.Show() instead, due to that MsgBox only exists for backwards compability.
The first parameter is the message you want it to show, and the second parameter is what title the window should have. They're just plain strings.
The third parameter however is the one you pass to tell the method what buttons to include in the MessageBox, and it should be passed like this:
MessageBox.Show("I am a message", "I am a title", MessageBoxButtons.YesNo)
This will show a message box with the buttons "Yes" and "No" inside it.
Now to get use out of this you should put your MessageBox in an If-statement, otherwise the application won't care if you press either Yes or No.
If MessageBox.Show("Are you sure you want to delete this file?", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Delete code here.
End If
There. Now I suggest that you would have started with something like a "Hello World"-application instead of an Anti-Virus application, as Anti-Virus is pretty much harder than just showing a simple MessageBox.

ToolStripTextBox action on pressing Enter key

I've got a similar issue - i've created a Windows Form Application which basically looks up data from a SQL server based on a code entered into the ToolStripTextBox. This works great when the button is clicked however I'd like to add the function of hitting enter after typing and that brings the results through.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet.Table' table. You can move, or remove it, as needed.
Me.TableTableAdapter.Fill(Me.DataSet.Table)
End Sub
Private Sub FillBySearchToolStripButton_Click(sender As Object, e As EventArgs) Handles FillBySearchToolStripButton.Click
Try
Me.TableTableAdapter.FillBySearch(Me.DataSet.Table, TYPEToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Appologies as I am a newbie.
Just use the KeyDown event:
Private Sub ToolStripTextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles ToolStripTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'run your code
End If
End Sub
The "run your code" block could just call the click event:
FillBySearchToolStripButton.PerformClick()

Reset My.Settings to values last saved

I'm currently using a "Settings" form to set various settings for my application. What I'm trying to do is to revert settings back to before any changes the user has made before opening and changing a field. I have a text box with a data binding to a setting and when I make a change and click okay it gets saved the next time I open it. When I hit cancel it also gets saved. Not quite sure if I'm approaching this correctly.
Public Class frmSettings
Private _mysettings As Configuration.SettingsBase
Private Sub frmSettings_Load(...) Handles Me.Load
_mysettings = My.Settings
End Sub
Private Sub btnCancel_Click(...) Handles btnCancel.Click
For Each p As Configuration.SettingsPropertyValue In _mysettings.PropertyValues
My.Settings(p.Name) = p.PropertyValue
Next
Me.Close()
End Sub
Private Sub btnOkay_Click(...) Handles btnOkay.Click
My.Settings.Save()
Me.Close()
End Sub
End Class
Rather than using databound control you can simply load the value of the setting when the settings form loads. It is simple doing it that way, and it works. Otherwise you would have to make a clone of My.Settings: doing _mysettings = My.Settings is only creating a pointer to My.Settings, not a copy of it.
For example, I have a Form named ChangeConnectionString which has OK/Cancel buttons and a TextBox control named connString:
Public Class ChangeConnectionString
Private Sub bnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnOK.Click
My.Settings.connectionString = connString.Text
My.Settings.Save()
Me.Close()
End Sub
Private Sub bnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCancel.Click
Me.Close()
End Sub
Private Sub ChangeConnectionString_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connString.Text = My.Settings.connectionString
End Sub
End Class

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub