• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 799
  • Last Modified:

How can I draw rectangles on a form at runtime?

Question:

how can I draw an unknown number of rectangles on an Access form at runtime?

Background:

I'm building an application for a marina. I want to show the occupied spaces on top of a picture of the marina.
I want to show the picture of the marina with a filled rectangle superimposed for every space that's occupied so the proprietor can see the free spaces.

What I've tried:

I've come very very very close using the Lebans PictureBox program, but the picturebox it uses has a solid backcolor, and I need it to be transparent, and I can't accomplish that. So if you can accomplish this in the Lebans PictureBox program OR if you know of an entirely different way to create an unknown number of rectangles, please let me know.

Thanks in advance!
Keimpe
0
keimpe
Asked:
keimpe
  • 6
  • 5
  • 2
  • +1
1 Solution
 
Leigh PurvisDatabase DeveloperCommented:
Well, why not a pre-existing number of rectangles.
Already overlaid in place over the picture map - all set to invisible.
Making the appropriately named retangles visible depending on current selections.

If every spot were occupied you'd have to show that many anyway.

(Of course - changing the colour might be desirable instead of just making visible or not).
0
 
keimpeAuthor Commented:
That would certainly do in a standard situation, HOWEVER, I want users to scan the map of their own marina and have it appear on the form, so I never know how many places they have in their marina and this could easily be several hundred and I don't think pre-creating several hundred rectangles is a good idea, especially if ever you need to change their properties.

I have found that creating rectangles on the fly is a piece of cake in VB6 by using Control Arrays, so if what I want is impossible in Access, I think I'll shell out to a small VB program whenever I want to show a map with rectangles.
0
 
Leigh PurvisDatabase DeveloperCommented:
But if the map can be scanned in - how do you predict where the rectangles need to be on an unpredictable image (in either scenario)?

(Yes - creating controls, and indeed just painting on the form is pretty trivial in VB).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
keimpeAuthor Commented:
I let the users create rectangles first (I already know how to let the user draw one rectangle) to define the places where boats can lie, and then store the coordinates of that rectangle. Later I want to show the map with all the rectangles and this is what I haven't been able to do yet.
0
 
LambertHeenanCommented:
Access has the Line method for drawing lines, but in ONLY works on reports, NOT of forms.

You CAN create controls on a form, via code, but only in design mode, which is no good at all if you distribute an MDE.

Your only option may be to create a form and use the CreateControl method to programatically add two or three hundred rectangle controls to the form, all invisible and all in the same location, with names Rectange1, Rectangle2 etc.

Then you will be able to write code to programatically render individual rectangle controls visible and sized and positioned apropriately.
0
 
Leigh PurvisDatabase DeveloperCommented:
So the user effectively "records" where the rectangles go themselves?
(And names/assigns them?)

Anyway - the method you have of "drawing" a rectangle (as supplied by Markus) isn't actually drawing at all is it.
It's a precise means of positioning an already existing rectangle.  (i.e. a really low number solution of the method I suggested here ;-)

Access doesn't support (at all) the spontaneous creation of controls at runtime.
(You need to get at the form in design view for that).

Ah - I see that opinion has just been expressed before me :-)
0
 
keimpeAuthor Commented:
You are both right and wrong (this is not meant disrespectfully).

Yes, Access cannot create controls at runtime, but yes, it is possible to create rectangles by using API calls, which is what Lebans Picturebox so wonderfully demonstrates. By using his code I am able to draw as many rectangles as I like, anywhere I want to, with one drawback: the rectangles appear on a solid picture box control, and I need that control to be transparent, because underneath it is the picture of the marina upon which I need to superimpose the rectangles.
0
 
Leigh PurvisDatabase DeveloperCommented:
I'm familiar with several examples of Steven Lebans efforts (his work in Access is unsurpassed - he is a real craftsman) and first saw the example you cite some years back.

You say yourself though - that (as was stated) *controls* cannot be created at runtime.

