c++ mfc create maximum number an edit control can have

with the following code i have managed to update edit control boxes with values on the click of a button.

void SomeDlg::OnBnClickedValue2Plus2()
{
    m_control1.GetWindowText(m_value1);
    m_control2.GetWindowText(m_value2);
    m_control3.GetWindowText(m_value3);
    int a,b,c;
    if (m_getcheck.GetCheck() == 1 
        && _ttoi(m_value2) < 40 
        && _ttoi(m_totalvalue) < 100)
    {
        a = _ttoi(m_value1);
        b = _ttoi(m_value2) + 2;
        c = a + b;
        GetDlgItem(IDC_EDIT1)->SetWindowText(_itot(b, buff, 10)); 
        GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
    }
}

void SomeDlg::OnBnClickedValue3Plus3()
{
    m_control1.GetWindowText(m_value1);
    m_control2.GetWindowText(m_value2);
    m_control3.GetWindowText(m_value3);
    int a,b,c;
    if (m_getcheck.GetCheck() == 1 
        && _ttoi(m_value1) < 40 
        && _ttoi(m_totalvalue) < 100)
    {
        a = _ttoi(m_value1) + 3;
        b = _ttoi(m_value2);
        c = a + b;
        GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10)); 
        GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
    }
}

there are 3 edit controls that update when either plus2 or plus 3 button is pressed. when plus2 is pressed edit1 updates with +2, and when plus3 is pressed edit2 is updated with +3. the 3rd edit control displays the total number of edit1 + edit2 which should have a max of 100 with edit1, and edit2 having a maximum of 40.

however this is not the case, the values won't change if the number is the same or over the limit once the button is pressed, but if the value was 39 and i added 3 it would become 42 instead of 40 before becoming inactive .

how would i go about setting a maximum value for the edit controls?

Answers


The problem is that you are checking the values before you modify them. Simply change both "if" statments like this

if (m_getcheck.GetCheck() == 1 
    && (_ttoi(m_value2 ) + 2 ) <= 40 
    && (_ttoi(m_totalvalue ) + 2 ) <= 100)

And

if (m_getcheck.GetCheck() == 1 
    && ( _ttoi(m_value1 ) + 3 ) <= 40 
    && ( _ttoi(m_totalvalue) + 3 ) <= 100)

I also changed the < in <= because you said the MAX value for 1 and 2 is 40 and the total is 100.


i solved my problem by changing

void SomeDlg::OnBnClickedValue3Plus3()
{
    m_control1.GetWindowText(m_value1);
    m_control2.GetWindowText(m_value2);
    m_control3.GetWindowText(m_value3);
    int a,b,c;
    if (m_getcheck.GetCheck() == 1 
        && _ttoi(m_value1) < 40 
        && _ttoi(m_totalvalue) < 100)
    {
        a = _ttoi(m_value1) + 3;
        b = _ttoi(m_value2);
        c = a + b;
        GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10)); 
        GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
    }    
}

to this

void SomeDlg::OnBnClickedValue3Plus3()
{
    m_control1.GetWindowText(m_value1);
    m_control2.GetWindowText(m_value2);
    m_control3.GetWindowText(m_value3);
    BOOL max1 = 40;
    BOOL max2 = 100;
    int a,b,c;
    a = _ttoi(m_value1) + 3;
    b = _ttoi(m_value2);
    c = a + b;

    if (m_getcheck.GetCheck() == 1 
        && a <= max1
        && c <= max2)
    {
        GetDlgItem(IDC_EDIT2)->SetWindowText(_itot(a, buff, 10)); 
        GetDlgItem(IDC_EDIT3)->SetWindowText(_itot(c, buff, 10));
    }
}

this means when the value is 39, and i try to add 3 nothing happens. however, if i add 1 it reaches 40 which was the way i wanted to go in the first place, i just thought it would have been simpler if i had the number change to the max if i tried to add 3 to 39. turns out it was much easier to do it the way i originally intended. thanks for all the help.


Need Your Help

Disabling Chrome Console Developers Tools

javascript google-chrome command-line console developer-tools

I tried disabling the console but it didnt work in my chrome for some reason.

Atmosphere @MeteorService annotation not working

java comet atmosphere

i trying to create a basic atmosphere test from the below link. I have everything downloaded what it ask from the pom file. But for some reason the eclipse cannot recogonise "@MeteorService(path = ...