【问题标题】:Why openFileDialog doesn't responding when i dispose the form and back to it again?为什么当我处理表单并再次返回时 openFileDialog 没有响应?
【发布时间】:2020-03-25 04:52:52
【问题描述】:

我在一个表单中有两个 openFileDialog 工具,当我浏览 openFileDialog1 并选择一张图片时,没关系,但是当我处理此表单并返回它并单击以浏览另一张图片,它没有响应,项目停止,没有错误!!!

    Private Sub Browse_Click(sender As Object, e As EventArgs) Handles Browse.Click
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Image Files (*.jpg, *.bmp, *.gif, *.png)|*.jpg; *.bmp; *.gif; *.png"
        If OpenFileDialog1.FileName = "" Then
            If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                txtFileName.Text = OpenFileDialog1.FileName
                txtFileName.SelectionStart = txtFileName.Text.Length 'to show the last portion of text
                If Trim(txtFileName.Text) <> "" Then picSave.Image = Image.FromFile(txtFileName.Text)
            End If
        Else
            Exit Sub
        End If
    End Sub

    Private Sub Btnclose_Click(sender As Object, e As EventArgs) Handles Btnclose.Click
        Me.Dispose()
    End Sub

【问题讨论】:

  • Me.Close()。这有什么意义:If OpenFileDialog1.FileName = "" Then ... 当您在检查之前设置 OpenFileDialog1.FileName = "" 时?顺便说一句,您可以在需要时创建一个 OpenFileDialog 对象:using ofd as new OpenFiledialog (...) end using

标签: vb.net


【解决方案1】:

在项目属性中,确保您已将关闭模式设置为“当最后一个表单关闭时”。这不是默认设置。

在表格 1 中

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    OpenFileDialog1.Filter = "Image Files (*.jpg, *.bmp, *.gif, *.png)|*.jpg; *.bmp; *.gif; *.png"
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName
        PictureBox1.Image = Image.FromFile(TextBox1.Text)
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form2.Show()
    Close()
End Sub

然后在Form2中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Form1.Show()
    Close()
End Sub

当我返回 Form1 时,OpenFileDialog 行为正常。

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多