简体   繁体   English

mfc acombo box添加字符串

[英]mfc acombo box add string

HKEY hKey = 0;
DWORD dwType = REG_SZ;
TCHAR buf[255] = {0};
DWORD dwBufSize = sizeof(buf);
DWORD ret;
CComboBox m_portCombo;

if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"), 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS )
{
    if( RegQueryValueEx( hKey, TEXT("\\Device\\Serial0"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
    {
        CString str = buf;
        m_portCombo.AddString(str);
    }

    if( RegQueryValueEx( hKey, TEXT("\\Device\\Serial1"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
    {
        CString str = buf;
    }

    if( RegQueryValueEx( hKey, TEXT("\\Device\\Serial2"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
    {
        CString str = buf;
    }

    if( RegQueryValueEx( hKey, TEXT("\\Device\\Serial3"), 0, &dwType, (LPBYTE)buf, &dwBufSize ) == ERROR_SUCCESS )
    {
        CString str = buf;
    }
}

First problem : i want to change the TEXT("\\\\Device\\\\Serial3") with something like TEXT("\\\\Device\\\\Serial",%i) , so i can resume all that lines of code to a for loop.Is tehre a way to accomplish this? 第一个问题 :我想用TEXT("\\\\Device\\\\Serial",%i)类的东西来改变TEXT("\\\\Device\\\\Serial3") TEXT("\\\\Device\\\\Serial",%i) ,所以我可以将所有代码行恢复for循环这是一种实现这个目标的方法吗?

Second problem : if i use the m_portCombo.AddString(str); 第二个问题 :如果我使用m_portCombo.AddString(str); i get an Debug Assertion Failed! 我得到一个Debug Assertion Failed! error, and, of course, the combobox is not populated with that registry value. 错误,当然,组合框没有填充该注册表值。 Why could that happen? 为什么会这样?

First Problem: Use the CString Format() function using %d for integer. 第一个问题:使用CString Format()函数,使用%d表示整数。

for (int i =0 ; i<10; i++)
{
    CString szPath;
    szPath.Format(TEXT("\\Device\\Serial%d"),i);
    // ...
}

Second Problem: There could be many reasons this would fail. 第二个问题:这可能有很多原因导致失败。 Most likely of which would be having not created the combo box yet.(It needs a window handle before it can add strings) To figure out the cause of the debug assertion, click the "retry" button on the Debug Assertion Failed window and it should jump to the code which caused the assertion. 很可能还没有创建组合框。(在添加字符串之前需要一个窗口句柄)要找出调试断言的原因,请单击Debug Assertion Failed窗口上的“重试”按钮,然后单击它应该跳转到导致断言的代码。 For example it might be something like: 例如,它可能是这样的:

ASSERT(GetSafeHwnd()!=NULL);

Your combo box class won't be 'subclassed' until after the first DoDataExchange is called (and any attempt to use it before that happens will ASSERT). 在调用第一个DoDataExchange之后,您的组合框类将不会被“子类化”(并且在此之前尝试使用它将是ASSERT)。 Either wait until the base class has run OnInitDialog or do something like this: 要么等到基类运行OnInitDialog,要么执行以下操作:

CComboBox * pcombo = static_cast<CComboBox*>(GetDlgItem( IDC_MYCOMBO ));
pcombo->AddString( szPath );

See @TheSteve's answer for string problem. 请参阅@ TheSteve的字符串问题答案。

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

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