Tuesday, July 19, 2011

Exploring CEdit VC++\MFC

CEdit Class

Provides the functionality of a Windows edit control.

class CEdit : public CWnd


Details:
An edit control is a rectangular child window in which the user can enter text.

You can create an edit control either from a dialog template or directly in your code. In both cases, first call the constructor CEdit to construct the CEdit object, then call the Create member function to create the Windows edit control and attach it to the CEdit object.

Construction can be a one-step process in a class derived from CEdit. Write a constructor for the derived class and call Create from within the constructor.

CEdit inherits significant functionality from CWnd. To set and retrieve text from a CEdit object, use the CWnd member functions SetWindowText and GetWindowText, which set or get the entire contents of an edit control, even if it is a multiline control. Text lines in a multiline control are separated by '\r\n' character sequences. Also, if an edit control is multiline, get and set part of the control's text by calling the CEdit member functions GetLine, SetSel, GetSel, and ReplaceSel.

If you want to handle Windows notification messages sent by an edit control to its parent (usually a class derived from CDialog), add a message-map entry and message-handler member function to the parent class for each message.

Each message-map entry takes the following form:
ON_Notification( id, memberFxn )
where id specifies the child window ID of the edit control sending the notification, and memberFxn is the name of the parent member function you have written to handle the notification.

The parent's function prototype is as follows:
afx_msg void memberFxn( );

Following is a list of potential message-map entries and a description of the cases in which they would be sent to the parent:

ON_EN_CHANGE The user has taken an action that may have altered text in an edit control. Unlike the EN_UPDATE notification message, this notification message is sent after Windows updates the display.
ON_EN_ERRSPACE The edit control cannot allocate enough memory to meet a specific request.
ON_EN_HSCROLL The user clicks an edit control's horizontal scroll bar. The parent window is notified before the screen is updated.
ON_EN_KILLFOCUS The edit control loses the input focus.
ON_EN_MAXTEXT The current insertion has exceeded the specified number of characters for the edit control and has been truncated. Also sent when an edit control does not have the ES_AUTOHSCROLL style and the number of characters to be inserted would exceed the width of the edit control. Also sent when an edit control does not have the ES_AUTOVSCROLL style and the total number of lines resulting from a text insertion would exceed the height of the edit control.

ON_EN_SETFOCUS Sent when an edit control receives the input focus.
ON_EN_UPDATE The edit control is about to display altered text. Sent after the control has formatted the text but before it screens the text so that the window size can be altered, if necessary.

ON_EN_VSCROLL The user clicks an edit control's vertical scroll bar. The parent window is notified before the screen is updated.
If you create a CEdit object within a dialog box, the CEdit object is automatically destroyed when the user closes the dialog box.

If you create a CEdit object from a dialog resource using the dialog editor, the CEdit object is automatically destroyed when the user closes the dialog box.

If you create a CEdit object within a window, you may also need to destroy it. If you create the CEdit object on the stack, it is destroyed automatically. If you create the CEdit object on the heap by using the new function, you must call delete on the object to destroy it when the user terminates the Windows edit control. If you allocate any memory in the CEdit object, override the CEdit destructor to dispose of the allocations.

CEdit::GetSel 
Call this function to get the starting and ending character positions of the current selection (if any) in an edit control, using either the return value or the parameters.

DWORD GetSel( ) const;

void GetSel(  int& nStartChar,  int& nEndChar ) const;

Parameters:
nStartChar :  Reference to an integer that will receive the position of the first character in the current selection.

nEndChar : Reference to an integer that will receive the position of the first nonselected character past the end of the current selection.

Return Value
The version that returns a DWORD returns a value that contains the starting position in the low-order word and the position of the first nonselected character after the end of the selection in the high-order word.

// The pointer to edit box.

CEdit* pEdit;
// Set the selection to be all characters after the current selection.
DWORD dwSel = pmyEdit->GetSel();
pmyEdit->SetSel(HIWORD(dwSel), -1);

CEdit::SetSel
Call this function to select a range of characters in an edit control.
In two Different ways:

i)   void SetSel( DWORD dwSelection, BOOL bNoScroll = FALSE );
ii)  void SetSel( int nStartChar,  int nEndChar,  BOOL bNoScroll = FALSE );

Parameters: dwSelection
Specifies the starting position in the low-order word and the ending position in the high-order word. If the low-order word is 0 and the high-order word is –1, all the text in the edit control is selected. If the low-order word is –1, any current selection is removed.

bNoScroll
Indicates whether the caret should be scrolled into view. If FALSE, the caret is scrolled into view. If TRUE, the caret is not scrolled into view.

nStartChar
Specifies the starting position. If nStartChar is 0 and nEndChar is –1, all the text in the edit control is selected. If nStartChar is –1, any current selection is removed.

nEndChar
Specifies the ending position.

Example: Ref. above.


Problems and Solutions in CEdit class
Problem 1: I'm tring to set the focus to an edit box with GetDlgItem(IDC_FIRSTNAME)->SetFocus();

then i tried to to select its content with SetSel().  but somehow it does not work.

I Tried this way:
(IDC_EDIT)->SetSel();
IDC_EDIT.SetSel();
m_Edit.SetSel();

Getting compiler Error saying:   "the left part of .SetSel has to be a class/struct/union."  or   "SetSel is no element of CWnd"
how is it correct?

Solution:
You are trying to implement it in a wrong way.
Try to understand here :
GetDlgItem  returns the Adress of your dialog item, but here the dataType is CWnd*


CWnd - is a parent class of all controls - doesn't have a method "SetSel()". This method is declared and defined in the class CEdit, which of course is a child class of CWnd.


Making a call  CWnd* (as you get it from GetDlgItem()) with the "->" operator, the pointer is dereferenced as CWnd.

So all you have to do is to cast the pointer to CEdit*, to to this change your line of code to

CEdit *pYourEdit = (CEdit*) GetDlgItem(IDC_FIRSTNAME);
pYourEdit ->SetSel(0, -1);

Or  use,      ( (CEdit*) GetDlgItem(IDC_FIRSTNAME) )->SetSel(0, -1);

Another way of calling the functions, You can also use , the functions this way.
m_CtlEdit.SetFocus()
m_CtlEdit.SetSel(5,7)


Here is an another sample code:

void CYourDlg::OnButton()
{
   UpdateData(TRUE);
   GetDlgItem(IDC_FIRSTNAME)->SetFocus();
   GetDlgItem(IDC_FIRSTNAME)->SetSel(0, -1);
   UpdateData(FALSE);

}

No comments:

Post a Comment

Health Benefits of Cashews

  Benefits of Cashews. Healthy food is an integral part of healthy body. Regular exercises such as Yoga and healthy diet is important to...