我的列表控件需要一个while循环

时间:2014-10-09 11:18:59

标签: c++ for-loop while-loop

这是我的代码:

  void CProjektSchulDBView::OnBnClickedButton1()
{
    CProjektSchulDBDoc * doc = GetDocument ();
    m_cListCtrl.DeleteAllItems  ();
    // first number is the row, second number is the column and the last number
    // is from the struct array
        AddData (m_cListCtrl, 0, 0, doc -> SchuelerDataArr[0].name);
        AddData (m_cListCtrl, 0, 1, doc -> SchuelerDataArr[0].vorname);
        AddData (m_cListCtrl, 0, 2, doc -> SchuelerDataArr[0].klasse);
        AddData (m_cListCtrl, 1, 0, doc -> SchuelerDataArr[1].name);
        AddData (m_cListCtrl, 1, 1, doc -> SchuelerDataArr[1].vorname);
        AddData (m_cListCtrl, 1, 2, doc -> SchuelerDataArr[1].klasse);
        AddData (m_cListCtrl, 2, 0, doc -> SchuelerDataArr[2].name);
        AddData (m_cListCtrl, 2, 1, doc -> SchuelerDataArr[2].vorname);
        AddData (m_cListCtrl, 2, 2, doc -> SchuelerDataArr[2].klasse);
        AddData (m_cListCtrl, 3, 0, doc -> SchuelerDataArr[3].name);
        AddData (m_cListCtrl, 3, 1, doc -> SchuelerDataArr[3].vorname);
        AddData (m_cListCtrl, 3, 2, doc -> SchuelerDataArr[3].klasse);
        AddData (m_cListCtrl, 4, 0, doc -> SchuelerDataArr[4].name);
        AddData (m_cListCtrl, 4, 1, doc -> SchuelerDataArr[4].vorname);
        AddData (m_cListCtrl, 4, 2, doc -> SchuelerDataArr[4].klasse);
    }

所以这适合我,但我想在一段时间或一个for循环中有这个,但我找不到答案。

3 个答案:

答案 0 :(得分:1)

for(i = 0; i< 5; i++)
{
     AddData (m_cListCtrl, i, 0, doc -> SchuelerDataArr[i].name);
     AddData (m_cListCtrl, i, 1, doc -> SchuelerDataArr[i].vorname);
     AddData (m_cListCtrl, i, 2, doc -> SchuelerDataArr[i].klasse);
}

答案 1 :(得分:1)

这是while循环版本:

int i = 0;
while(i< 5)
{
     AddData (m_cListCtrl, i, 0, doc -> SchuelerDataArr[i].name);
     AddData (m_cListCtrl, i, 1, doc -> SchuelerDataArr[i].vorname);
     AddData (m_cListCtrl, i, 2, doc -> SchuelerDataArr[i].klasse);
     i++;
}

答案 2 :(得分:0)

以防万一你只想进行while循环

 void CProjektSchulDBView::OnBnClickedButton1()
 {
     CProjektSchulDBDoc * doc = GetDocument ();
     m_cListCtrl.DeleteAllItems();
     unsigned int uiTotalRows = 5;
     unsigned int uiRow = 0;

     while(uiRow < uiTotalRows)
     {
         AddData (m_cListCtrl, uiRow , 0, doc -> SchuelerDataArr[uiRow].name);
         AddData (m_cListCtrl, uiRow , 1, doc -> SchuelerDataArr[uiRow].vorname);
         AddData (m_cListCtrl, uiRow , 2, doc -> SchuelerDataArr[uiRow].klasse);
         uiRow ++;
     }
  }