Bear with me guys, I'm new to VB. Long story short. I have a VB app that has a couple buttons that open a new form. I've tried to repurpose this form for multiple uses using Case selections.
2 buttons:
btnPing:
Private Sub btnPing_Click(sender As Object, e As EventArgs) Handles btnPing.Click
dataTransferBtnCase = "ping"
formDataTransfer.ShowDialog()
btnRobocopy.Enabled = True
End Sub
And btnRobocopy
Private Sub btnRobocopy_Click(sender As Object, e As EventArgs) Handles btnRobocopy.Click
dataTransferBtnCase = "robocopy"
formDataTransfer.Show()
End Sub
In each button, it assigns the value to the variable dataTransferBtnCase.
Now, on that form, I have the following code:
Public Class formDataTransfer
'Clear the variable right off the bat
Public dataTransferBtnCase As String = ""
Private Sub formDataTransfer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oldPath As String = viewInstall.txtOldHostname.Text
Dim newPath As String = viewInstall.txtNewHostname.Text
dataTransferBtnCase = viewInstall.dataTransferBtnCase
Select Case dataTransferBtnCase
Case "ping"
Me.Text = "Pinging " & oldPath
ExecuteCommand("cmd.exe", "/D /c ping " & oldPath)
Case "robocopy"
Me.Text = "Robocopy from " & viewInstall.txtOldHostname.Text & " to " & viewInstall.txtNewHostname.Text
ExecuteCommand("robocopy.exe", "C:\temp\robo1 C:\temp\robo2 /E /XF file *.dat *.dat.log *.dat.log1 /log:robolog.log")
Case Else
Me.Text = "lol idk!"
MessageBox.Show("An unknown error has occurred!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
dataTransferBtnCase = ""
viewInstall.dataTransferBtnCase = ""
End Sub
End Class
the btnPing_Click sub works every time like it should. However, the btnRobocopy_Click sub will only work after you click it once, close the new form window, and click the button again. I'm sure it's because I'm not clearing the variable correctly somewhere, I just don't know where. Any help?
The problem is the way you are using the Dialog:
If you use
ShowDialog
, you ought to alsoDispose
of it afterwards. As is, you are re-showing the old instance with all the old data still there.Dialogs are different from a regular
Form
in this regard. When we are done with a form and close it, NET disposes of things for us. Dialogs are not typically closed (usually just hidden) so we can fetch information from them.This will create a new form instance, show it and then dispose of it when we are done:
Since the form is not automatically disposed of - you probably have code to just
Hide
it - you are reusing the last instance.