469,319 Members | 2,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

Changing pictures

Hi!!

I have optionboxes on userform1 which dependent on which one is selected, the picture in imagebox1 on userform2 changes. I have got the code working so when i first load the programme and select an option the correct picture comes up. However when i then select a different option the picture doesnt change and remains on the first picture.
How do I get it to continue to change when different options are selected?
Hope it makes sense! Thanks in adavance!
:D
Dec 20 '11 #1

✓ answered by BigPapaN0z

Sure thing! The project contains 2 forms (frmImage, frmMain). frmImage has a picturebox (picImage) and frmMain has 2 option buttons in an array (optChoices).

No code in frmImage, copy and paste this in frmMain:

Expand|Select|Wrap|Line Numbers
  1. ' Used to preload and hold our images
  2. Private spicA As StdPicture
  3. Private spicB As StdPicture
  4.  
  5. Private Sub Form_Load()
  6.     ' Load the images for use
  7.     Set spicA = LoadPicture(App.Path & "\a.bmp")
  8.     Set spicB = LoadPicture(App.Path & "\b.bmp")
  9.     ' Let's show the Picture form and make it our child
  10.     frmImage.Show vbModeless, Me
  11. End Sub
  12.  
  13. Private Sub Form_Unload(Cancel As Integer)
  14.     ' Release/Clear our loaded images
  15.     Set spicB = Nothing
  16.     Set spicA = Nothing
  17.     ' Finally, unload our Picture form
  18.     Unload frmImage
  19. End Sub
  20.  
  21. Private Sub optChoices_Click(Index As Integer)
  22.     ' Select case for easy expandability
  23.     Select Case Index
  24.         Case 0      ' A
  25.             Set frmImage.picImage.Picture = spicA
  26.         Case 1      ' B
  27.             Set frmImage.picImage.Picture = spicB
  28.     End Select
  29. End Sub
As you can see, it looks for a.bmp and b.bmp in the same folder as the program resides. These can be any image (adjust your picturebox on frmImage if you wish).

I really hope this clarifies things for you, jcb! Feel free to pick my brain some more. :)

--EDIT--
By the way, this will NOT load a default image, as no option buttons are selected. If you want to set a default, just add this to your Form_Load:
Expand|Select|Wrap|Line Numbers
  1. optChoices(0).Value = True
Changing the index to something other than 0 will select whichever option button you wish to have selected on load. Also, changing the .Value will also initiate the "Click" event, where we change the images. Just make sure you put the above code AFTER we load the images.(Set spicA = LoadPicure)

5 1301
I've enclosed a simple example to show. Extract it somewhere and load the project. I've tried to comment on exactly what is happening. If this isn't what you're looking for, please feel free to clarify and I'll help as best as I can!
Attached Files
File Type: zip ImageSwap.zip (3.1 KB, 48 views)
Dec 21 '11 #2
Hi Thanks for getting back to me!

However I am unable to open the file you attached. is there anyway of pasting the code directly into the forum.

Cheers :)
Dec 21 '11 #3
Sure thing! The project contains 2 forms (frmImage, frmMain). frmImage has a picturebox (picImage) and frmMain has 2 option buttons in an array (optChoices).

No code in frmImage, copy and paste this in frmMain:

Expand|Select|Wrap|Line Numbers
  1. ' Used to preload and hold our images
  2. Private spicA As StdPicture
  3. Private spicB As StdPicture
  4.  
  5. Private Sub Form_Load()
  6.     ' Load the images for use
  7.     Set spicA = LoadPicture(App.Path & "\a.bmp")
  8.     Set spicB = LoadPicture(App.Path & "\b.bmp")
  9.     ' Let's show the Picture form and make it our child
  10.     frmImage.Show vbModeless, Me
  11. End Sub
  12.  
  13. Private Sub Form_Unload(Cancel As Integer)
  14.     ' Release/Clear our loaded images
  15.     Set spicB = Nothing
  16.     Set spicA = Nothing
  17.     ' Finally, unload our Picture form
  18.     Unload frmImage
  19. End Sub
  20.  
  21. Private Sub optChoices_Click(Index As Integer)
  22.     ' Select case for easy expandability
  23.     Select Case Index
  24.         Case 0      ' A
  25.             Set frmImage.picImage.Picture = spicA
  26.         Case 1      ' B
  27.             Set frmImage.picImage.Picture = spicB
  28.     End Select
  29. End Sub
As you can see, it looks for a.bmp and b.bmp in the same folder as the program resides. These can be any image (adjust your picturebox on frmImage if you wish).

I really hope this clarifies things for you, jcb! Feel free to pick my brain some more. :)

--EDIT--
By the way, this will NOT load a default image, as no option buttons are selected. If you want to set a default, just add this to your Form_Load:
Expand|Select|Wrap|Line Numbers
  1. optChoices(0).Value = True
Changing the index to something other than 0 will select whichever option button you wish to have selected on load. Also, changing the .Value will also initiate the "Click" event, where we change the images. Just make sure you put the above code AFTER we load the images.(Set spicA = LoadPicure)
Dec 22 '11 #4
THANK YOU SOOOOOO MUCH!!! :D

That was a fantastic help! Took me few reads to get it in my head but the code was very similar to what I had already written just with a few tweaks to make it work!

Much apprieciated! You are a STAR!! :D

Cheers
jcb
Dec 22 '11 #5
Very happy that this was of help, jcb. I've run into the same problem before where you think it should be working but sometimes missing just one or two small points can make a world of difference. :)

I would ask if this helped you, make sure you choose the post that helped you so other users that search the website with similar problems know the answer that solved your issue.

Again, if you have any other problems/questions/comments, we're always here to help! Best wishes!
Dec 22 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Raymond Dunton | last post: by
1 post views Thread by David | last post: by
23 posts views Thread by Wade | last post: by
2 posts views Thread by brian | last post: by
4 posts views Thread by Doug van Vianen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.