Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling form events

Status
Not open for further replies.

steve4king

IS-IT--Management
Feb 6, 2007
154
US
I've got a form1 that calls a process, which hides form1(to display an external form) then alters form1's values, then returns to form1.
I need to set the focus on a specific field on the form prior to returning.

However, setting focus displays an "alias 'thisform' is not found" error.

I believe this is due to an event on the form firing while the form is inactive.
Is it possible to just disable certain/all events while I move the focus then re-enable it?

Is there a better way to handle this?
(note that the form behavior is legacy code and is pivotal to the application. So I want to encapsulate changes as much as possible)
 
I need to set the focus on a specific field on the form prior to returning.

Actually you don't set the focus prior to returning.

Instead you set the focus after to returning so that thisform.<whatever> exists, but prior to showing.

Code:
* --- HIDE The current Form ---
THISFORM.Hide

* --- Go To the Other Form ---
DO FORM "OtherForm"

* --- Here you have Returned from the OtherForm ---
<set your focus here [u]after to returning[/u] from the OtherForm>
< and do whatever else you need to do to the first form >

* --- Now SHOW This Form ---
ThisForm.Show
<do whatever>

Good Luck,
JRB-Bldr



 
Well, your process (whatever that is, could you describe?) does address form1 in some way. Does it do that via "thisform"? If so, then this code must be a form method of that form. If not, you cannot address the form1 via thisform, of course. If your process is a function in a prg you need to pass in a reference to the form, so the prg code can address the form. And you would already have that code to pass on a form referencee and work with it, as your process is hiding form1, setting values and showing it again.

Could you perhaps show a portion of that code? That would be helpful to help.

Bye, Olaf.
 
Another approach would be to store a reference to Form 1 at the time that you first launch it:

Code:
DO FORM Form1 NAME oForm1

Then, when setting focus to the specific control in form 1, you could do this:

Code:
oForm.SomeField.SetFocus

Of course, oForm would have to remain in scope when the above code is executed. A quick-and-dirty way of achieving that would be to make it a public variable. If you don't like that idea, there are other ways of doing it, but this isn't really the important issue here.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks for the replies. I actually found a workaround where I don't have to set the field.. I just disconnect the ControlSource after disabling the field.

It's not perfect since I'd still prefer to move the focus as well. JRB, your suggestion is probably the simplest.

Mike and Olaf, the prg is actually called including thisform as a reference, but there is an odd phenomenon where the form events still fire, though it's references don't seem to be accessible to itself..


Example:
(Legacy code starts at OK button on form.. it jumps through a few hoops before it gets to the code I'm working in. It returns to the form if the approved amount is less than the expected amount.)

prg
Code:
Function LegacyFunction
  Lparams oThisForm,Amount,Type
  local nAprAmt as number, sType as string, nShortAmt as number, oErr as exception, bResult as boolean
  bResult = .F.

  Try

    SET CLASSLIB TO mywrapper 
    LOCAL mytrans1 as mytrans OF mywrapper
    mytrans1 = CREATEOBJECT("mytrans")

    mytrans1.Amount = Amount       && takes either string or numeric
    myTrans1.Type   = Type        && identifies and converts the type using an assign method
    
    myTrans1.Process()             && takes user input and determines the approved amount and determines the final type 
    
    nType     = myTrans1.Type
    nAprAmt   = myTrans1.AprovedAmt
    nShortAmt = Amount - nAprAmt    

    oThisForm.Type1.Value = alltrim(str(nType,9,0))

    if (nShortAmt = 0)
       bResult = .T.
       
    else 
      bResult                       = .F.
      oThisForm.Amount1.Value       = nAprAmt
      oThisForm.Amount2.Value       = oThisForm.Amount2.Value + nShortAmt
      oThisForm.Amount1.Enabled     = .F.
      oThisForm.Type1.Enabled       = .F. && 
      oThisForm.Type1.ControlSource = 0 && workaround to prevent type from changing when user changes focus
      *oThisForm.Amount2.SetFocus() && causes "unrecognized alias 'thisform'" error
    endif

  Catch to oErr
    MessageBox(oErr.Message)
  Endtry

  Return bResult
endfunc
 
What control base class is "Type1"?
No matter what it is, one thing already is for sure: ControlSource is a string type property, you can't set it to 0.

>oThisForm.Amount2.SetFocus() && causes "unrecognized alias 'thisform'" error
Well, the reason for that error can't be this liune, it has to be searched in the Amount2 control. Setting focus to it triggers something wrongly referencing Thisform. Unfortunately this is hard to debug, because if you set a breakpoint right before setting focus to Amount2, and you step into the Setfocus method, the form and Amount2 control gets the focus, but also loses it again, as the debugger get's active again.

For sure this has nothiung to do with the function or oThisform.

Bye, Olaf.
 
If nothing else, expanding your MessageBox may help a little:
Code:
   MESSAGEBOX("Error number: " + TRANSFORM(oError.ERRORNO)  + CHR(13) + CHR(10) + ;
              "Procedure: " + oError.Procedure + CHR(13) + CHR(10) + ;
              "Message: " + oError.MESSAGE   + CHR(13) + CHR(10)   + ;
              "Line of code: " + oerror.LineContents + CHR(13) + CHR(10) + ;
              "Details: " + oerror.details + CHR(13) + CHR(10) , 16, "Error")


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top