1

Konu: Creating everything in code or not

In implementing the "Locatorgrid" of FoxyClasses, I have created a form in code, implemented Locatorgrid in code, and then started to add labels and textboxes/editboxes in code. That's when I discovered that placing labels and textboxes/editboxes (and other controls) is very time-consuming if you do it in code.

So, let me just ask, to get confirmation, whether it is not better to create a form in the Project Manager, implement any FoxyClasses control in code, and then use the Project Manager  again to add other controls?

Regards,

Hans L

2

Re: Creating everything in code or not

Hans,
Project Manager is normally is not part of the question. It is a container (yet another table) that keeps the files needed in a project together and that is all. I only need it to keep track of files in an executable and build an executable. Other than that I don't need it and can work with any file in it individually (and more often than not I use:

modify form mySomeForm

to modify a form rather than going into project manager).

Putting Project maganer aside, I think it is very hard to build a form totally in code (well I do that too but almost always just for the sake of I have been posting form samples on forums and code version is the easiest - otherwise I would need to zip scx/sct files and upload somewhere). Normally, like normal mortals I too do it visually and my forms are scx/sct. Visually using builders, drag & drop, and also using layout toolbar to align things it is much easier. Then if you haven't done it before comes the question "how do I add a locatorgrid in a form and format it"? Well first off, sorry I could never find time to create a visual builder for the locatorgrid. I did create one really but it was not end user quality I didn't release it. However still there some tricks:

1) Since I almost always use FoxyClasses classes in my real world projects I need them to be easily accessible. Go to Tools\Options, Controls tab and 'Visual Class libraries' marked, add all of the classes from FoxyClasses Classes folder (Click add and select those vcx'es one by one).
2) Now when I create or modify a form clicking books icon in "form controls" toolbar I get a list of those classes. Choose 'Grids' from there and toolbar is populated with the classes from grids.vcx. You can click Locatorgrid and then click on form to add one.

You can then set columncount to desired setting and edit widths, captions etc and mark lKeepFormat .T. There is another trick I use to make the process easier but unfortunately I don't have time to show it at this moment. Later I hope to create a video and send the link if I can.

3

Re: Creating everything in code or not

Well, Cetin, your answer dispels my uncertainty. I will go back to the form I am creating in my initial/training project and apply what you have said.

Thank you, and regards,

Hans L

4

Re: Creating everything in code or not

I now remember one of the reasons I wanted to create everything in code. In general, this is what has happened to me in the past. I have set the RecordSource in a container to a table and then, ControlSource in controls in the container to fields. Then, for whatever reason, if the RecordSource (table) was changed, the  value in ControlSource in all controls were deleted, and I had to add them again, manually.

How would I avoid having this happening?

Hans L

5

Re: Creating everything in code or not

If you do anything to recordsource then VFP 'rebuilds' the grid. It causes all formats get lost. To do that first recordsource to an empty string, do whatever with the source and set it back. You can find a sample in LocatorGrid's reload method (or MultiSelectGrid's reloadcursor). ie:

Visual Fox Pro
lcRecordSource = .RecordSource

.RecordSource = ''
* ... do whatever
.RecordSource = m.lcRecordSource

Also with .LKeepFormat .t. you could do this with something like:

Visual Fox Pro
with .myLocatorGrid

  .cSQL = '.... from ...'
  .Init() && running init actually building from scratch keeping the column formats
endwith

6

Re: Creating everything in code or not

Thanks, Cetin.

Hans L

7

Re: Creating everything in code or not

I have now created a form, dropped the locatorgrid on it, and done this:

Form Init:

Visual Fox Pro
DO twsdatacreation   &&  Creating cursor curFirstList: SELECT cname, centity, csubjmat, cnolang FROM tbtewese INTO CURSOR curFirstlist

THISFORM.set_up_list()
THISFORM.set_up_cmdgr()

Form method set_up_list:

Visual Fox Pro
WITH THISFORM.grdLocgrid

    .Height = 300
    .Top = 20
    .Visible = .T.
*
    .lKeepFormat = .T.       
    .ColumnCount = 4
    .cSQL = '* FROM curFirstList'
    .Init()
*
    .Columns(1).Header1.Caption = 'Name'
    .Columns(1).Width = 500
    .Columns(2).Header1.Caption = 'Entity'
    .Columns(2).Width = 200
    .Columns(3).Header1.Caption = 'Subject'
    .Columns(3).Width = 200
    .Columns(4).Header1.Caption = '# of languages'
    .Columns(4).Width = 200
 
    .SetAll('Fontbold', .T., 'Header')
    .SetAll('Alignment', 2, 'Header')
 
    .Width = .Columns(1).Width + .Columns(2).Width + .Columns(3).Width + .Columns(4).Width
    .Left = .Parent.Left + (.Parent.Width - .Width)/2
ENDWITH


Form method set_up_cmdgr: Sets up commandgroup

The grid and commandgroup are created as expected, except that there are *no* data in the grid.

What am I missing?

Thanks,

Hans L

8

Re: Creating everything in code or not

hopefully cetin didn't wake up
THISFORM.grdLocgrid.Recordsource="curFirstList"

Preferably (to be on the safe side)
start each grid's Init method with :
THISFORM.grdLocgrid.Recordsource=""
and then add the form's init method:
THISFORM.grdLocgrid.Recordsource="curFirstList"
---
or create data in the forms Load method ...

---
for the idea benind read in help: Event Sequence in Visual FoxPro

VFP9 SP2

9

Re: Creating everything in code or not

Hello, Konuka, and thanks for your answer.

Just to be sure I understand. You said

Preferably (to be on the safe side)
start each grid's Init method with :
THISFORM.grdLocgrid.Recordsource=""

If this were to be put in a grid's Init method, should it not be

THIS.Recordsource = ""  ?

Anfd when you say

and then add the form's init method:
THISFORM.grdLocgrid.Recordsource="curFirstList"


I assume you mean

and then add this code to the form's Init method:

Am I right?

Regards,

Hans L

10

Re: Creating everything in code or not

'then' was not the correct expression, I stressed that the sequent of events is playing the major role:
In a form events happen following order (our case among others):
-form.load
-form.object.init (in our case your grid)
-form.init
-form.activate

when you create the cursor in the form.init period, the grid's establishment is already done; in other words: the grid searched a cursor or open dbf, but didn't find - so the .Recordsource=""
---
if you create the cursor in the form's load event, afterwords the grid would find a cursor or open dbf in grid's init phase.
The disadvantage is that, if you have more than one open data source (a cursor or open dbf) the grid might catch the unwanted, and some preset properties such as columnwidth might change.

Therefore "prefereably" I used to assign in the founding phase of the grid 'nothing': grid's init--> myform.grid.Recordsource="" Even if there are cursors or dbfs the grids properties remain same.
And 'THEN' when the forms.init event work I assign the wanted cursor or open dbf: form's init -->
myform.grid.Recordsource="myCursorormyOpendbf"

hope to be clear ...

VFP9 SP2

11

Re: Creating everything in code or not

Hans L yazdı:

If this were to be put in a grid's Init method, should it not be
THIS.Recordsource = ""  ?    YES- same effect
....
I assume you mean
and then add this code to the form's Init method:
Am I right? YES maybe better expressed: and instead add this code to the form's Init method:


brief discussion above

VFP9 SP2

12

Re: Creating everything in code or not

Got it to work by making sure that the cursor opened before the grid was created.

Thank you, konuka!

Hans L