简体   繁体   English

MFC CListCtrl :: SetItemText()不起作用

[英]MFC CListCtrl::SetItemText() not working

I am a beginner in building MFC application. 我是构建MFC应用程序的初学者。 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 . 如果我只更新ID ,更新功能可以正常工作。 If I try to update all/ many fileds, only the ID gets updated and nothing else. 如果我尝试更新所有/多个文件,只会更新ID而不会更新。 BTW, age is a spinControl, department is a comboBox and the other two are editBox. BTW,age是一个spinControl,department是一个comboBox,另外两个是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); 我发现变量m_name和editBox值的值都变为行m_listControl.SetItemText(index,0,m_id);之后的旧值m_listControl.SetItemText(index,0,m_id); . Its the same case with m_age and m_department . 它与m_agem_department情况相同。 If I comment the line m_listControl.SetItemText(index,0,m_id); 如果我注释行m_listControl.SetItemText(index,0,m_id); , I can update everything at a time except the ID . ,除了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); 我可以通过将m_namem_agem_department存储在行m_listControl.SetItemText(index,0,m_id);之前的局部变量中来更新所有内容m_listControl.SetItemText(index,0,m_id); and using those variables in SetItemText() . 并在SetItemText()使用这些变量。 But as I'm learning, I wanna know where I'm going wrong. 但是在我学习的时候,我想知道自己哪里出错了。

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. 我想你忘记在Update_Bn_Click下的代码之前添加UpdateData() ,因为在我这边我使用你的代码和updatedata()并且工作正常。

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);

Try pumping some messages after updating the items. 更新项目后尝试提取一些消息。

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

Turn off your sorting. 关闭你的排序。 In your designer: Properties>Behaviour>Sort set to None. 在您的设计器中:属性>行为>排序设置为无。

You have to add Item into 0 colomn index first. 您必须先将Item添加到0 colomn index中。

listcontrol->InsertItem(0,_T("text")); listcontrol-> InsertItem(0,_T( “文本”));

then, you can set text to the subItem; 然后,您可以将文本设置为subItem;

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

First, make sure the Owner Data property of the control is set to FALSE. 首先,确保控件的Owner Data属性设置为FALSE。

Maybe try m_List.Update(index) after the last SetItemText() . 也许在最后一个SetItemText()之后尝试m_List.Update(index) 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. 我必须承认,在我需要更新列表元素的任何地方,我使用所有者数据CListCtrl因为我认为在大量项目的情况下它更快,并且在长期内更容易处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM