Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Columnwidth of a grid
Message
From
07/12/2001 03:57:51
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
06/12/2001 13:56:59
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00590438
Message ID:
00591015
Views:
23
>Hallo Cetin,
>
>Ron did not tell you that we made the grid by using the designer.
>The number columns is set to -1.
>We just want to connect a variable table to the grid, not allways the same layout, not allways the same structure.
>When we put the columnscount to 10 f.i., we can get the value of the first field back.
>
>(i am a collegue of Ron)
>Thanks for your cooperation and help (we need it.....)
>
>ad

Ad,
Then you have to choose one of the options :
1)Instead of default textbox use your own subclassed textbox. To do it after assigning recordsource (with columns=-1, reset as I understood), you traverse the columns and removeobject text1, addobject yourtxtbox and set currentcontrol to it with sparse = .f.
*A custom method
lparameters toGrid,tcNewSource
with toGrid
  .RecordSource=""
  .Columncount = -1
  .Recordsource = tcNewSource
  for each oCol in .Columns
    for each oControl in oCol.Controls && Actually directly controls(2) would do
        if upper(oControl.Baseclass) = "TEXTBOX"
           oCol.RemoveObject(oControl.Name)
           oCol.NewObject('Text1','myTextBox','myClassLib.vcx')
           oCol.CurrentControl = 'Text1' && This and next is not really needed
           oCol.Sparse = .f.
           exit
        endif
    endfor
  endfor
endwith
If you download 'two grid classes' from download area multiselectgrid has similar code changing header and textbox on the fly.

2) There is another trick to only change needed columns but it's too complex to explain here.

3) Use click transparency. From download area you could get Nick Neklioudov's GridHighlighter and I think the click transparency article is on advisor site.
Basic idea is to cover the grid with a transparent container, container gets the mouse events first and if you need you propagate it to underlying grid control of your choice.

4) Use a .prg based grid class that builds itself on the fly with the given cursor. This one is really my preference when I need my grid to do things more than usual. Here is an example (beware it's powerfull but sample is fancy and you might thing it's useless - rightclick is only applied to 3rd column :)
Why my preference ? Well this one is easy and flexible and lets you define a grid at runtime provided you use VFP6 and later (VFP5 needs precompiled). You could just keep the skeleton and fill in the blanks at run time :)
Use products in 0
Use employee in 0
oForm  = createobject('myForm')
With oForm
  .Addobject('myGrid','myGrid')
  .Addobject('myButton','myButton')
  .MyButton.visible = .t.
  With .myGrid
    .Top=oForm.MyButton.Height + 10
    .Left = 10
    .Height = oForm.Height - (.Top+20)
    .Width = oForm.Width - 20
    .SetSource('products')
    .Visible = .t.
  Endwith
  .Show
Endwith
Read events

Define class myForm as Form
  Height = 400
  Width = 600
  ShowTips = .T.
  Procedure SampleShortCut
  Lparameters oTarget
Define POPUP shortcut shortcut RELATIVE FROM MROW(),MCOL()
Define BAR _MED_CUT OF shortcut PROMPT "Cut"
Define BAR _MED_COPY OF shortcut PROMPT "Copy"
Define BAR _MED_PASTE OF shortcut PROMPT "Paste"
Define BAR 4 OF shortcut PROMPT "Copy Object value"
Define BAR 5 OF shortcut PROMPT "Change column color"
On SELECTION BAR 4 OF shortcut _cliptext = oTarget.Value && Alternative copy
On SELECTION BAR 5 OF shortcut oTarget.SwitchColor()
Activate POPUP shortcut
Endproc

  Procedure queryunload
  Clear events
Endproc
Enddefine

Define class MyButton as CommandButton
  Caption = 'Switch source'
  Procedure click
  With thisform.myGrid
    .SetSource(iif(upper(.Recordsource)='EMPLOYEE','products','employee'))
  Endwith
Endproc
Enddefine

