Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

.net - Application not clearing Dialog contents on form close

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:emp
obo1 C:emp
obo2 /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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is the way you are using the Dialog:

Private Sub btnPing_Click(sender As Object, 
        e As EventArgs) Handles btnPing.Click
    dataTransferBtnCase = "ping"
    formDataTransfer.ShowDialog()
    btnRobocopy.Enabled = True
End Sub

If you use ShowDialog, you ought to also Dispose 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:

Dim dlgR As DialogResult
Using dlg As New formDataTransfer
   ' use a public property maybe to pass the data
   dlg.ActionToTake = "ping"

   dlgR  = dlg.ShowDialog()
   ' ... do stuff
End Using      ' dispose of the dialog

Since the form is not automatically disposed of - you probably have code to just Hide it - you are reusing the last instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...