c++mfcslidercontrolseditbox

Handle the Sliders Control with Edit Box in MFC


I have a problem with appropriate using Slider Control and good understanding of mechanism of UpdatingData of values. This is a very simple program which should help me get understood of problem.

Take a look on my code:

// TestDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ControlsandVariables.h"
#include "TestDialog.h"
#include "afxdialogex.h"


// TestDialog dialog

IMPLEMENT_DYNAMIC(TestDialog, CDialog)

TestDialog::TestDialog(CWnd* pParent /*=NULL*/)
    : CDialog(TestDialog::IDD, pParent)
    , editbox_value(0)
    , slider_value(0)
{

}

TestDialog::~TestDialog()
{
}

void TestDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_SLIDER1, slider_ctrl);
    DDX_Text(pDX, IDC_EDIT1, editbox_value);
    DDV_MinMaxInt(pDX, editbox_value, 0, 99);
    DDX_Slider(pDX, IDC_SLIDER1, slider_value);
}


BEGIN_MESSAGE_MAP(TestDialog, CDialog)
    ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, &TestDialog::OnNMCustomdrawSlider1)
    ON_EN_CHANGE(IDC_EDIT1, &TestDialog::OnEnChangeEdit1)
END_MESSAGE_MAP()


// TestDialog message handlers


void TestDialog::OnNMCustomdrawSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: Add your control notification handler code here
    
    int value = slider_ctrl.GetPos();
    editbox_value = value;
    UpdateData(TRUE);
    
    //editbox_value = slider_value;
    //UpdateData(TRUE);
    *pResult = 0;
}

I have a problem with change the value on slider and edit box simultanously. If I use UpadateData(TRUE) the value of slider is changing but edit box remains unchanged.
On the other hand, if I set UpdateData (FALSE) the slider remains unchanged but editbox value is changing and after iteration comeback to 0. How to achieve the state where both slider and editbox change their values ​​depending on each other. I would like to mention that this is a WindowsEmbedded 2013 version of the MFC library, not for local Windows.


Solution

  • You have to create two control type variables for CEdit and CSliderCtrl. example:

    enter image description here

    enter image description here

    Remarques :

    Add the following functions to the Dlg header class:

    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg void OnEnChangeEdit();
    

    And in CPP:

    BEGIN_MESSAGE_MAP(CMFCSpinerDlg, CDialogEx)
        ON_WM_HSCROLL() // Called when Slider is scrolled
        ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit) // Called when CEdit change value
    END_MESSAGE_MAP()
    
    
    void CMFCSpinerDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
        CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
    
        // You can have multiple sliders: Check which one sent the notification  
        if (pSlider == &m_slider)
        {
            CString strSliderValue;
            int iValue = m_slider.GetPos(); // Get Slider value
            strSliderValue.Format("%d", iValue); 
    
            m_cedit.SetWindowText(strSliderValue); // Change CEdit Value
        }
    }
    
    void CMFCSpinerDlg::OnEnChangeEdit()
    {
        CString strEditValue;
        m_cedit.GetWindowText(strEditValue);
        int iCeditValue = atoi(strEditValue);
        
        // do other check on value ...
        m_slider.SetPos(iCeditValue); // set Slider value
    }
    

    By doing this :