When adding a variable for the edit control, you can specify the maximum allowable number of characters the user can enter. If the user attempts exceeding this maximum, an ON_EN_MAXTTEXT event is sent. This event is implemented through the OnMaxtext() method. You can catch this event to let the user know about the problem. If you set up the Maximum Characters fine, you do not need to perform any "if" or "while" checking. The edit control would do it itself. The OnErrSpace() event is event is sent if the compiler encountered a problem when attempting to allocate memory space to the control. This event is generated from the ON_EN_ERRSPACE message.
An edit box can be used to request information from the user. If you want to prevent the user from changing the value of an edit box, you can make it read-only. This is taken care of by setting the Read-Only property to True or checking its check box. To do this programmatically, call the CEdit::SetReadOnly() method.
If an edit box is not read-only, that is, if it allows the user to change its value, the user must first give it focus. When an edit box has focus, it displays a blinking caret. By default, the caret is an I beam. If you want to use a different caret, you have various options. You can change the caret from an I beam to a wider or taller gray caret by calling the CWnd::CreateGrayCaret() method. Its syntax is: void CreateGrayCaret(int nWidth, int nHeight); This method allows you to specify a width and a height for a gray blinking caret. After creating the caret, to display it, call the CWnd::ShowCaret() method. Its syntax is: void ShowCaret( ); Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_Username.ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } The above caret appears gray. If you want the caret to be completely black, you can call the CWnd::CreateSolidCaret() method. Its syntax is: void CreateSolidCaret( int nWidth, int nHeight ); This method creates a rectangular caret of nWidth x nHeight dimensions. Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_Username.CreateSolidCaret(5, 15); m_Username.ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } To provide a better designed caret, you can call the CWnd::CreateCaret() method. Its syntax is: void CreateCaret(CBitmap* pBitmap); Before calling this method, you can first design a bitmap, load it, and then pass it as the pBitmap argument. After initializing and loading the bitmap, to display it in the edit box, call the CWnd::ShowCaret() method. Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CEdit *edtBookTitle; CBitmap *pBitmap = new CBitmap; edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE)); pBitmap->LoadBitmap(IDB_BMP_CARET); edtBookTitle->CreateCaret(pBitmap); edtBookTitle->ShowCaret(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } If an edit box is editable and the user starts typing in it, the new characters would display. As the user is typing, the caret moves from left to right (for US English). The user can also move the caret back and forth using the Backspace, Delete, Home, End, or the arrow keys. At any time you can find out the current position of the caret by calling the CWnd::GetCaretPos() method. Its syntax is: static CPoint PASCAL GetCaretPos(); This method retrieves the left (x) and bottom (y) coordinates of the caret in the control that called it. These coordinates represent the member variables of a CPoint that this method returns. The measures are relative to the control and not the control's parent. Here is an example: void CExerciseDlg::CaretPosition(void) { CEdit *edtBookTitle; CStatic *stcCaretPos; CPoint CrtPos; char Msg[40]; edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE)); stcCaretPos = reinterpret_cast<CStatic *>(GetDlgItem(IDC_CARET_POS)); CrtPos = edtBookTitle->GetCaretPos(); sprintf(Msg, "Caret Position: (%d, %d)", CrtPos.x, CrtPos.y); stcCaretPos->SetWindowText(Msg); }
If want to hide the characters that display in an edit box, you can set the Password property to True. To do this programmatically, add the ES_PASSWORD style to the edit control. This style makes the edit control displays each character, or changes each one of its characters into an asterisk. If you prefer another symbol for the password style, call the CEdit::SetPassword() method. Its syntax is: void SetPasswordChar(TCHAR ch); This method takes as argument the character that you want to use. If an edit box is configured to display an asterisk character and you want to find out what that character is, call the CEdit::GetPasswordChar(). Its syntax is: TCHAR GetPasswordChar() const; This method takes no argument but returns the symbol used to mask the characters of a password-configured edit box.
By default, an edit box is configure to display or receive text of any alphabetic and non-alphabetic character. The alphabetical letters are received by their case and kept like that, uppercase and lowercase. If you want to convert an edit box' characters to uppercase, set the Uppercase property to True or programmatically add the ES_UPPERCASE style. In the same way, to convert the characters to lowercase, either at design time set the Lowercase property to True or programmatically add the ES_LOWERCASE style. The non-alphabetical characters are not treated by their case.
If you want, you can configure the edit box to allow only numeric characters. This is done by setting the Number property to True.
By default, the characters entered in an edit box start their positioning to the left, which is the default for regular text. You can align its value to the center or the right by selecting the desired value from the Align Text combo box. Any of these three alignment modes can also be set programmatically by adding either the ES_LEFT, the ES_CENTER, or the ES_RIGHT style.
By default, an edit box is used to display a single line of text. An edit box is referred to as multiline when it can display its text on more than one line. To create a multiline edit box, use the same Edit control as the above single line Edit box. To make this a multiline edit box, set its Multiline property to True. At design time, to make it obvious that the control can display more than one line of text, heighten it:
To programmatically create a multi-line edit control, declare a variable of type CEdit. When initializing it, add the ES_MULTILINE style. Here is an example: BOOL CExerciseDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // Add "About..." menu item to system menu. . . . No Change // TODO: Add extra initialization here CEdit* edtMemo = new CEdit; edtMemo->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_MULTILINE, CRect(10, 10, 300, 200), this, 2002); return TRUE; // return TRUE unless you set the focus to a control }
If the user is typing text in an edit control and press Enter, the control, usually a button, that is the default would be activated. This feature is valuable for a single line edit box. If you are creating a multiline edit box, you should allow the user to press Enter while entering text into the control. This would prevent the default button from being activated and would transfer the caret to the next line. To provide this functionality, add the ES_WANTRETURN style or, at design time, set the Want Return property to True.
If the text of the edit control is longer than the edit control can display at one time, you should equip it with scroll bars. To do this, at design time, set the Horizontal and/or the Vertical Scroll properties to True. At run time, add the WS_HSCROLL and/or the WS_VSCROLL properties. |
|
|||||||||||||||||||||||
|