Picture by name (string)

Hi,

I have some starter problems with xojo, so i hope you can help me.
I used VBA for Zenon before, but its a little bit different. In VBA for Zenon It was easy to get something by the name, as string:

Dim P as picture
Set P = thisProject.Pictures.Item(“Picture1”)

But now i want to use xojo and I can’t finde a way like this.

I have a canvas and I draw a picture in it.
In my project, I have many different pictures and I want to change them automatically at the runtime. (I have put the pictures directly into my project by drag and drop, so they are at the same level like App, Window… or MainMenuBar.)
My first idea was creating an array with a structure and put all parameters in it, like P As Picture, X As Integer and and and
but xojo said: “Picture isn’t allowed in structure” or something else…
So I’ve put an string into the structure that contains the name of the picture.

Now: How can I get the Picture into the DrawPicture Function by using a string with the name of the picture?

Maybe I don’t have the right idea, and there is a better way, so please give me the right idea…

Thank you!

Andreas

If you’ve dragged them into your project, you can access them anywhere in your code just by typing their names (the IDE will even autocomplete the name for you).

Example:

Assuming you have myPicture1.jpg dragged into your project, this should work (in your canvas’ Paint event):

g.drawPicture myPicture1, 0, 0

Basically dragged in pictures are like global properties you can reference by name anywhere you want to use a picture.

Tip: you can also do this with text files, sounds, and other resources you drag into your projects. They work the same way.

Or, if you really want to reference them by name at runtime, you could build a dictionary with the key of each dictionary entry being the string (name), and the value being the picture itself. (In Xojo, a dictionary’s key and value are both variants, so you can use any sort of scheme you want.)

or dont drag them into the project and load them by name from disk when you need them

Maybe you could write something like this that will do roughly what I believe you want:

Function GetMyPicture(PictureToGet as String) As Picture select case PictureToGet case "Picture1" return Picture1 case "Picture2" return Picture2 case else return Picture9 end select End Function

Usage below:

dim p as Picture p = GetMyPicture("Picture1")

Cheers
Grant

Thanks to all of you.

I made it this way:

I have an Array As Structure, were I put the parameters like x, y,w1… into it but without the picture itself, because I can’t put the picture into the structure.
So i have a second Array As Picture, where my Pictures are inside.
Now I fill the Arrays and draw all of them into the canvas by using “while”.

The solution from Grant also sounds good, because i don’t need as much data space, but my question is, is your solution fast or will it slow down my program (I draw more than 100 Pictures with the while loop and they often change)?
Whats better? Get the pictures into an array and draw them or search them every time by a case before drawing?

Thank you!

Andreas

Don’t use a Structure. A Structure is a very limited device meant to be a little more flexible than a pure MemoryBlock when dealing with Declares. Instead, create a custom class and add all of your properties to it including the Picture. Then you can add those instances to a Dictionary using the name of the Picture as your key.

In fact, you can even make such a class intelligent and have it load the picture from disk only when it’s needed. Or you can mix pictures that reside in memory with those that need to be loaded from disk on demand.

Heck, you can even make it so that a picture is loaded into RAM when needed, then unloaded if it hasn’t been accessed for X time. The flexibility is practically unlimited.

But only if you work with a class and not a Structure.

Ok, I will try to use a class.

I’ll take my time and check a tutorial or something else to see how to use classes, because I’ve never used them before.
I’m a programmer for automation Technology, so I usually program PLC’s (language “st” -> based on C). There you don’t have classes. You have to use structures.

Thank you for your help. I see, I have to change my thinking when I program in xojo.

Andreas