Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: FoxPro FAQ

Classes and Objects

How to put grid on the form programmatically by TomasDill
Posted: 2 Feb 01

  Suggest Grid1 is a name of grid required to put onto the form, grid should contain 10 columns an display data from the MyAlias alias. Grid should contain a combo box in the second colunm with the name Combo1. Following sample routine will allow you to see a generic idea of how to put grid on the form programmatically and recreate it again when needed. You can make it as a custom method of the form and call it when needed (for example, thisform.MakeGrid or thisform.RefreshGrid). See also comments in the code.
  Note that when you need grid in some container or page of page frame, replace all thisform reference in routine by appropriate reference to the container, for example thisform -> thisform.PageFrame1.Page2.MyGridContainer
  Note also that you can use your own classes for grid and combobox, just change second parameter of the AddObject methods in the code to your class name.
  Routine demonstrates also a technique of screen locking for smooth refresh and how to check if object already exists.

local llFirstRun, llNotLockedHere
m.llNotLockedHere = thisform.LockScreen
m.llFirstRun = .T. && indicate that grid created first time
       && so we will not use lockscreen to
       && speed up form initialization

* test if grid is already on the form
if PEMSTATUS(thisform, 'Grid1', 5)
    m.llFirstRun = .F. && this is not a first run - form is visible
    * remove old grid object from form
    thisform.LockScreen = .T.
    * DOEVENTS && required only for case when following
               && code fail to remove grid properly
    thisform.Grid1.Visible = .F.

&& create grid
with thisform.Grid1
   && assign main thing - data source
    .RecordSource = "MyAlias"
    .ColumnCount = 10

&& define control sources - what each column will display
    .Column1.ControlSource = 'MyAlias.Field1'
    .Column2.ControlSource = 'MyAlias.Field2'
    .Column10.ControlSource = 'MyAlias.Field10'

&& define column widths
    .Column1.Width = 100
    .Column2.Width = 50

&& define column header captions
    .Column1.Header1.Caption = 'Field1Caption'
    .Column2.Header1.Caption = 'Field2Caption'

&& Sample of how to add custom object into column
    .Column2.Combo1.Visible = .T.
    .Column2.CurrentControl = 'Combo1'
      && remove original (default) control
      && NOTE: for header class do not need to
      && remove default header, it is removed automatically
      && when you add new header to keep only one
      && header in column at a time
    .Column2.Text1.Visible = .F.
      && define properties of combobox in column
    with .Column2.Combo1
        .ColumnCount = 2
        .ColumnWidths = 30,50

     && Don't forget to show grid ;)
    .Visible = .T.

&& unlock screen if locked here
if !m.llFirstRun
    thisform.LockScreen = m.llNotLockedHere

Back to Microsoft: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close