Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Array in grid
Message
From
02/08/2008 12:24:29
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
 
 
To
01/08/2008 10:50:41
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
01335805
Message ID:
01336090
Views:
23
>Hi All,
>
>Is it possible to use an array as a recordsource in a grid?
>If so, how would I go about doing that?

No, because generally you can't be sure that the elements are of the same type within a column (or even a row). Within an array, any element can be anything.

Which is why the code below wasn't updated in quite a long time, and wasn't used much either. If you feel like completing it, be my guest (and put it in downloads here when you're done), though I doubt it would cover too many cases (objects, binary content, foreign keys into lookups) that can appear in an array. Also, dearly needed, scrolling.

Don't forget to pass parameter by reference, prefixed with @.
* array grid - actually created on the fly, one textbox per array element.
* v1.0,	4. aug 1999 - just got it to work, size the textboxes and look decently.
*[2007/06/28 17:37:37] dragan - added the parameter

* Author: Dragan Nedeljkovic
*	Alas LTD, Zrenjanin, Yugoslavia

PARAMETERS taArray

#define c_clib ""
#define c_textbox 'textbox'
#define c_checkbox 'checkbox'
#define c_editbox 'editbox'

if !empty(c_clib)
	set classlib to c_clib addi
endif



oArrForm=createobj("form")
oArrForm.ShowTips=.t.
oArrForm.addobject("oACont", "ArrCont", "taArray")
with oArrForm
	.height=min(.oACont.height, _screen.height-100)
	.width=min(.oACont.width, _screen.height-100)
endwith
with oArrForm.oACont
	.top=0
	.left=0
	.visible=.t.
endwith
oArrForm.show(1)
* read events
* disp memo like taArray


define class ArrCont as Container
	Name = "ArrCont"
* one textbox for each array element
	DIMENSION aobj[1]
	DoCreate = .T.
* copy the parameter here
	cArrayName=""
	nRows=0
	nCols=0
	NextRow=0
	NextCol=0
	lClicked=.f.

	PROCEDURE Init
		lPara laArray
		SET STEP ON
		this.cArrayName=laArray
		local i,j, nColWidths[1], lcSource, ;
			nMaxWidth, nMaxHeight, nLastLeft, nLastTop
		This.nRows=alen((laArray),1)
		This.nCols=alen((laArray),2)
		? this.nrows, this.ncols, "rows, cols"
		dime this.aObj[This.nRows, This.nCols]
		this.aObj=.null.
		for i=1 to This.nRows
			for j=1 to This.nCols
* I had some plan for different objects for different sorts of data, like checkboxes for logicals
* but a plain old textbox is quite ok for most of the stuff
				cType=type(laArray+'[i,j]')
				do case
					case inlist(cType, 'C', 'N', 'Y', 'D', 'T')
						lClass='Atextbox'
					case cType='L'
						lClass='Acheckbox'
					other
						lClass='Atextbox'
				endc
				this.addobject("This.aobj[i,j]", lClass,i,j)
				with this.aobj[i,j]
* we could put lcCSource=laArray+"["+allt(str(i))+','+allt(str(j))+"]" just the same and
* omit the ThisForm.aArray altogether
					lcCSource=laArray+"["+allt(str(i))+','+allt(str(j))+"]"
					.controlsource=lcCSource
					.ToolTipText='['+trans(i)+','+trans(j)+']'
					if cType#'L'
	* this doesn't work as nice with dates, don't know why:
						.width=thisform.textwidth(.text)+4
					else
						.width=12
					endif
				endwith
			endfor
		endfor

		nLastLeft=2
		for j=1 to this.nCols
* position the columns
* make all the textboxes in the same column same width, calculate max first:
			nMaxWidth=0
			for i=1 to this.nRows
				with this.aobj[i,j]
					.left=nLastLeft
					nMaxWidth=max(nMaxWidth, .width)
				endwith
			endfor
* then apply this max to them
			for i=1 to this.nRows
				this.aobj[i,j].width=nMaxWidth
			endfor
			nLastLeft=nLastLeft+nMaxWidth+2
		endfor
* calculate vertical positions
* I'm not trying to align them horizontally - if we apply some other controls this
* may become impossible. Make them visible, this is the last round.
		nLastTop=2
		for i=1 to this.nRows
			nMaxHeight=0
			for j=1 to This.nCols
				with this.aobj[i,j]
					.top=nLastTop
					nMaxHeight=max(nMaxHeight, .height)
					.visible=.t.
				endwith
			endfor
			nLastTop=nLastTop+nMaxHeight
		endfor
* resize the form (or whatever container we base this on). We may check for some
* max values and apply some scrollbars if necessary - left for next version.
		with this
			.width=nLastLeft
			.height=nLastTop
		endwith
	ENDPROC
	
	Proc MemberLostFocus
		lParam oThis
		local nNewRow, nNewCol
		nNewRow=oThis.nRow
		nNewCol=oThis.nCol
		NoDefault
		lnLastKey=lastkey()
		do case
			* clicked elsewhere
			case abs(This.NextRow-oThis.nRow)>1 or abs(This.NextCol-oThis.nCol)>1 
				nNewRow=This.NextRow
				nNewCol=This.NextCol
				store 0 to This.NextCol, This.NextRow
			* home, leftarrow, backtab
			case inList(lnLastkey, 1, 19, 15)
				nNewCol=iif(nNewCol= 1, This.ncols, nNewCol-1)
			* dnArrow
			case inlist(lnLastKey, 24)
				 nNewRow=iif(nNewRow=This.nrows,  1, nNewRow+1)
			* upArrow
			case inlist(lnLastKey, 5)
				 nNewRow=iif(nNewRow= 1, This.nrows, nNewRow-1)
			* end, rightarrow, enter, tab
			Other
				nNewCol=iif(nNewCol=This.ncols, 1, nNewCol+1)
		endcase
		This.aObj[nNewRow, nNewCol].setfocus
	endproc

	Proc MemberWhen
		lPara oThis
		This.nextRow=othis.nRow
		This.NextCol=othis.nCol
	endproc


enddef

Define Class ATextbox as c_textbox
	nRow=0
	nCol=0
	Proc Init
		LPara lnRow, lnCol
		this.nrow=lnRow
		this.ncol=lnCol
		dodefault()
	endproc
	
	proc When
		return(this.parent.memberwhen(this))
	endproc
	
	proc LostFocus
		this.parent.MemberLostFocus(this)
	endproc

enddefine

Define Class ACheckbox as c_checkbox
	nRow=0
	nCol=0
	Proc Init
		LPara lnRow, lnCol
		this.nrow=lnRow
		this.ncol=lnCol
		dodefault()
	endproc
	
	proc When
		return(this.parent.memberwhen(this))
	endproc
	
	proc LostFocus
		this.parent.MemberLostFocus(this)
	endproc

enddefine

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Reply
Map
View

Click here to load this message in the networking platform