如何在MFC C ++中将CString值列表添加到ComboBox中?

时间:2018-08-22 06:58:21

标签: c++ visual-c++ combobox mfc

我有一个CString像这样:

CString cstring = "Aaaa\nBadsalkjhf\nSM876\nSKSJDJHALSD";

如何添加每个CString并将这些项目作为列表添加到ComboBox(MFC,C ++)中。

1 个答案:

答案 0 :(得分:1)

尝试以下代码段:

CStringA cstring = "Aaaa\nBadsalkjhf\nSM876\nSKSJDJHALSD";

// transfer to stringstream to ease splitting 
std::stringstream ss;
ss << cstring.GetString();
std::string str;

// splitting using "\n" as the delimiter
while (std::getline(ss,str,'\n')) {     
    ComboBox.AddString(str.c_str());
}

已编辑:对于Unicode

#include<iostream>
#include<sstream>
#include"atlstr.h"

void main()
{

    CStringW cstring = L"Aaaa\nBadsalkjhf\nSM876\nSKSJDJHALSD";

    // transfer to stringstream to ease splitting 
    std::wstringstream ss;
    ss << cstring.GetString();
    std::wstring str;

    // splitting using "\n" as the delimiter
    while (std::getline(ss, str, L'\n')) {
        ComboBox.AddString(str.c_str());
    }
}