Change image in weights form based on option selected

JGH

New Member
Joined
Dec 16, 2004
Messages
12
Hi,
I have created a form in Excel VBA and have some option buttons which when selected calculate various volumes of vessels depending on shape selected. How do I change an image on the form associated with a particular shape indicated by option button selected.

For example a horizontal cylindrical vessel could comprise of a dished or flat ends with a cylinder in the middle, (so on the form there would be 3 images, left hand end, middle and right hand end) as each option is selected the image changes in line with the option selected on the form.

The option button could be changed for a dropdown box I suppose this could also cascade if required.


Also what is the best way to place an image on the form, how does one generate the image and what is the best file format to use?

any suggestions would be appreciated

regards


john
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello John,

I'm not an expert on Images or Userforms, but since you haven't gotten any bites I'll share two approaches for you to consider.

Approach A: Use a single Image object and load a different Picture each time your Option button changes.

Approach B: Use 3 Image objects and when your Option button changes, Show 1 image and Hide the other 2 images.

Here is example code for Approach A. It assumes you have a UserForm1, with 3 Option Buttons inside a Frame; and an Image object called ImageControl. You also need to have the 3 .jpg files at the path shown.

Code:
'---Paste this into Userform1 code
Private Sub OptionButton1_Click()
    Update_Image_A
End Sub
Private Sub OptionButton2_Click()
    Update_Image_A
End Sub
Private Sub OptionButton3_Click()
    Update_Image_A
End Sub
 
'---Paste this into a Module
Option Explicit
Public Function Update_Image_A()
    With UserForm1
        If .OptionButton1.Value = True Then
            .ImageControl.Picture = LoadPicture("c:\test\LeftHandEnd.jpg")
        ElseIf .OptionButton2.Value = True Then
            .ImageControl.Picture = LoadPicture("c:\test\Middle.jpg")
        Else
            .ImageControl.Picture = LoadPicture("c:\test\RightHandEnd.jpg")
        End If
    End With
End Function

Here is example code for Approach B. It assumes you have a UserForm1, with 3 Option Buttons inside a Frame; and a 3 Image objects called Image1, Image2 and Image3.

You need to load 1 Picture into each Image as follows:
-Display the Image's Properties;
-In the Picture Property, click the Browse button and select the corresponding .jpg file.

Code:
'---Paste this into Userform1 code
Private Sub OptionButton1_Click()
    Update_Image_B
End Sub
Private Sub OptionButton2_Click()
    Update_Image_B
End Sub
Private Sub OptionButton3_Click()
    Update_Image_B
End Sub
 
'---Paste this into a Module
Option Explicit
Public Function Update_Image_B()
    With UserForm1
        If .OptionButton1.Value = True Then
            .Image1.Visible = True
            .Image2.Visible = False
            .Image3.Visible = False
        ElseIf .OptionButton2.Value = True Then
            .Image1.Visible = False
            .Image2.Visible = True
            .Image3.Visible = False
        Else
            .Image1.Visible = False
            .Image2.Visible = False
            .Image3.Visible = True
        End If
    End With
End Function

Pros and Cons:
The main difference is that Approach B embeds the images into your workbook whereas Approach A loads them from .jpg files as needed.

If you are distributing the workbook to other users, Approach B is cleaner because everything is in one file. On the other hand, if you had a lot of large images, your file size will be greater and you would need to manage all those separate Image objects in your Userform.

I'd welcome any feedback or corrections on this...most my experience on this topic came from my experimenting with this tonight after reading your question. ;)

Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top