CEditbox question

This is a discussion on CEditbox question within the Windows Programming forums, part of the Platform Specific Boards category; Hi, This what I am trying to do in an Edit Box: Enter the numbers for a date in an ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    17

    CEditbox question

    Hi,

    This what I am trying to do in an Edit Box: Enter the numbers for a date in an edit box and as I am entering the numbers a forward slash appears at predetermined locations. I type MM and a forward slash appears. Type DD and another slash appears. The end result is mm/dd/yy. I recently used an online application that had such a feature.

    I tried using the OnChar() for WM_CHAR but this only deals with the character entered.

    I tried using OnChangeEdit for EN_CHANGE but did not get anywhere. What I have below will put a new string in the edit box but the program will lock. Doe anyone have any additional suggestions on how to figure this out.

    void CEditSampleDlg::OnChangeEditName()
    {
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here

    CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_NAME);

    CString strText;


    pEdit->GetWindowText(strText);


    int nLength = strText.GetLength();

    CString strResult;

    strResult.Format("The lenth is %d ", nLength);

    AfxMessageBox(strResult);


    pEdit->SetWindowText(strResult);



    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    The problem is that if the text is updated within the code then OnChangeEditName() will immediately get another 'on change' call so you need to ignor the next 'on change' when the text is changed, otherwise it will create an infinite loop.

    Something like this seems to work and also moves the cursor to the end of the text.
    Code:
    bool FormatDate(char *Buffer)
    {
        // DD/MM/YY     
        if( /*date buffer needs modifing then*/ )
         {
             //modifiy the date string etc... 
              return 1 
         }    
         return 0; 
    }
    
    void CEditSampleDlg::OnChangeEditName() 
    {
         static bool IgnorNextChange = 0;
         char Buffer[50];
    
         m_EditSearchUpdate.GetWindowText( Buffer, 50 );
    
         if( !IgnorNextChange && FormatDate(Buffer) )
         {
              IgnorNextChange = 1;
              m_EditSearchUpdate.SetWindowText("");
              m_EditSearchUpdate.ReplaceSel(Buffer);
         }
         IgnorNextChange = 0;
    }
    Last edited by Scarlet7; 06-06-2003 at 04:35 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    17

    Thanks

    Hi,

    I followed you suggestion. Here is what I have so far. You comments would be appreciated. I set the LimitText() for the edit box to 8 characters: 01/21/03.

    Question: Do I need to be concerned about the new line character? I tried to add in at the end put it just left a solid black line. The way the code is written seems to work fine.


    Question: How does the the value of "static bool IgnorNextChange = 0" change? Each time OnChangeEditName is invoked the value of static bool IgnorNextChange = 0. I changed if(!IgnorNextChange && FormatDate(Buffer) )
    FormatDate(Buffer) ) to if(FormatDate(Buffer) ) and it works fine.






    void CEdSample2Dlg::OnChangeEditName()
    {
    // TODO: If this is a RICHEDIT control, the control will not
    // send this notification unless you override the CDialog::OnInitDialog()
    // function and call CRichEditCtrl().SetEventMask()
    // with the ENM_CHANGE flag ORed into the mask.

    // TODO: Add your control notification handler code here




    static bool IgnorNextChange = 0;
    char Buffer[50];

    m_edDate.LimitText(8);


    m_edDate.GetWindowText( Buffer, 50 );

    if(!IgnorNextChange && FormatDate(Buffer) )
    {




    IgnorNextChange = 1;
    m_edDate.SetWindowText("");
    m_edDate.ReplaceSel(Buffer);
    }

    IgnorNextChange = 0;




    }

    bool CEdSample2Dlg::FormatDate(char *Buffer)
    {


    char Message[25] = "in formatdate()";

    char NewChar[3] = "/";
    char EOL[3] = "\n";

    int nLen = strlen(Buffer);

    // DD/MM/YY
    if(nLen == 2)///*date buffer needs modifing then*/true )
    {
    //modifiy the date string etc...
    //AfxMessageBox(Message);

    strcat(Buffer,NewChar);

    return 1;
    }

    if(nLen == 5)
    {

    strcat(Buffer,NewChar);
    return 1;

    }


    // if(nLen == 8)
    // {

    //AfxMessageBox("You entered to many characters");
    //strcat(Buffer,EOL);

    // return 1;
    // }
    return 0;

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21