0

I am using VS2015, I have a small form with a picturebox. I can't close the form until I choose an image for the picturebox. How can I close the form without choosing an image? It seems to hang.

This Line in Form1.Closed seems to cause the problem. When I remove it everything is fine except then the image won't save when I go to reload the program.

My.Settings.SetCharImage = System.IO.Path.GetFullPath(OpenImage.FileName)

Imports System.ComponentModel
Public Class Form1
    Dim OpenImage As New OpenFileDialog

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles CharImage1.Click

        OpenImage.Filter = "*.JPG;*.PNG;*.GIF|*.jpg;*.png;*.gif"
        If OpenImage.ShowDialog = DialogResult.OK Then
            CharImage1.Image = Image.FromFile(OpenImage.FileName)
        End If    
    End Sub

    Private Sub DisplayURL_Click(sender As Object, e As EventArgs) Handles DisplayURL.Click    
    End Sub

    Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed    
        My.Settings.SetCharImage = System.IO.Path.GetFullPath(OpenImage.FileName)
        My.Settings.Save()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        TextBox1.Text = My.Settings.SetCharImage
        CharImage1.ImageLocation = My.Settings.SetCharImage

    End Sub
End Class
Ken White
  • 123,280
  • 14
  • 225
  • 444
Naythan
  • 13
  • 5
  • What does `Settings.Save()` do? – John Wu Jun 24 '18 at 23:53
  • I figured it out. Hopefully this might help someone. – Naythan Jun 26 '18 at 01:49
  • It will only help someone if you give the answer. It's perfectly acceptable on SO to post an answer to your own question, if it would help someone in the future. – Ryan Bemrose Jun 26 '18 at 01:58
  • I've rolled back your edit, as it is improper to add the answer to your question. If you want to answer your own question in order to help others, that's great. However, you have to do it properly by writing an answer in the space below. You cannot edit it into the question itself. The [help] has more information about how to do it properly in [Can I answer my own question here?](http://stackoverflow.com/help/self-answer). – Ken White Jun 26 '18 at 02:05
  • Thanks for the link Ken. Hope my answer is correct. Still learning. – Naythan Jun 26 '18 at 14:00

1 Answers1

1

This Is what I figured out. It seems this line

My.Settings.SetCharImage = System.IO.Path.GetFullPath(OpenImage.FileName)

Was causing the form to wait for me to open the OpenFileDialog and choose a file, causing the form to hang. This new solution grabs the file name and location and writes it to the image.tag. The thing that caused me the most problem was realizing I need to re-save the image location string back to the image.tag when I load, otherwise it's lost.

I think this is correct. If anyone has a better understanding of what is happening, let me know.

Public Class Form1

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles CharImage1.Click
        ' On Click, set up a new File Dialog
        Dim OpenImage As New OpenFileDialog
        ' Set File Filters and Title
        OpenImage.Filter = "*.JPG;*.PNG;*.GIF|*.jpg;*.png;*.gif"
        OpenImage.Title = "Show Me the Money!"
        ' Open the Dialog, check if a suitable file we chosen
        If OpenImage.ShowDialog = DialogResult.OK Then
            CharImage1.Image = Image.FromFile(OpenImage.FileName())
            'Save the file name and path somewhere.  This case is in the Image.Tag
            CharImage1.Tag = OpenImage.FileName
            'Use previous Tag info when hitting Cancel button
        ElseIf DialogResult.Cancel Then
            CharImage1.ImageLocation = CharImage1.Tag

        End If

    End Sub

    Private Sub DisplayURL_Click(sender As Object, e As EventArgs) Handles DisplayURL.Click
        'TextBox2.Text = CharImage1.Tag

    End Sub


    Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        'On Close, Save Settings with Tag Info
        My.Settings.TextBox2String = CharImage1.Tag
        My.Settings.Save()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Load Tag info to Image.ImageLocation, and reload Tag info
        CharImage1.Tag = My.Settings.TextBox2String
        CharImage1.ImageLocation = CharImage1.Tag


    End Sub
End Class
Naythan
  • 13
  • 5