mfc >> GetDlgItem Problem

by Scott McPhillips [MVP] » Tue, 21 Mar 2006 21:45:48 GMT

PeterOut wrote:
> I am using MSVC++ on Windows ME and have the following event handler
> for when I click on an item in a list box on a dialog box I have built.
>
> void CStereoControlPtsDlg::OnSelchangeInputImages()
> {
> CWnd *cwListBox=GetDlgItem(IDC_INPUT_IMAGES);
>
> *lpNumberOfDocumentsSelected = (long)cwListBox->SendMessage((UINT)
> LB_GETSELITEMS,
> (WPARAM)(lpNumberOfDocumentsSelected), (LPARAM) (lpSelectedIndices));
>
> }
>
> IDC_INPUT_IMAGES is the ID for the list box.
>
> For some reason I get funny values for cwListBox. That is
> cwListBox=0x00bc3ed0
> cwListBox->m_hWnd=0x00000e6c
>
> When I call cwListBox->SendMessage, the program crashes at the
> following point.
> _AFXWIN_INLINE LRESULT CWnd::SendMessage(UINT message, WPARAM wParam,
> LPARAM lParam)
> { ASSERT(::IsWindow(m_hWnd)); return ::SendMessage(m_hWnd, message,
> wParam, lParam); }
> and returns the message.


It looks like you are passing a pointer in WPARAM. A count is expected.

Your code is a mess and the error was caused by failing to use basic MFC
technique. The entire thing should look like this:

int n = m_ListBox.GetSelItems(count, lpSelectedIndices);

Since this requires no casts the compiler would have detected your error.

--
Scott McPhillips [VC++ MVP]



mfc >> GetDlgItem Problem

by PeterOut » Wed, 22 Mar 2006 10:10:26 GMT





You are absolutely correct. That was the problem. Thanks very much.




Similar Threads

1. GetDlgItem Problem

2. Problem when removing GetDlgItem

Hello, all.

I've been handed an old code base, written using VC++ 1.52C and MFC.

I'm trying to clean it up and one of the things I'm trying to do is to get
rid of GetDlgItem as Joseph Newcomer advises.

Today I ran into a problem.

I added controls for two listboxes c_BaselineLB and c_TargetLB.

I replaced all the listbox pointers uses in the code and got
the following error only when I tried to use AddString() or
SetItemData() with the controls.  All other uses of the controls are
fine.

error C2662: 'SetItemData' : cannot convert 'this' pointer from 'const class
::CListBox __far *' to 'class ::CListBox __far *const '

To work around this I had to do something like the following:

((CListBox*)(&c_TargetLB))->SetItemData((c_TargetLB.GetCount() - 1), acqID);

rather than simply c_TargetLB.SetItemData((c_TargetLB.GetCount() - 1),
acqID);
which generates the error.

What am I missing here?

Thanks for any light you can shed upon this,



3. GetDlgItem() returns NULL

4. Weird results using GetDlgItem function

Can someone please explain why this problem occurred.

My program was failing on this line.  Even though I have used almost exact
code in other dialogs in the same program and it works fine.

CWnd* pStaticControl = GetDlgItem(IDC_SOMESTATICCONTROL);

so I switched it to

CWnd* pStaticControl =
static_cast<CWnd*>(GetDlgItem(IDC_SOMESTATICCONTROL));

after that it worked fine so I decided to switch it back to the original
version expecting it to fail.  To my surprise it worked.

Have mercy on me I'm a newbie with C++

thanks


5. avoiding use of UpdateData() and GetdlgItem() with radiobuttons ???

6. GetDlgItem for CComboBox in Rebar?

A combo box works better if it is not in a dialog.  You can set it up like
the following:

Combo box is defined in the .h as:

 CComboBoxEx  m_wndAddressCombo;

 // Create a combo box for the address bar
 if (!m_wndAddressCombo.Create(CBS_DROPDOWN | WS_CHILD | CBS_AUTOHSCROLL,
  CRect(0,0,200,400), this, IDC_ADDRESSBAR))
 {
  TRACE0("Failed to create combobox\n");
  return -1;      // fail to create
 }

// Add stuff to the combo box however

 if (!m_wndReBar.Create(this) ||
  !m_wndReBar.AddBar(&m_wndAddressCombo, "Address", NULL,
RBBS_NOGRIPPER|RBBS_FIXEDBMP | RBBS_BREAK) ||
  !m_wndReBar.AddBar(&m_wndToolBar,      NULL,          NULL,
RBBS_NOGRIPPER|RBBS_FIXEDBMP | RBBS_BREAK))
 {
  TRACE0("Failed to create rebar\n");
  return -1;      // fail to create
 }


Then you can access it with:

 m_wndAddressCombo.GetWindowText(strData);

The rebar will also resize the combo box to fit the size of the window which
is nice.

HTH,

Tom

"Michael B. Allen" < XXXX@XXXXX.COM > wrote in message
news:066001c37e22$83aa74c0$ XXXX@XXXXX.COM ...
> I want to get the text of the combo box in a toolbar. If I
> do the following:
>
>   CComboBox *combo = (CComboBox *)GetDlgItem(IDC_TARGET);
>   CString target;
>   combo->GetWindowText(target);
>
> it asserts here:
>
>   void CWnd::GetWindowText(CString& rString) const
>   {
>     ASSERT(::IsWindow(m_hWnd));  <-- error
>
> The combo box was created using the resource editor. I
> just created a dialog, dragged a combo box into it, and
> gave it an ID of IDC_TARGET.
>
> So how do I do this?
>
> Thanks,
> Mike


7. Cache hWnd from ::GetDlgItem

8. GetDlgItem and CStatic derived control

Hello,

I have a CDialog with some custom CStatic controls inside (CStatic with
SetTextColor,SetBkColor, ..) and I would like to do something like this

GetDlgItem( IDC_LABEL1 )->SetFont("Arial", 20, FW_BOLD);
GetDlgItem( IDC_LABEL1 )->SetWindowText( m_csLabel1 );

but it cannot work since GetDlgItem returns a pointer to a CWnd.
So I tried :


CxStatic* pStatic;
pStatic = (CxStatic*)GetDlgItem( IDC_LABEL1 );
pStatic->SetFont("Arial", 20, FW_BOLD);
pStatic->SetWindowText( m_csLabel1 );


But I get a Unhandled exception 0xxxxxxx : Access violation reading 
location 0xcdcdcdcd.

And finally if I do :

CxStatic* pStatic;
pStatic = (CxStatic*)GetDlgItem( IDC_LABEL1 );
pStatic->SetFont("Arial", 20, FW_BOLD);
m_staLabel1.SetWindowText( m_csLabel1 ); //  with m_staLabel1 a control 
variable associated to IDC_LABEL1 it works fine.

Any idea ?