Define class myGrid as grid
  DeleteMark = .F.
  ReadOnly = .T.
  RecordMark = .F.
  ScrollBars = 3
  SplitBar = .F.
  Highlight = .F.
  HighlightRow = .F.
  Name = "grdMyGrid"
  FreeTable = .t.

  Procedure addcolumn
  Lparameters nIndex, cAlias, cField, cCaption
  Nodefault
  This.addobject("clm"+cField,"myColumn", cAlias+"."+cField,nIndex)
Endproc

  Procedure SetSource
  Lparameters tcRecordsource
  With this
    .recordsource = ''
    .Columncount = -1
    .recordsource = tcRecordsource
    .FreeTable = empty(cursorgetprop("Database",tcRecordsource))
    If !.FreeTable
      Open database (cursorgetprop("Database",tcRecordsource))
    Endif
    nOldColCount = .columncount
    For ix = 1 to fcount(tcRecordsource)
      .AddColumn(ix, tcRecordsource,field(ix,tcRecordsource))
    Endfor
    For ix = nOldColCount to 1 step -1
      .RemoveObject(.Columns(ix).name)
    Endfor
    .SetAll('Visible',.t.)
    .SetFocus()
  Endwith
Endproc
Enddefine

Define class myColumn as column
  Resizable = .F.
  Movable = .F.
  Procedure init
  Lparameters cControlSource, nIndex
  With this
    .controlsource = cControlSource
    .ColumnOrder = nIndex
    .RemoveObject('Header1')
    .Addobject('myHeader','myHeader')
    If type(cControlSource)='L'
      .AddObject("myControl","myGridChkBox")
    Else
      .AddObject("myControl","myGridTxtBox")
    Endif
    .CurrentControl = "myControl"
    .Sparse = .F.
  Endwith
Endproc
  Procedure resize
  This.myControl.Resize
Endproc

  Procedure MouseMove
  Lparameters nButton, nShift, nXCoord, nYCoord
  With this.Parent
    lnActiveRow = ceiling( ;
      ( nYCoord - (.top + .headerheight) ) / .rowheight )
    lnActivecol = this.Columnorder - this.Parent.LeftColumn + 1
    .ActivateCell(lnActiveRow,lnActivecol)
  Endwith
  With this
    If type(.controlsource)$'CM'
      .myControl.Tooltiptext = ;
        iif(type(.controlsource)='C',  eval(.Controlsource),;
        iif(type(.controlsource)='M',  mline(eval(.Controlsource),1),''))
    Endif
  Endwith
Endproc
Enddefine

Define class myGridTxtBox as TextBox
  Name = "Text1"
  Procedure init
  This.BorderStyle = 0
Endproc
  Procedure click
  This.setfocus()
Endproc
  Procedure SwitchColor
  With this.Parent
    .DynamicBackColor = ;
      'iif('+.Controlsource+'="M",'+str(int(rand()*0xFFFFFF))+','+str(.BackColor)+')'
    .Parent.Refresh
  Endwith
Endproc

  Procedure Rightclick
  If this.parent.Columnorder = 3
*Do myshortcut.mpr with this
    Thisform.SampleShortCut(this)
  Endif
Endproc

Enddefine

Define class myGridChkBox as Container
  Width = 14
  Height = 17
  BackStyle = 0
  BorderWidth = 0
  Name = "grdcheckbox"
  Add OBJECT check1 AS checkbox WITH ;
    Top = 0, ;
    Left = 0, ;
    Height = 17, ;
    Width = 13, ;
    BackStyle = 0, ;
    Caption = "", ;
    Name = "Check1"
  Procedure Init
  With this
    .check1.Controlsource = .parent.controlsource
    .Resize()
  Endwith
Endproc
  Procedure Resize
  With this.check1
    .Left = (this.Parent.Width - .Width)/2
  Endwith
Endproc
Enddefine


Define class myHeader as Header
  BackColor = 0
  ForeColor = rgb(255,255,0)
  Procedure init
  With this
    If !.parent.parent.FreeTable and ;
        !empty(dbgetprop(.parent.controlsource,"Field","Caption"))
      .Caption = dbgetprop(.parent.controlsource,"Field","Caption")
    Else
      .Caption = substr(.parent.controlsource,at('.',.parent.controlsource)+1)
    Endif
  Endwith
Endproc
Enddefine
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform