Wiki Home

Adding Some Code To An Event Of AControl Which Has Been Put On The Form INRuntime

Namespace: SoftwareEng
I have a form and in the runtime add a control to form (an editbox) but I have to add some code to the Key Press() evnet. How do you reckon I could do that?

I have to mention that I am using VFP6.0.

Thanks For Your Help

Look at the following Example:

define class MyEditBox as EditBox        && Custom editbox class for Grid
  BorderStyle = 1
  SpecialEffect = 1
  Visible = .T.
  cDataSource = ""
  cReturnVal = ""
  cText = ""
  cFieldName = ""

  &&procedure init(cDS, cTxt, cFName)
  procedure init(cDS, cFName)
  	if pcount() = 2
  		this.cDataSource = alltrim(cDs)
  		this.cFieldName = alltrim(cFName)
  		this.cText = alltrim(cDs) + "." + cFName
  		&&messagebox("1" + this.cText)

  function DblClick
  	lcVal = this.cText
  	&&messagebox("3" + lcVal)
	cMemoTxt = ALLTRIM(&lcVal)
	do form frmMemoDisplay WITH this.cFieldName
	this.cReturnVal = cMemoTxt
	lcField = this.cDataSource + "." + this.cFieldName
	replace &lcField with cMemoTxt

  function getRetVal()

And we instantiate an object this way:
tcCurName = tableName or cursorName
this.grid1.recordSource = tcCurName
lnFieldsNo = afield(arrFieldSpec)
objName = "edtBox"
this.grid1.columns(i).addObject(objName, "MyEditBox", tcCurName, arrFieldSpec(i, 1))

Possible Solution
If you need to add multiple lines of code to the keypress event, include the following in your keypress event in the editbox class definition:
IF File('mykeypresscode.prg')
  DO mykepresscode.prg

Then, create your code on the fly, store it in the mykepresscode.prg file using STRTOFILE(), and it will run at the next keypress.

Ben Creighton
( Topic last updated: 2007.05.31 04:18:50 PM )