MFC中的CStringList

时间:2008-12-18 05:04:10

标签: mfc

我用2个CStringList对象编码。每个都有自己的数据,例如一个有名字和其他的phoneno,两个都是同步的,即如果有一个phoneno有一个名字,反之亦然。

现在,我有2个组合框,其中我显示了名称和相应的phonenos。名称组合框被排序,因此两者之间的同步进行折腾。因此,为了排序,我做了以下事情:


int aComboElementNo = myNameComboBox.GetCount();
if( aComboElementNo >= 1 )
{
    for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
    {
        CString aTempStr;
        // Getting the string in the combobox
        myNameComboBox.GetLBText( aIndex, aTempStr );
        // Getting the position where the name is present in the list
        POSITION aPos = aNameList.Find( aTempStr );
       // setting the appropriate phoneno in the 2nd combobox
        myPhoneComboBox.AddString( aPhoneList.GetAt( aPos ) );
    }
}

当我执行此操作时,我在myPhoneComboBox中找到了名字而不是phonenos。

现在我有2个qns:

  1. 当我访问录音带时,为什么我会在名单中出现名字?这不是一个漏洞,因为我可以使用其他变量访问其他一些变量数据。

  2. 如何排序第二个列表。

3 个答案:

答案 0 :(得分:1)

我希望你正在使用CStringArray而不是CStringList。 您需要使用FindIndex而不是Find,因为Find将返回OBJECT Pos而不是Index count ....  并使用[]运算符来获取带数组的元素。 如果你仍然想要使用CStringList,那么通过Iterator查找一个List中第一个匹配字符串的索引计数,并使用该IndexCount的FindIndex获取第二个列表的postition对象,以便将GetAt用于第二个列表。

答案 1 :(得分:0)

为什么你有2个单独的名单?为什么没有一个CTypedPtrArray结构同时包含名称和手机nb?

答案 2 :(得分:0)

这是一个谦虚的,原谅我,找到名字的愚蠢方式。它假定名称是唯一的。上帝帮助我,我不得不处理这些事情,名字字段永远不应被视为独特,它的血腥危险。请问我爸爸Baash05 Sr.

我认为应用程序添加到组合框时会有ID或某些数据集。请在地图中使用它。我的猜测是程序员将数据设置为名称的ID,或指向包含该名称的对象的指针。 (人物对象/业务对象/学生对象......)。

如果添加名称的代码没有添加方法来区分George Forman和他的任何一个孩子,那么就向老板提出一个论点,它的实现应该改变,因为上帝应该是!

int aComboElementNo = myNameComboBox.GetCount();
for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
{
    int nameLocal = myNameComboBox.GetItemData( aIndex);
    myPhoneComboBox.InsertString(aIndex, aPhoneList[namelocal] );
}
相关问题