| | | | Browse by category |
Trap the enter key in any zComboBox (portable solution).
Cause
Pressing the enter key needs to react the same as pressing a go button.
Action
//============================================================================
// Code from sample app that traps enter key in ANY zComboBox
// Also: is portable.
// 1. zFactory - put zComboBoxFull (or any other) on dialog
// 2. Choose source code for setFocus and killFocus events.
// 3. Generate code - add the following (total 15 lines).
//============================================================================
// How it works:
// Enter is an accelerator for OK button
// Intercept the message when the OK button is pressed (setCommand)
// Find out who had the focus last
// If Combobox has focus: Enter key must have been pressed.
// If OK button has focus: OK button was pressed.
//============================================================================
// In HPP file: (Note all code not marked by //<============== is factory
// generated).
class DDialog2 : public zFormDialog {
zEditLine *pEdit1, *pEdit2;
zComboBoxBase *pComboBox2;
zButton *pOK, *pButton1;
public:
zString _Edit1, _Edit2;
zString _ComboBox2;
DDialog2(zWindow *, const zResId&);
int setFocusComboBox2(zFocusEvt*);
int killFocusComboBox2(zFocusEvt*);
// zpb_begin DDialog2Class
int cmdIDOK ( zCommandEvt*); //<==============
BOOL _gotFocus; //<==============
// zpb_end
};
//============================================================================
// In CPP file:
DDialog2::DDialog2(zWindow *w,const zResId& rid) : zFormDialog(w,rid) {
// zpb_begin DDialog2Constructor2
// zpb_end
pEdit1 = ZNEW zEditLine(this, ID_EDIT1, &_Edit1, 0);
pEdit2 = ZNEW zEditLine(this, ID_EDIT2, &_Edit2, 0);
pComboBox2 = ZNEW zComboBoxFull(this, ID_COMBOBOX2, &_ComboBox2);
pComboBox2->add(zString(zResId(IDS_DIALOG2COMBOBOX2_1)));
pComboBox2->add(zString(zResId(IDS_DIALOG2COMBOBOX2_2)));
pComboBox2->add(zString(zResId(IDS_DIALOG2COMBOBOX2_3)));
pComboBox2->add(zString(zResId(IDS_DIALOG2COMBOBOX2_4)));
pComboBox2->setNotifySetFocus(this,
(NotifyProc)(NotifyFocusProc)&DDialog2::setFocusComboBox2);
pComboBox2->setNotifyKillFocus(this,
(NotifyProc)(NotifyFocusProc)&DDialog2::killFocusComboBox2);
// zpb_begin DDialog2Constructor
pOK = ZNEW zDefPushButton(this, IDOK); //<=========
pOK -> setCommand( this, (CommandProc)&DDialog2::cmdIDOK ); //<=========
_gotFocus = 0; //<=========
// zpb_end
show();
}
int DDialog2::setFocusComboBox2(zFocusEvt* ev) {
// zpb_begin DDialog2setFocusComboBox2
_gotFocus = 1; //<=========
// zpb_end
return 0;
}
int DDialog2::killFocusComboBox2(zFocusEvt* ev) {
// zpb_begin DDialog2killFocusComboBox2
_gotFocus = 0; //<=========
// zpb_end
return 0;
}
//============================================================================
// This function defined by programmer (not factory)
// zpb_begin DDialog2MemberFunctions
int DDialog2::cmdIDOK ( zCommandEvt*) {
if( _gotFocus ) {
_gotFocus = 0;
//============ do your Enter key processing here =====================
delete new zMessage( this, Enter key trapped!, MyComboBox, MB_OK );
return 1;
}
return 0;
}
// zpb_end
If you would like to see a different solution for strictly for MOTIF/UNIX environments click here