Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Combo Problem in a Grid
Message
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Titre:
Combo Problem in a Grid
Divers
Thread ID:
00638700
Message ID:
00638700
Vues:
66
I have a dropdown combo class that works correctly unless it is in a grid. Outside of the grid, I can add a new value and have this new value inserted into the MonFreq lookup table. Trying to do the same with the exact same combo in the grid will not allow me to add a new choice. The combo only accepts numeric values. The following code illustrates the problem. Is this really a bug or something I am doing wrong?
CLOSE ALL
CREATE TABLE monfreq (keyid I, descript c(30))
CREATE TABLE test (FrqID I)
INSERT INTO monfreq VALUES (1, '1/day')
INSERT INTO monfreq VALUES (2, '1/week')
INSERT INTO monfreq VALUES (3, '1/month')
INSERT INTO monfreq VALUES (4, '1/year')
INSERT INTO test VALUES ( 2 )

oForm = CREATEOBJECT("ComboForm")
oForm.Show(1)


DEFINE CLASS ComboForm AS Form


 Top = 0
 Left = 0
 Height = 374
 Width = 562
 DoCreate = .T.

 ADD OBJECT combo1 AS combo WITH ;
  BoundColumn = 2, ;
  ColumnCount = 2, ;
  ColumnWidths = "100,0", ;
  ControlSource = "test.frqid", ;
  Height = 24, ;
  ColumnLines = .F., ;
  Left = 93, ;
  Top = 21, ;
  Width = 198, ;
  BoundTo = .T., ;
  cRowSourceAlias = "MonFreq", ;
  cRowSourceField = "Descript", ;
  lUpdateRowSource = .T.

 ADD OBJECT gridcontrol1 AS grid WITH ;
  ColumnCount = 1, ;
  Height = 138, ;
  Left = 81, ;
  Panel = 1, ;
  RecordSource = "test", ;
  RowHeight = 24, ;
  Top = 99, ;
  Width = 261, ;
  Column1.ControlSource = "test.frqid", ;
  Column1.Width = 219, ;
  Column1.Sparse = .F., ;
  Column1.Name = "Column1"

 PROCEDURE Init
  WITH This.gridcontrol1.column1
   .AddObject('combo1', 'combo')
   .CurrentControl = 'combo1'
   WITH .Combo1
    .BoundColumn = 2
    .ColumnCount = 2
    .ColumnWidths = "100,0"
    .ColumnLines = .F.
    .Left = 72
    .SpecialEffect = 1
    .Top = 43
    .BorderStyle = 0
    .BoundTo = .T.
    .cRowSourceAlias = "MonFreq"
    .cRowSourceField = "Descript"
    .lUpdateRowSource = .T.
    .Visible = .t.
   ENDWITH
  ENDWITH
 ENDPROC
 PROCEDURE Load
  IF NOT USED('test')
   USE Test IN 0
  ENDIF
  SELECT Test
 ENDPROC
ENDDEFINE

DEFINE CLASS combo AS combobox

 ColumnCount = 1
 RowSourceType = 5
 RowSource = "This.aList"
 Height = 24
 SelectOnEntry = .T.
 Width = 100

 *-- The Alias of the table where the 
 *-- Combo Row Source is derived from.
 cRowSourceAlias = ("")
 *-- Field name in the RowSource table 
 *-- [cRowSourceAlias property] to populate with new values
 cRowSourceField = ("")
 *-- Set to TRUE if you want the table used to 
 *-- populate the RowSource to be updated with new values.
 lUpdateRowSource = .F.

 DIMENSION alist[1]

 PROCEDURE Init
  DODEFAULT()
  WITH THIS
   .mQuery()
   LOCAL lnI
   FOR lnI = 1 TO ALEN(.aList)
    IF VARTYPE(.aList[lnI]) = 'C'
     .aList[lnI] = ALLTRIM(.aList[lnI])
    ENDIF
   ENDFOR
   .REQUERY()
  ENDWITH
 ENDPROC
 PROCEDURE Valid
  LOCAL lnResult, lcOldValue, lcAlias, lcField
  WITH THIS
   lnResult   = 1
   lcOldValue = ALLTRIM(.DISPLAYVALUE)
   lcAlias    = .cRowSourceAlias
   lcField    = .cRowSourceField
   IF !EMPTY(lcOldValue)
    lnResult = ASCAN(.aList, lcOldValue)
    IF lnResult = 0
     .mAddItem(lcOldValue)
    ENDIF
   ENDIF
   .DISPLAYVALUE = lcOldValue
  ENDWITH
 ENDPROC
 PROCEDURE mQuery
  SELECT descript,keyid FROM MonFreq ;
     ORDER BY descript INTO ARRAY This.aList
 ENDPROC
 PROTECTED PROCEDURE mAddItem
  LPARAMETER lcItem
  LOCAL lnLen, lnWidth
  IF VARTYPE(lcItem) = 'C'
   lcItem = ALLTRIM(lcItem)
  ENDIF
  WITH THIS
   lnLen   = ALEN(.aList, 1) + 1
   lnWidth = ALEN(.aList, 2)
   IF lnWidth = 0
    DIMENSION .aList[lnLen]
    .aList[lnLen] = lcItem
   ELSE
    DIMENSION .aList[lnLen, lnWidth]
    .aList[lnLen, 1] = lcItem
   ENDIF
   IF .lUpdateRowSource
    THIS.mUpdateRowSource(lcItem)
   ENDIF
   .mFillColumns(lnLen)
   IF ALEN(.aList, 1) > 1
    =ASORT(.aList)
   ENDIF
   .REQUERY()
  ENDWITH
 ENDPROC
 *-- Called by mAddItem to add data to additional
 *-- columns of the array
 PROCEDURE mFillColumns
  lparameter lnRowID
  This.aList(lnRowID, 2) = MonFreq.KeyID
 ENDPROC
 *-- called by mAddItem to update the table specified 
 *-- in the cRowSourceAlias property
 PROTECTED PROCEDURE mUpdateRowSource
  lparameter tcItem
  local lcAlias, lcField, lnSelect
  lcAlias = alltrim(This.cRowSourceAlias)
  lcField = alltrim(This.cRowSourceField)
  if empty(lcAlias) or empty(lcField)
   return .f.
  endif
  if not used(lcAlias)
   use (lcAlias) again in 0
  endif
  if not This.mValidateField()
   return .f.
  endif
  lnSelect = select()
  select (lcAlias)
  locate for alltrim(upper(lcField)) == alltrim(upper(tcItem))
  if eof(lcAlias)
   insert into (lcAlias) ( (lcField) ) values ( tcItem )
  endif
  select (lnSelect)
  return eof(lcAlias)
 ENDPROC
 *-- Checks the cRowSourceAlias to see if the 
 *-- cRowSourceField actually exists
 PROTECTED PROCEDURE mValidateField
  local lcAlias, lcField, lnRetVal, laFields[1]
  lcAlias = alltrim(This.cRowSourceAlias)
  lcField = alltrim(This.cRowSourceField)
  lnRetVal = afields(laFields, lcAlias)
  lnRetVal = ascan(laFields, upper(lcField))
  return (lnRetVal > 0)
 ENDPROC
ENDDEFINE
*
*-- EndDefine: combo
**************************************************
Mark McCasland
Midlothian, TX USA
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform