CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2016
    Posts
    1

    Format Edit Control with Spin Control, Leading Zeros

    I have an MFC Edit Control with a Spinner and it works pretty good but I want to format the number to a fixed length with leading zeros like this: 000, 001, 012, 123.
    I tried to use the Spin Control event UDN_DELTAPOS to read the Edit Controls Member Variable CString and modify it with leading zeros as necessary.
    Unfortunately, when this event happens, this Member Variable still contains the OLD value and not the new one.

    Is there an event that happens AFTER the Spin Control finishes its work?
    Any other suggestions?

    I am also limiting the spinner like this:
    OnInitDialog()
    {
    //This part works
    //Set range of spin control
    CSpinButtonCtrl* SpinControl;
    SpinControl = (CSpinButtonCtrl*)GetDlgItem(IDC_spnActual);
    SpinControl->SetRange( MinActual, MaxActual );
    }

    Thanks,
    Ken

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Format Edit Control with Spin Control, Leading Zeros

    You could try handling EN_CHANGE or EN_UPDATE notifications.
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: Format Edit Control with Spin Control, Leading Zeros

    A spin control also sends WM_VSCROLL to its parent. So, if you want to customize what is displayed in the buddy edit control, first remove UDS_SETBUDDYINT style (in the resource editor, put Set Buddy Integer property to False) then handle WM_VSCROLL message in the parent dialog class.

    Here is an example:
    Code:
    class CDemoDialog : public CDialog
    {
        // ...
        CSpinButtonCtrl m_spinCtrl;
        afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    };
    Code:
    void CDemoDialog::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_SPIN_DEMO, m_spinCtrl);
       // ...
    }
    
    BEGIN_MESSAGE_MAP(CDemoDialog, CDialog)
        // ...
        ON_WM_VSCROLL()
    END_MESSAGE_MAP()
    
    // ...
    
    void CDemoDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
        if ((pScrollBar->GetSafeHwnd() == m_spinCtrl.m_hWnd)
            && (SB_THUMBPOSITION == nSBCode))
        {
            CString strText;
            strText.Format(_T("%04d"), nPos); // format with leading zeros
            m_spinCtrl.GetBuddy()->SetWindowText(strText);
        }
    
        CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    }
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured