小弟在用Listbox的时候,在主对话框中初始化函数中添加了几条记录,现在在对话框按钮中利用添加/删除按钮来修改记录
,虽然可以实现,但是有个问题,就是添加或者删除完后的记录如何保存呢?使得重新运行对话框的时候listbox中显示的是你上次修改过后保留的内容?

解决方案 »

  1.   

    简单点,配置文件INI的读写http://www.vckbase.com/code/filesys/ini/fileinirw.rar
    //看看这个
      

  2.   

    我一般习惯用access数据库!!
      

  3.   


    datoucaicai,你建议看的已经看了,可是还是不大清楚,我的是listbox控件有好几行数据,不是edit,能不能和我说一下具体怎么做?
      

  4.   

    添加或者删除完后,可以用一个循环将ListBox每一行的数据写到配置文件中
    在主对话框的初始化函数中,将配置文件的内容读进来添加到ListBox每一行中显示
      

  5.   

    配置文件格式可以如下所示:
    ////////////////////////////////////////////////////////////////////////////////////
    ;该配置文件用来初始化ListBox
    ;当ListBox的内容有所变化的时候,将修改后的内容保存到该配置文件
    [ROW1]
    CONTENT=第1行
    [ROW2]
    CONTENT=第2行
    [ROW3]
    CONTENT=第3行
    //////////////////////////////////////////////////////////////////////////////////////初始化的时候读配置文件,将内容显示到listbox中,如下所示BOOL CTestLBIniDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
        ...
        // TODO: Add extra initialization here    //读配置文件LISTBOX.INI,初始化ListBox控件内容
        CString  strrow;  //行数
        CString  strsection;  //配置文件段名
        CString  strcontent;  //读取内容保存到strcontent
        int nrow = 1;    strrow.Format("%d",nrow);
        strsection = "ROW"+strrow;  //初始段名为“ROW1”    //通过GetPrivateProfileString读取配置文件LISTBOX.INI
        while(GetPrivateProfileString(strsection,"CONTENT",NULL,
    strcontent.GetBuffer(50),50,".\\LISTBOX.INI")!=0)
        {
    m_ctrlList.InsertString(nrow-1,strcontent);
    nrow++;
    strrow.Format("%d",nrow);
    strsection = "ROW"+strrow;
        }

        return TRUE;  // return TRUE  unless you set the focus to a control
    }
    //删除按钮响应函数如下:void CTestLBIniDlg::OnButtonDel() 
    {
        // TODO: Add your control notification handler code here
        //点击一次删除按钮,删除list的最后一行,并删除LISTBOX.INI中对应的行
         CString strrow;
        CString strsection;
        if(m_ctrlList.GetCount()!=0)
        {
            strrow.Format("%d",m_ctrlList.GetCount());
            strsection = "ROW"+strrow; WritePrivateProfileString(strsection,NULL,NULL,".\\LISTBOX.INI");  //删除配置文件行 m_ctrlList.DeleteString(m_ctrlList.GetCount()-1);  //删除list最后一行
        }
    }
    //添加按钮对应函数void CTestLBIniDlg::OnButtonAdd() 
    {
        // TODO: Add your control notification handler code here
        //点击一次添加按钮,添加一行新的内容到listbox最后,并写入配置文件
        int nlastrow = m_ctrlList.GetCount();
        CString strrow;
        CString strsection;
        CString strcontent;    strrow.Format("%d",nlastrow+1);
        strsection = "ROW" + strrow;
        strcontent = "第"+strrow+"行";    WritePrivateProfileString(strsection,"CONTENT",strcontent,".\\LISTBOX.INI"); //写入配置文件     
        m_ctrlList.InsertString(nlastrow,strcontent);  //list添加一行
    }
    //大概是这么个意思
      

  6.   

    数据放在一个list中,初始化时把list中数据显示到listbox中,删除时list同步删除,增加也是,这样就可以达到你的要求了
      

  7.   

    datoucaicai 是好同志~
    诲人不倦~
    支持你~