Handle double-click event in a dialog

Handle double-click event in a dialog

Post by csektor200 » Fri, 05 Dec 2003 22:20:29


I have an MDI app, which contains some menus. One of the menu command
let me open a dialog box (CMyListDialog), which contains a list box
with a number of items.

I have added a double click handler (LBN_DBLCLK), which allow the user
to double click on a particular item, and then display the result in a
child frame.
On the dialog, there are also an "OK" and "Cancel" buttons. So
basically, this handler servers the same purpose as the OK button.

I am handling the OK event (when the user clicks on the OK button)
within CMainFrame, something like this:

void CMainFrame::OnPressOK()
CMyListDialog listDlg;
if (listDlg.DoModal() == IDOK)
// Code for creating new child frame
// and display selected result

In the double-click event handler, which is added for the list box in
the class CMyListDialog, is where I'm a little bit unsure. What should
I do here to return the selected item to the main frame? I have added
an event handler for the OK button, so should I just call that
function. My thinking is that since the double-clicking is effectively
the same as clicking the OK button, I can just simply call the OK

Any help would be much appreciated.



Handle double-click event in a dialog

Post by Frank Hick » Sat, 06 Dec 2003 03:47:26

Simple data items can easily be used from a dialog resource, here's how I
generally retrieve items from dialog boxes...

void CMainFrame::OnPressOk()
CMyListDialog listDlg( this );
if ( listDlg.DoModal() == IDOK )
CString strVal;
int nVal;
MY_DATA myData;
strVal= listDlg.m_strSomeString;
nVal= listDlg.m_nSomeInt;
myData= listDlg.m_MyData; // May require copy constructor,
depends on what's in structure...

// TODO: Do something with data...

Since your using data from a listbox and processing a double-click from the
dialog I do something like this...

void CMyDialog::OnDblClickLB()
{ // Pseudo code:
// Make sure listbox item that was double-clicked is selected...
// Call OnOk...

void CMyDialog::OnOk()
{ // Pseudo code:
// Get selected item from listbox...
// If no valid item selected return without ending dialog...
// Fix up any required member variables from selected item...
// Call base class OnOk...

In your case it also appears that you want to open a new view to relay
additional information to the user based on what was selected in the dialog.
This gets a little trickier depending on what you actually want to do. If
your adding a view to an existing doc/view you may want to add a splitter
window and show an additional view conveying the new information. If it's
an entirely new doc/view you may just want to open a new document (after
setting up the data) and displaying that.

Frank Hickman
NobleSoft, Inc.
Replace the _nosp@m_ with @ to reply.


Handle double-click event in a dialog

Post by csektor200 » Sat, 06 Dec 2003 23:11:51

i Frank,

Thanks for the reply.

Retrieving the information from the dialog box back to the main frame
is no problem for me, if I invoke the dialog box from within
CMainFrame. But what happen if I was to call the dialog box from
another class, yet being able to detect/process the IDOK event in

As your example code shows:

This is if I invoke the dialog box from CMainFrame, which I can
process the IDOK event. If I was to invoke CMyListDialog from another
class, say, CSomeOtherClass, then I would have the above code in this
class. However, CMainFrame does not know about it, because the event
is handled in CSomeOtherClass. This is where I'm stuck at.

How can I let CMainFrame know about that the IDOK event has fired in
another class? Maybe it's something quite simple, but I can't seem to
find examples of how to do this.

If anyone can point me in the right direction, I would really
appreciate it.


"Frank Hickman" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...

Handle double-click event in a dialog

Post by Scott McPh » Sat, 06 Dec 2003 23:36:40

You can call CMainFrame functions from anywhere. MFC provides a global
function that returns a pointer to the CMainFrame object:

CMainFrame* p = (CMainFrame*)AfxGetMainWnd();

Scott McPhillips [VC++ MVP]

Handle double-click event in a dialog

Post by Anan » Sun, 07 Dec 2003 01:38:25

You can have a user defined message say

#define UWM_OKCLICKED WM_USER+<some value>

and you can send this message when OK is clicked


Hope that helps..


Handle double-click event in a dialog

Post by csektor200 » Sun, 07 Dec 2003 11:53:02

That did it Scott. Worked like a charm.

Thank you both for your advices. World class.


Handle double-click event in a dialog

Post by csektor200 » Sun, 07 Dec 2003 15:20:36

Thanks Anand. I have got it working now, based on Scott's tip. Your
suggestion is interesting too, I will keep that in mind. Much