1
votes

I am a beginner in building MFC application. I've just started using list controls (in report view) and I am facing some problems while updating the list. I have three buttons for add, update and delete. Everything works well except the update. Here's the code.

void CAddDetailsDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX); 
    DDX_Control(pDX, IDC_DEPARTMENT, departmentControl);
    DDX_Text(pDX, IDC_NAME, m_name);
    DDX_Text(pDX, IDC_ID, m_id);
    DDX_Text(pDX, IDC_AGE_BUDDY, m_ageVariable);
    DDX_CBString(pDX, IDC_DEPARTMENT, m_department);
    DDX_Control(pDX, IDC_LIST1, m_listControl);
}

BOOL CAddDetailsDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    // TODO:  Add extra initialization here 
    ageSpin=reinterpret_cast<CSpinButtonCtrl*>(GetDlgItem(IDC_AGE_SPIN));   
    ageBuddy=reinterpret_cast<CEdit*>(GetDlgItem(IDC_AGE_BUDDY));   
    ageSpin->SetBuddy((ageBuddy));
    ageSpin->SetRange32(18,60);     
    departmentControl.AddString("Human Resource");
    departmentControl.AddString("Manager");
    departmentControl.AddString("Administrator");
    departmentControl.AddString("Desktop Engineer");

    m_listControl.InsertColumn(0,"ID",0,100);
    m_listControl.InsertColumn(1,"Name",0,100);
    m_listControl.InsertColumn(2,"Age",0,60);
    m_listControl.InsertColumn(3,"Department",0,100);       
    m_listControl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );

    m_ageVariable="18";
    UpdateData(FALSE);

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}
void CAddDetailsDlg::OnBnClickedEdit()
{
    // TODO: Add your control notification handler code here    
    UpdateData();
    if((m_id=="")||(m_name=="")||(m_department=="")||(m_ageVariable==""))
    {
        MessageBox("Please choose an item to edit","Error");
    }
    else
    {           
            int index=m_listControl.GetSelectionMark();
            m_listControl.SetItemText(index,0,m_id);
            m_listControl.SetItemText(index,1,m_name);
            m_listControl.SetItemText(index,2,m_ageVariable);
            m_listControl.SetItemText(index,3,m_department);            
            MessageBox("Successfully Updated","Info");       
    }       
}
void CAddDetailsDlg::OnBnClickedNewButton()
{
    // TODO: Add your control notification handler code here    
    UpdateData();
    if((m_id=="")||(m_name=="")||(m_department=="")||(m_ageVariable==""))
    {
        MessageBox("Please fill in all the details","Error");
    }
    else
    {       
        int count=m_listControl.GetItemCount();
        count=m_listControl.InsertItem(count,m_id);
        m_listControl.SetItemText(count,1,m_name);
        m_listControl.SetItemText(count,2,m_ageVariable);
        m_listControl.SetItemText(count,3,m_department);        
    }
}

Note:-

The update function works fine if I update only the ID. If I try to update all/ many fileds, only the ID gets updated and nothing else. BTW, age is a spinControl, department is a comboBox and the other two are editBox.

Edit:-

I found that both, the value of variable m_name and the editBox value changes to the older values after the line m_listControl.SetItemText(index,0,m_id);. Its the same case with m_age and m_department. If I comment the line m_listControl.SetItemText(index,0,m_id);, I can update everything at a time except the ID.

I am able to update everything by storing m_name, m_age and m_department in a local variable just before the line m_listControl.SetItemText(index,0,m_id); and using those variables in SetItemText(). But as I'm learning, I wanna know where I'm going wrong.

5
Did you debug it ? What's the value of mindexright after int index=m_listControl.GetSelectionMark()? - Jabberwocky
And please show the two versions of the code you describe under "Note". Generally when asking questions here, dont't describe your code, show it. - Jabberwocky
@Micheal walz, those are the two cases the user would do in UI and are not any separate versions of my code. And while debugging, 'm_name' value was the right value. But after the line 'm_listControl.SetItemText(index,0,m_id);' , m_name takes the old value. I have no clue. - Hari Ram
Please explain that by editing your question. - Jabberwocky
@MichaelWalz, Sorry. my bad. I updated the question now. - Hari Ram

5 Answers

2
votes

i think you forget to add UpdateData() before your code which is under Update_Bn_Click because at my side i use your code with updatedata() and its working fine.

UpdateData();
int index=m_List.GetSelectionMark();
m_List.SetItemText(index,0,m_id);
m_List.SetItemText(index,1,m_Name);
m_List.SetItemText(index,2,m_Age);
m_List.SetItemText(index,3,m_DepartMent);
1
votes

Try pumping some messages after updating the items.

while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
1
votes

Turn off your sorting. In your designer: Properties>Behaviour>Sort set to None.

0
votes

You have to add Item into 0 colomn index first.

listcontrol->InsertItem(0,_T("text"));

then, you can set text to the subItem;

listctrol->SetItemText(0,1,_T(subText)):

-1
votes

First, make sure the Owner Data property of the control is set to FALSE.

Maybe try m_List.Update(index) after the last SetItemText().

I must admit that everywhere I need updated list elements, I use an Owner Data CListCtrl because I think its faster in case of a big number of items and easier to handle in the long term.