Drawing Shapes In A Userform (Simple Circles and Rectangles)

JerBearG

New Member
Joined
Oct 4, 2014
Messages
12
Hello world!

I have a unique "problem" that I'm looking to solve and I'm wondering if anyone is able/willing to take on this challenge with me.

This is purely for visual "scale confirmation" and aesthetics, but I'm pretty sure it is possible and I'd like to implement this into my userform. Even a nudge in the right direction would be great. I've seen this guide, but I'm not sure how I would implement this into my existing userform: (andy pope) http://www.andypope.info/vba/userformdraw.htm

I've got users who will be selecting between different shapes (during quoting) that we will be cutting in our steel shop. If they select our stock shape "Ring" and enter the outside and inside diameters, I want a scaled version of that exact ring to be drawn in my userform in a picture "framed" area next to the input section. The same goes for if they select Rectangle, and that's about it... I just want my users to be able to see a fat rectangle or a skinny one depending on the dimensions of the rectangle, same goes for the rings... (small or large difference between inside and outside diameter). For this application, any "Disc" shape would always look the exact same since it would be scaled to fit the "viewing area"


It would help a lot if I can simply see how someone has successfully drawn a rectangle and a circle of variable dimensions into a userform!

Thanks in advance.

Note: Input only comes from two textboxes (two variables) "Width or OutsideDiameter" and "Length or InsideDiameter". I have error checkers to make sure that the width is always shorter than the length for rectangles, and the opposite for discs/rings (OD > ID). (this is odd from a programming standpoint, but it's basically an industry standard)

Also, I use excel 2007.

Last thing, I posted this question 1 month ago here. (with no solutions or responses).
http://www.mrexcel.com/forum/excel-questions/863793-drawing-shapes-userform-simple-circles-rectangles.html

Thanks for looking,
Jeremy
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I'm no expert (just came here to ask a question a couple hours ago), But I know not getting a response can suck :P but I had to do something a little similar. It won't be exact, but, what you could do it stock the userform with pictures that you have hidden on startup. when they enter a value into the second number box, have on "textbox_change" or something read what the values are and decide what picture to show. maybe like:
if textbox1.value compared to textbox2.value = some ratio (to determine the shape), show Picture X. then just have different pictures show if different width/length ratios are presented.

Remember I'm just a casual trying to help, My knowledge on VBA is pretty limited, Hope someone else can provide a better solution.
 
Upvote 0
You could perhaps just get some nice pictures of your shapes and load then in an Image control based on the Users selection.
 
Upvote 0
Hi,

I agree with sunnydaze, you might be able to get by with a small set of images and the best one could be selected and displayed.

There are ways to get images on userforms but they often involve some extensive coding.

This method works, it is a bit tenuous but will do what you want. It works like this:

Create a temporary worksheet for use as a "scratchpad".
Draw the shape you want to see on the scratchpad.
Copy the image to the clipboard.
Create a Chart (did I mention it was tenuous?)
Paste the clipboard into the Chart.
Export the Chart as a File.
When you initialize the UserForm, load the file into an Image object embedded on the UserForm.

The code below is not a complete solution but it will demonstrate the above technique.

Code:
Sub PictureOnForm()
    Static iSw As Long
    Dim r As Range
    Dim co As ChartObject
    Dim picFile As String
    Dim ws As Worksheet
    Dim sh As Shape
    
    Set ws = ThisWorkbook.Worksheets("_tmp")
    
    Application.ScreenUpdating = True ' Required for CopyPicture Method
    Set r = ws.Range("A1")              ' Use Cell A1
    r.ColumnWidth = 100                 ' Make it ...
    r.RowHeight = 400                   ' ... large enough
    
    ' Remove existing Shjapes from the Worksheet
    For Each sh In ws.Shapes
        sh.Delete
    Next

    ' Draw the Shape
    Set sh = ws.Shapes.AddShape(msoShapeNotchedRightArrow, 87, 125.25, 294, 215.25)

    ' Copy range as picture onto Clipboard
    r.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    
    ' Create a Temporary File Name
    picFile = Environ("Temp") & "\Temp.jpg"
    
    ' Create an empty chart with exact size of range copied
   Set co = r.Parent.ChartObjects.Add(Left:=r.Left, Top:=r.Top, Width:=r.Width, Height:=r.Height)
    With co
        ' Paste into chart area, export to file, delete chart.
        .Chart.Paste
        .Chart.Export picFile
        .Delete
    End With
    
    ' Display the UserForm
    UserForm1.Show

End Sub

And for form initialization:
Code:
Private Sub UserForm_Initialize()
    Image1.Picture = LoadPicture(Environ("Temp") & "\Temp.jpg")
End Sub

You will have things like size, aspect ratio, file and sheet names to sort out.
 
Upvote 0
Ah, interesting. I'll have to give that a shot in the next few days. Very creative solution. And yep, each one of those last items you listed (Size, aspect ratio, and file names) would be a little tricky but eventually doable.
 
Upvote 0
Hi, I always use the Temp folder and re-use the file name so that it does not start filling the folder - each one will overwrite.

Aspect ratio: You might be able to make everything square - don't know because I haven't tried.

The reason for the new worksheet and the enlarged A1 cell is to avoid cell lines in the background.
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,793
Members
451,589
Latest member
Harold14

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