Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys.I create a model dialog using DialogBoxParam.But when I want to add string to list box,it didn't work.I hope you can help me.Tnk..Here is my code.
C#
void FillListBox(HWND hwnd, UINT filter) {
    SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_RESETCONTENT,0,0 );

    int index;
    for ( int i=0; i<numMessages; i++) {
        if ( msginfo[i].fType & filter ) {
//          index = (int)SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );
//          index = ::SendMessage( ::GetDlgItem(hwnd,IDC_MESSAGES),LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );
            index = ListBox_AddString( GetDlgItem(hwnd,IDC_MESSAGES),msginfo[i].pWmMsg );
            SendDlgItemMessage( hwnd,IDC_MESSAGES,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType );
        }
    }
}

Whatever method I use,it didn't work at all.Why??
Posted
Comments
Andrew Brock 28-Mar-11 2:27am    
In addition to what Hans said:
Make sure the hwnd you pass in is the dialog not some other control like the list box.
Make sure this is called after the list box is created, at the earliest in OnInitDialog(), not in the constructor.
Comment out SendDlgItemMessage(LB_SETITEMDATA) for testing

I cant see a really problem in your code. But you should distinguish between combo and list box.
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#ifdef _WIN64
  #define  ON64(C,E)  C
#else
  #define  ON64(C,E)  E
#endif

#ifdef _DEBUG
  #define  ASSERT(B)  if(!(B)){ __int2c(); }
#else
  #define  ASSERT(B)  
#endif

static LPCDLGTEMPLATE __dlgResource(const int idr,HINSTANCE h)
{
  HRSRC      hrsrc = FindResource(h,MAKEINTRESOURCE(idr),RT_DIALOG);
  HGLOBAL    hres = hrsrc?LoadResource(h,hrsrc):0;
  return (LPCDLGTEMPLATE)(hres?LockResource(hres):0);
}

void FillListBox(HWND hwnd,const int idc,UINT filter);
void FillComboBox(HWND hwnd,const int idc, UINT filter);

ON64(INT_PTR,int) FAR PASCAL __dlgProc(HWND h,unsigned int m,WPARAM w,LPARAM l)
{
  switch(m)
  {
    case WM_CLOSE: EndDialog(h,0); break;
    case WM_COMMAND:
      switch(LOWORD(w))
      {
        case IDOK:
        case IDCANCEL: EndDialog(h,LOWORD(w)); break;
      }
    break;
    case WM_INITDIALOG:
      FillListBox(h,101,0x0002);
      FillComboBox(h,102,0x0002);
    break;
  }
  return 0;
}

int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
  DialogBoxIndirectParam(h,__dlgResource(1,h),0,__dlgProc,0);
  return 0;
}

  struct
  {
    const TCHAR*  pWmMsg;
    unsigned int  fType;
  } msginfo[] =
  {
    { __T("first")  ,0x001 },
    { __T("second")  ,0x003 },
    { __T("third")  ,0x007 },
  };

  const int  numMessages = sizeof(msginfo)/sizeof(msginfo[0]);

void FillListBox(HWND hwnd,const int idc,UINT filter)
{
  HWND    hlb = ::GetDlgItem(hwnd,idc);
  int     index,i,done;

  ASSERT(IsWindow(hlb)); // invalid window?
  if(IsWindow(hlb))
  {
    SendMessage(hlb,LB_RESETCONTENT,0,0 );
    for(done=i=0;i<numMessages;i++)
    {
      if(msginfo[i].fType & filter)
      {
        index = SendMessage(hlb,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg);
        SendMessage(hlb,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType);
        ++done;
      }
    }
    if(0==done)
    {
      index = SendMessage(hlb,LB_ADDSTRING,0,(LPARAM)__T("(empty)"));
      SendMessage(hlb,LB_SETITEMDATA,(WPARAM)index,(LPARAM)0);
    }
  }
}

void FillComboBox(HWND hwnd,const int idc, UINT filter)
{
  HWND    hcb = ::GetDlgItem(hwnd,idc);
  int     index,i,done;

  ASSERT(IsWindow(hcb)); // invalid window?
  if(IsWindow(hcb))
  {
    SendMessage(hcb,CB_RESETCONTENT,0,0 );
    for(done=i=0;i<numMessages;i++)
    {
      if(msginfo[i].fType & filter)
      {
        index = SendMessage(hcb,CB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg);
        SendMessage(hcb,CB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType);
        ++done;
      }
    }
    if(0==done)
    {
      index = SendMessage(hcb,CB_ADDSTRING,0,(LPARAM)__T("(empty)"));
      SendMessage(hcb,CB_SETITEMDATA,(WPARAM)index,(LPARAM)0);
    }
  }
}

Good luck.
 
Share this answer
 
Comments
Leasangle 28-Mar-11 12:41pm    
I finally find out my mistake.I changed the Owner Draw behavior at the listbox's properties.My god.What a stupid mistake.Anyway,thanks for you help.I've learn much from you code.
Possible problems:
1. hwnd is not a window (use IsWindow() to check)
2. IDC_MESSAGES is not a control - see (1)
3. numMessages is zero
4. msginfo[i].fType & filter is never true
5. msginfo[i].pWmMsg is NULL or empty string

With a little more error checking and the debugger it should be easy to find out what the problem is.
 
Share this answer
 
Comments
Leasangle 28-Mar-11 2:07am    
I debugged it many times.Everything that you mention is right.But the list box is always empty.I guess I miss sth that seems unimportantly.But I cann't find out.
Albert Holguin 28-Mar-11 10:02am    
something I've also bumped into is hwnd is the WRONG window and the message gets lost.
Leasangle 28-Mar-11 12:45pm    
Thanks.Guyes.There's nothing wrong in my code.I changed the Owner Draw behavior at the listbox's properties.That mess my up.Och!!Thanks for your help.
I just tried the following code:

C++
void FillListBox(HWND hwnd, UINT filter)
{
    SendDlgItemMessage( hwnd,IDC_NOTE,LB_RESETCONTENT,0,0 );

    int index;
    for ( int i=0; i < numMessages; i++)
    {
        if ( msginfo[i].fType & filter )
        {
            index = (int)SendDlgItemMessage( hwnd,IDC_NOTE,LB_ADDSTRING,0,(LPARAM)msginfo[i].pWmMsg );
            SendDlgItemMessage( hwnd,IDC_NOTE,LB_SETITEMDATA,(WPARAM)index,(LPARAM)msginfo[i].fType );
        }
    }
}

and it worked fine. Check the values of all the variable items like filter and numMessages to ensure you are using the right values.
 
Share this answer
 
Comments
Leasangle 28-Mar-11 12:42pm    
I find out what's wrong.I changed the Owner Draw behavior at the listbox's properties.Thanks for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900