Three programming methods for Modal Dialogs (Modal Form)
Translated and modified by carol
Original: http://www.palmos.com/dev/support/docs/recipes/modal_dialogs.html
There are more discussion on TomPDA: http://www.tompda.com/bbs/display.asp?luntan=2&ForumId=1660213
From simple-up, in turn: 1) Use frMdodialog, do not need to write event handler (Event Handler) is the easiest, but flexibility is relatively poor
2) Use frMdodialog, write part of the event handler
3) Use frmpopupForm, write independent event handler, event loop function
According to the different complexity of Form / Dialog, choose different ways to handle, wait a half-time
Although the above three programming methods are different, their resource files are not different. They are all buffal to build MODAL FORMs. We have to do "Details" on Mainform, drive open Detailsform
Option 1: FRMDodialog that does not need to write event-driven
Add this paragraph in mainformhandleevent:
IF (eventp-> data.ctlselect.controlid == maindetailsButton) {fRMP = frMinitform (detailsform);
/ * Initialize your controls on the form here * / Add Form's control initialization
// Open the dialog, And Wait Until A Button Is Pressed to Close It. // This sentence frMdodialog opened Detailsform, and Form will returned out after you pressing Button, // The value returned whichButton tells you what Which button whichbutton = frmdodialog (frmp);
IF (WhichButton == Detailsokbutton) {/ * get data from controls on the form to save / apply change, based on different return values, get the change in Detailsform (FRMP); Handled = true;
Option 2: Modal Form with event processed frMdodialog can make developers depend on FRMDodialog handle most of the work, and handle some of the events that are more concerned. This method uses the same code as Option 1, just add the following sentence before frmdodialog call:
FRMSETEVENTHANDLER (FRMP, DetailSpartialeVentrandler);
FRMDodialog will call DetailsPartialeventHandler to handle your defined events before processing events.
static Boolean DetailsPartialEventHandler (EventPtr eventP) {Boolean handled = false; if ((eventP-> eType == ctlSelectEvent) && (eventP-> data.ctlSelect.controlID == DetailsOKButton)) {if (ValidationFailsOnTheInput ()) {FrmAlert (PleaseFixYourInputAlert ); // Tell frmdodialog we Handled this event, so it // shopn't do anything hand = true;}} Return Handled;
Note that if you want to communicate in this form, or to delay some background events, you should not use frMdodialog.frmdodialog, you can write less code, and these code is concentrated in the program. (Also see the original English text better: FrmDoDialog runs in its own event loop instead of yours, giving you slightly less control over event handling than you normally have If you are doing communications or are using event timeouts for some background processing or updating, FrmDoDialog. Is Not a Good Implementation for Your Dialog. The benefit of frmdodialog is this you need is located in innly one place in your application.)
Option 3: Handling using a complete independent event
This method has the maximum flexibility and the most complex, which can be processed as a usual form. Establish an event handler:
Static Boolean DetailsHandleEvent (EventPtr Eventp) {Boolean Handled = False; FormPtr FRMP;
Switch (Eventp-> ETYPE) {CASE frmopnevent: fmp = frMgetActiveform (); / * Initialize ui objects here * / / / / I also add a function to initialize, just like MainForminit (). FRMDrawform (FRMP); Handled = true; "BREAK;
case ctlSelectEvent: if (eventP-> data.ctlSelect.controlID == DetailsOKButton) {/ * save or apply the changes now * / FrmReturnToForm (0); handled = true; break;} else if (eventP-> data.ctlSelect. ControlID == detailscancelbutton) {/ * don't save change, just return. * / frmreturntoform (0); handled = true; break;} default: Break;
Return Handled;}
Note: To use frmreturntoform () to close this form and return to the previous activity FORM, so save data and other work, remember to complete in this handleevent (not in the MAIN FORM code).
The above function is called in ApphandleEvent:
Case Detailsform: frmseteventhandler (fRMP, DetailsHandleevent); Break;
Put it onto the Case Mainform: strip.
Finally, to display this form, use FRMPOPUPFORM when processing the Button event, instead of FrMDodialogfrmpopForm generates both events of LoadEvent and OpenEvent.
if (eventP-> data.ctlSelect.controlID == MainDetailsButton) {MenuEraseStatus (0); // in case the status is still active // Some apps might want to save the current focus here FrmPopupForm (DetailsForm);. handled = true }