简介
本教程为您在Ultimate Grid中内容的编辑提供指导,还将展示如何在网格中实现掩码编辑。
第1步– 在OnSetup()内对网格进行设置
在MyCug::OnSetup()中对您的网格进行设置:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void
MyCug::OnSetup() { /* each cell has a default edit and masked edit control If a cell typeuses a masked edit control then the masked edit is used, otherwise the cell'sdefault edit control is used */ SetNumberCols( 3 ); SetNumberRows( 8 ); // 列0 (last name) ** 配置顶部标题栏 ** QuickSetText( 0 , - 1 , "Last Name" ); SetColWidth( 0 , 120 ); // 列1 (initials) ** 配置顶部标题栏 ** QuickSetText( 1 , - 1 , "Initials" ); SetColWidth( 1 , 80 ); CUGCell cell; GetColDefault( 1 , &cell ); // 只允许列1输入2个字符 cell.SetMask( "aa" ); SetColDefault( 1 , &cell ); // 列2 (phone number) **配置顶部标题栏** QuickSetText( 2 , - 1 , "Phone Number" ); SetColWidth( 2 , 200 ); GetColDefault( 2 , &cell ); // 只允许列2按格式输入电话号码 cell.SetMask( "(000)000-0000" ); SetColDefault( 2 , &cell ); } |
第2步– 调用StartEdit()和配置OnEditStart()
要在网格中启动编辑“会话”,你必须调用startEdit()函数。startEdit()函数的作用是确定网格中单元格的位置,然后在那个位置显示编辑控件。默认情况下,网格的标准编辑控件是派生于标准CEdit的CUGEdit类。当然了,Ultimate Grid是能够承载从CWnd派生的任何编辑控件。startEdit()函数也调用OnEditStart()消息函数。此消息除了有别的用途之外,还让你指定你想用的编辑控件。
如OnSetup()函数所述,我们想让列1、列2的单元格为掩码编辑控件,在OnEditStart()函数内的代码可以做到。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/************************************************************************************/
void MyCug::OnCharDown(UINT *vcKey, BOOLprocessed) { /* startediting when the user hits a character key on a cell in the grid. Pass that key to the edit control, so it doesn't get lost */ StartEdit( *vcKey ); } int MyCug::OnEditStart( int col, longrow, CWnd **edit ) { /*mask editcontrol will be set once a cell has mask string set*/ /* wealways want the initials to be in uppercase, modify the style of the edit control itself */ if ( col == 1 ) (*edit)->ModifyStyle( 0 , ES_UPPERCASE ); else (*edit)->ModifyStyle(ES_UPPERCASE, 0 ); return TRUE; } |
第3步– 在OnEditVerify()和OnEditFinish()内编写校验代码
在OnEditVerify()和OnEditFinish()两个函数中,可以在用户向网格中输入数据时进行验证。
OnEditVerify() 函数被调用是在编辑控件获得焦点、字符键被按下时。这可以在用户输入的早期对数据进行验证。
OnEditFinish() 函数被调用是在编辑控件失去焦点转移到另外的窗口时。这可以对用户输入的整个的字符串进行最后的验证。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/**************************************************************************************/
int MyCug::OnEditVerify( int col, longrow, CWnd *edit, UINT *vcKey ) { if ( col == 0 ) { /* numbers are not allowed in the last name field */ if ( *vcKey >= '0' && *vcKey <= '9' ) return FALSE; } // allowthe edit control to process this character return TRUE; } int MyCug::OnEditFinish( int col, longrow, CWnd *edit, LPCTSTR string, BOOL cancelFlag ) { if ( col == 0 ) { // checkif user has actually entered a value if ( string == "" ) { MessageBox( "Sorry, but you must enter a value" ); // force userto continue edit and enter a value return FALSE; } } //otherwiseeverything is okay return TRUE; } |
第4步– OnEditContinue()函数
OnEditContinue()函数被调用是在用户从一个编辑控件失去焦点到下一个新的窗口获得焦点编辑时,即用户输入数据后按TAB键把光标移到下一个单元格。OnEditContinue()消息函数在OnEditFinish()函数后被调用,并告知来自哪个行或列,提供到哪个新行或新列的指针,因此你可以通过修改*newcol和*newrow这两个指针来改变。
默认情况下,网格中的焦点将在当前行或列保持循环往返。如果你在第i列的最后一行,你按下Enter键,那么焦点将保留在第i列并回到第一行。如果你是在最后一栏,并在j行,那么,如果你打TAB键,你会回到第0列,但仍保持在j行上。OnEditContinue()函数让你改变这种行为。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
int
MyCug::OnEditContinue(
int
oldcol,
long
oldrow,
int
*newcol,
long
*newrow ) { /* implement wrapping so when we hitthe last column, we move down a row and vice versa... i.e. if we're in the first column and we move backwards (ie SHIFT-Tab) thenmove up a row move down a row if we have thissituation */ if ( oldcol == GetNumberCols() && *newcol == 0 ) { /* ifwe're in the last row then wrap up to the first cell of the grid */ if ( *newrow == GetNumberRows() - 1 ) { *newrow = 0 ; *newcol = 0 ; } else ++*newrow; } /* move upa row if we're going backwards (SHIFT-Tab) */ else if ( oldcol == 0 && *newcol == GetNumberCols()) { /* go tothe last cell if we're in the first cell */ if ( *newrow == 0 ) { *newrow = GetNumberRows() - 1 ; *newcol = GetNumberCols() - 1 ; } else --*newrow; } /* allow the edit to continue, if wewanted to stop editing from continuing in the next cell, we'd return FALSE */ return TRUE; } |