Skip to main content
www.olaf-groeger.de

Main navigation

  • Home
  • NMR
  • Programming
    • Streams
    • Subclassing of edit controls
  • Software
    • Calimah Scheduler
    • ServicesCtl

Breadcrumb

  1. Home
  2. Programming
  3. Subclassing of Edit Controls
  4. Implementation of The Setter/getter Methods

Implementation of the setter/getter methods

By olaf , 3 July 2025

The methods SetValue(double), GetValue() and the operator = define the interface between the CHertzEdit and the surrounding application. They can be used to set and get the value of the control.

void CHertzEdit::SetValue(double dValue)
{
        m_dValue=dValue;
        m_strValue=DoubleToString(m_dValue).c_str();
}

double CHertzEdit::GetValue()
{
        return(m_dValue);
}

CHertzEdit& CHertzEdit::operator =(double dValue)
{
        SetValue(dValue);
        return(*this);
}

The methods GetValue(HWND), SetValue(HWND), SetValueToWindow(), SetValueToWindow(HWND), GetValueFromWindow() and GetValueFromWindow(HWND) define the interface between CHertzEdit and the windows input control.
The real job will be done by the methods SetValueToWindow(HWND) and GetValueFromWindow(HWND). The methods SetValue(HWND) and GetValue(HWND) are used for the data transfer via DDX, the methods SetValueToWindow() and GetValueFromWindow() are used for the data transfer that is triggered by the WM_KILLFOCUS message.

void CHertzEdit::SetValueToWindow(HWND hWnd)
{
        if (m_hWnd)
                ::SetWindowText(hWnd, m_strValue);
}

BOOL CHertzEdit::GetValueFromWindow(HWND hWnd)
{
        CString strValue;
        TCHAR* pChar;
        BOOL bReturn;
        int i;
        double d;

        i= ::GetWindowTextLength(hWnd);
        if (!i) return(FALSE);

        // hole den Inhalt des Controls
        pChar=new char[i+1];
        ::GetWindowText(hWnd, pChar, i+1);
        strValue=pChar;
        delete[] pChar;

        // Konvertieren in double
        d=CStringToDouble(strValue);

        // wenn gültig in Member-Variabler speichern
        if ((d < 0.0) && !m_bNegativAllowed)
        { // Inhalt ist ungültig
                if (m_dValue > 0.0)
                        m_dValue=-1.0; // Wert ungültig machen
                bReturn=FALSE; // sonst ungültigen Wert belassen
        }
        else
        {
                m_dValue = d;
                bReturn = TRUE;
        }

        // zurückkonvertieren (vor allem wenn invalid)
        m_strValue = DoubleToString(m_dValue).c_str();
        return(bReturn);
}

The methods GetValue(HWND) and SetValue(HWND) are used by the methods DDX_HertzEdit, which is called by the DDX method DoDataExchange().

BOOL CHertzEdit::GetValue(HWND hWndCtrl)
{
        return(GetValueFromWindow(hWndCtrl));
}

BOOL CHertzEdit::SetValue(HWND hWndCtrl)
{
        m_strValue = DoubleToString(m_dValue).c_str();
        SetValueToWindow(hWndCtrl);
        return(m_strValue.CompareNoCase("invalid") == 0);
}

The methods SetValueToWindow() and GetValueFromWindow() are called from OnKillFocus().
void CHertzEdit::SetValueToWindow()
{
        HWND hWnd = GetSafeHwnd();
        SetValueToWindow(hWnd);
}

BOOL CHertzEdit::GetValueFromWindow()
{
        HWND hWnd = GetSafeHwnd();
        return(GetValueFromWindow(hWnd));
}

Book traversal links for 15

  • Implement the message handlers for WM_CHAR
  • Up
  • Implementation of the DDX method

Language switcher

  • English
  • German
Powered by Drupal