He's manipulating the bitmap displayed in a Image control.
(But a bitmap is first created if none is present and displayed to then be manipulated - to "paint" the new renders upon).
That's why you can't see through it - there's a bitmap the exact size of the control obscuring your view.

However - I don't see why you need it behind.
Can't you just load the map imagine into the control and then paint over the top of that with rectangles wherever you want them?  (Assuming you can still predict where you'll need them).
Rather than letting it
(He's even already provided the class method for you - LoadImageControl).

Or if you want it separate and simple - you can begin by copying the image information from the control you have behind into the drawing control.

Me.DrawingImg.PictureData = Me.MapImage.PictureData
0
 
Leigh PurvisDatabase DeveloperCommented:
Nope - just tested.  Setting the direct PictureData won't work.
As, naturally, you'd have to use the class method to make it aware of the bitmap to draw upon.
But in general the concept is still there... just use LoadImageControl.
0
 
harfangCommented:
Hello keimpe

Wow, quite a project!

As far as I know, image controls cannot have a transparent background (although I haven't ckecked A2k3), but Leigh's suggestion does work, as you can easily verify using Lebans' demo form. You can then use the DrawLine and DrawPolygon methods for each boat space. Rectangles (meaning horizontal or vertical rectangles) seem to be out of the question. If I look at any harbour, I don't see how rectangles could possibly describe boat spaces.

http://maps.google.com/maps?q=46.2074+6.1557(Jet+d'eau)&t=k&ll=46.208,6.16&spn=0,0.01

Ideally, a click would place a standard-sized rectangle, with move, resize and rotate handles. Although this is possible based on Lebans' library, it will be quite a feat. I'm starting to think that VB is perhaps not the appropriate language for that...

Your next problem will be that the users will expect the map to be "hot". Moving the mouse should show tooltips or highlight a space, selecting one should reveal details, etc. You will have to reprogram that. I suspect that the marina map would become the central navigation form for your application.

So the only benefit you would get from developping in Access would be – besides the database capabilities naturally – to have selectable controls instead of rectangles. I have seen checkboxes and "option buttons" superimposed on maps, and that looks quite well. You would then have all the needed events, tooltips, etc. and would not have to reprogram that part. If you create a class module with an array or a collection of instances, you would not even duplicate any code.

The user would also be up and running much faster. Drawing all these rectangles, even with a very good interface, is quite a task. Just clicking once on every space is so much simpler.

Using Access, I would thus create 500 checkboxes and manipulate those (if the marina has more boat spaces, the user would have to work section-by-section).

Happy hacking!
(°v°)
0
 
keimpeAuthor Commented:
Thank you everyone for helping me out. Thank you Leigh, for coming up with the solution. I was totally unaware of the LoadImageControl method. It works like a charm!

And yes, Harvang, I know that marina's do not consist solely of rectangles, but letting the user draw free shapes is not my intention (i.e. too difficult for me to accomplish), they'll just have to be creative with drawing rectangles ;-)

I'm first making this program for one marina, to combine with my (already commercial) boat rental program (see http://www.vaarplan.nl if you understand Dutch) , and if successul, may market it.
0
 
keimpeAuthor Commented:
Oops, sorry, it's "harfang" (why is there no edit button?!)
0
 
harfangCommented:
keimpe,

My Dutch isn't good, but I can "almost" read it. I liked your site, and the "online handleiding" is really cool. Also, the "planbord" shows your already have some experience with rectangles...;)

To others: it's worth a peek:
http://www.vaarplan.nl/demo/Vaarplan.htm
http://www.vaarplan.nl/demo/Voorbeelden/Rondleiding.htm

Success with your project!
(°v°)
0
 
keimpeAuthor Commented:
Thanks for the compliments!

The online manual was created with RoboHelp and for the Planbord I used a Gant-chart ActiveX from PlexityHide (I believe they're Swedish)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now