Here I go again. Another interest caught my attention, this time about creating a listing of my contacts. I really am very forgetful with these matters. I have these lists placed before on a Page Frame, each with its own grid. But since I played last time with treeview and was able to make it run, I said to myself, yep, I will make my contact lists look sleeker and create a treeview for that too. However, I thought later why not put it in a mobile phone image instead to make it appear more realistic? That would be cooler!
But we were straying off the topic, this is not about irregular images. For that just look at the Solutions sample. Once the Mobile image is in place, I debated whether to use listview or grid (treeview is out of the question, can you imagine a mobile with treeview? What the heck?!!!)
And since I always am in favor more of a grid (with VFP9) than listview, grid it is. Now the problem starts, there is no DynamicPicture! Huh?!!! Quoting from
Marcia Akins - "
with grid, there is only one real row; the rest are just mirrors and smoke screens!". So if you choose an image, it will be applied all throughout the rest of the grid; that is why the need for DynamicPicture. Why didn't VFP Team thought of that? The answer is because as
Mike Yearwood fondly says,
"there are various ways to skin a cat!". And there are indeed ways. One is the real topic of this blog. It is called
DynamicCurrentControl.I decided to make this blog a tutorial of some sort instead of me always attaching a sample. Just to make this different.
a) So what is CurrentControl? CurrentControl property is used to specify which control contained in a column object is used to display and enter the values of the active cell. Default is textbox. Now, control is not only limited to one object. You can add as many objects within a column but VFP can only have one current (active) control at a time, i.e., CurrentControl.
b) How can we then show all the results when there is only one current control at a time? Here, Sparse property will come into play. Sparse, by default is set to .T. but setting it to .F. will do the trick. With Sparse = .F. , ALL cells (not only the active) in the column object will use the CurrentControl property setting to display data.
c) What is DynamicCurrentControl then? It is just the same with CurrentControl with one exception. It is dynamic (changing). Applying DynamicCurrentControl property in a column will instruct VFP to re-evaluate the values of EACH cells for every refresh of the Grid based on your conditions. So you can change CurrentControl in a column more than once, excellent! Just what I needed!
We needed the three properties mentioned above to achieve the trick on creating a grid with various images. Here goes:
To display an image in a cell, I cannot do that with the default textbox as the current control. I needed an Image object:
1. Use the properties window and navigate to the desired column of the target grid, in my case, Column1 of Grid1.
2. Click on the image object and then click on the grid. That will place another control on the target column which is now an Image object, aside from the default textbox.
3. Remove the textbox by navigating to it using again the properties windows. Once you are in Text1 of Column1 of Grid1, then click again on the grid and press delete.
4. Add more Image objects using step 1 and 2 above. Add as many as you need (in my case, I only wanted 4).
5. Put your desired picture on each images programmatically or by using Properties Window.
6. Go back to your target column using again properties window and set Sparse to .F.
7. Click on the DynamicCurrentControl. In my case, I have a table with the following structure:
PHONES.DBF
ContactName C(30)
Details M
IconUsed N(1,0)
Since I only desired 4 different icons to appear, in DynamicCurrentControl I placed this: ICASE(IconUsed=1,"Image1",IconUsed=2,"Image2",IconUsed=3,"Image3","Image4")
An alternative to that is to create a method within the form then do the switching there, let us say a method named ChangePix. Then in the DynamicCurrentControl, just type Thisform.ChangePix()
8. Create several records, place different values on IconUsed field and run the form.
There you are! A grid with different images.
Here is the picture of MyMobile v1.0. I am planning to do a sleeker version when I have time, one with icons inside the screen like the latest mobile phones these days plus making the keypad works. But for now, this works just fine.
I have attached the mobile zip below. Please extract it to C:\Samples. Enjoy!