Question Retrieving info from registry works... sometimes.

JLM

Member
Joined
Nov 23, 2012
Messages
6
Programming Experience
10+
My current project sets and gets registry values to retain user preferences. One of these preferences are the contents of a picturebox. Below I have included all code that interacts with the picturebox in question. Keep in mind that I have the picturebox's InitialImage set to a placeholder image, which then is changed by the user and saved to the registry (path to the file). The registry shows these values are saved properly, yet upon loading the program, the picturebox shows the saved image, but when transferring the image contents to another picturebox (via picturebox.image) it usually transfers the initialimage value, not the image value. This ofcourse can be fixed if the user sets the imagelocation at the launch every time the app is started, defeating the purpose of saving these settings via registry. I am at a loss, lol.

(MergeImages is a separate function that overlays picturebox3.image over picturebox1.image.)

*Note, in pulling the below code out of the entire project, I may have inadvertently found the problem, please enlighten me! I am pulling the file path previously saved in the registry, which is setting the image value to what is expected. But when pulling the image value it provides its initialimage value. Could this be the issue, and if it is, can I somehow force imagelocation/image/initialimage to all be the same? (in theory, I know in the literal sense this would be impossible, being imagelocation is a string value, but you know what I mean)

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        PictureBox3.ImageLocation = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\DJMCReHost", "Overlay", ToString)


    End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


            PictureBox1.Image = MergeImages(PictureBox1.Image, PictureBox3.Image)



    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\DJMCReHost", "Overlay", PictureBox3.ImageLocation)


    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click


        Dim dialog As New OpenFileDialog()


        If DialogResult.OK = dialog.ShowDialog Then
            PictureBox3.Image.Dispose()
            PictureBox3.Image = Nothing
            PictureBox3.ImageLocation = dialog.FileName
        End If


    End Sub
 
Is there a specific reason that you're using the Registry instead of the recommended option of application settings? The only advantage that using the Registry offers that I'm aware of is easier access to the data for other non-.NET apps. If you don't need that then there's really no reason to be using the Registry at all.
 
Application settings? I'll do a bit of research and try this out. I was not aware there was another option besides registry and local files (such as a text array or ini file).
 
Well, converted my code to use application settings (and will from now on as well, thats awesome!).

Problem is, issue not resolved. I saved the picturebox's imagelocation via app settings, set it to load the settings in the form_load event. I closed/opened the program 5 times. First 4 times it worked like a charm. 5th time it returned the initialimage value.

If I clear the picturebox and load the setting again one line prior to the merging, nothing gets merged.

Any input?
 
Here is the new code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       
 PictureBox3.ImageLocation = My.Settings.Overlay
    End Sub        

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click            
PictureBox1.Image = MergeImages(PictureBox1.Image, PictureBox3.Image)    

End Sub    

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click        
My.Settings.Overlay = PictureBox3.ImageLocation        
My.Settings.Save()
    End Sub    

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click        
Dim dialog As New OpenFileDialog()        
If DialogResult.OK = dialog.ShowDialog Then            
PictureBox3.Image.Dispose()            
PictureBox3.Image = Nothing            
PictureBox3.ImageLocation = dialog.FileName        
End If    
End Sub
 
Last edited:
You should simply bind your property to the setting. Then you need no code at all. Select your PictureBox in the designer, open the Properties window, expand the (ApplicationSettings) property and then open the (PropertyBinding) property. That will display a dialogue that allows you to bind control and component properties to application settings. It will even create the setting for you. There is then no code required at all. The value will be automatically loaded from the config file into the setting and pushed from the setting to the property at startup. Whenever you change the property it will be automatically pushed to the setting and then the setting will be automatically saved at shutdown.
 
You should simply bind your property to the setting. Then you need no code at all. Select your PictureBox in the designer, open the Properties window, expand the (ApplicationSettings) property and then open the (PropertyBinding) property. That will display a dialogue that allows you to bind control and component properties to application settings. It will even create the setting for you. There is then no code required at all. The value will be automatically loaded from the config file into the setting and pushed from the setting to the property at startup. Whenever you change the property it will be automatically pushed to the setting and then the setting will be automatically saved at shutdown.


Isn't that what I'm doing? Using the Application settings I mean. Also keeping in mind that this value needs to be changeable by the user. And it *IS* saving the setting via application settings. It's even loading the settings. Only not always. As my previous replies show, it loaded them 4 out of 5 times for the picturebox (loads them fine for any other controls, such as text boxes and sliders)
 
Given that I said to use the (ApplicationSettings) property and no code but you are using code and not the (ApplicationSettings) property, no that's not what you're doing. That said, if it's working most of the time then it's working. It would just be cleaner if you used binding.

As for the issue, it would appear that it's because you are setting the Image property. Doing that has zero effect on the ImageLocation property. The ImageLocation property is the address of an image to load into the PictureBox. If you then set the Image property, it will load a new Image but the ImageLocation is not affected. If you want to be able to use the ImageLocation like this then don't set the Image property. Create an Image and save it to a file and then use the path of that file to set the ImageLocation. ImageLocation has to be the path of a file, either local or online, so you can't expect to be able to save the location of an Image that is not contained in a file.
 
Thanks for all of the input and help, I learned about applicationsettings and how to use it, although it was not the topic at hand. It seems the issue was that the picturebox must be loaded on-screen at least once after app load, if not it only returns the .initialimage value. So basically the below scenario will NOT work, fyi.

1 - set .imagelocation value for picturebox
2 - save said value to registry or via applicationsettings (binding or not, no difference to outcome)
3 - set app to auto apply saved value upon form_load
4 - have picturebox off-form by default (My app has settings panel hidden on right side, clicking settings button expands form to show settings as well as our picturebox)

5 - run app, copy .image value from our pb to a secondary pb returns .initialimage, not our registry/appsettings declared image
or
6 - run app, extend settings portion, copy .image value from our pb to a secondary pb works great (under the assumption that it is because the pb is displayed on screen, forcing the app to acknowledge our saved settings)
 
Back
Top