Sending message between objects
-
Hello, I have a dialog that contains some radio buttons and a list box I wondered how can I pass a message to another object that the user chaghed his selection by press one of the redio buttons, I need to call to a method of the second object accordeing the user selction . thanks
-
Hello, I have a dialog that contains some radio buttons and a list box I wondered how can I pass a message to another object that the user chaghed his selection by press one of the redio buttons, I need to call to a method of the second object accordeing the user selction . thanks
What is the second object?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Hello, I have a dialog that contains some radio buttons and a list box I wondered how can I pass a message to another object that the user chaghed his selection by press one of the redio buttons, I need to call to a method of the second object accordeing the user selction . thanks
It depends how the objects are subclassed (or derived) and who their parent is. Just about all buttons in MFC are CWnd derived, but their event handlers are usually managed by the parent window (usually some CDialog or some sort of form). If the second object that you're referring to is another button type of object, that is owned by the same parent window, then it's ok to make direct calls to access the other object. On the other hand, if that other object is in a separate dialog or form, then you should pass a message using SendMessage or PostMessage to the parent window and let it handle the event. It's going to make quite a big difference as to what these "objects" you are talking about are and who the owner is. A number of different solutions will probably work, but you should be careful and follow framework guidelines as you may end up with an unstable solution.
-
Hello, I have a dialog that contains some radio buttons and a list box I wondered how can I pass a message to another object that the user chaghed his selection by press one of the redio buttons, I need to call to a method of the second object accordeing the user selction . thanks
Its a simple process with a number of small steps. 1. Wire-up the message handling of the radio buttons so that you get a WM_NOTIFY (or whatever the pertinent message is) whenever the active radio-button in the group changes. 2. Call method of object 2 with the selected option passed in as a variable somehow - either as text, or (preferably) as an INT that represents the choice number. Here's a dialog-based app that demonstrates the principle. Whether you call the method of the second object when the selection changes or when an 'action' button is pressed, is up to you. (And yes, I do realize that as they're radio-buttons, one of the options should be checked by default - just can't remember how just this minute - it was easier and quicker to add a test case to check if an option had been selected yet or not :-O ) resource.rc
// Generated by ResEdit 1.5.8
// Copyright (C) 2006-2011
// http://www.resedit.net#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOGEX 6, 5, 194, 106
STYLE DS_3DLOOK | DS_CENTER | DS_SETFONT | WS_CAPTION | WS_VISIBLE | WS_GROUP | WS_THICKFRAME | WS_SYSMENU
CAPTION "Code::Blocks Template Dialog App"
FONT 8, "Tahoma", 0, 0, 1
{
PUSHBUTTON "&Test", IDC_BTN_TEST, 138, 5, 46, 15
PUSHBUTTON "&Quit", IDC_BTN_QUIT, 138, 29, 46, 15
GROUPBOX "Static", IDC_STATIC, 13, 16, 114, 68
AUTORADIOBUTTON "Radio Button 1", IDC_RADIO1, 38, 32, 63, 8
AUTORADIOBUTTON "Radio Button 2", IDC_RADIO2, 38, 45, 63, 8
AUTORADIOBUTTON "Radio Button 3", IDC_RADIO3, 38, 59, 63, 8
}resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif#define DLG_MAIN 101
#define IDC_BTN_TEST 1000
#define IDC_BTN_QUIT 1001
#define IDC_RADIO1 1002
#define IDC_RADIO2 1003
#define IDC_RADIO3 1004main.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>#include "resource.h"
HINSTANCE hInst;
int curRadioOption = -1;BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ -
What is the second object?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Its a simple process with a number of small steps. 1. Wire-up the message handling of the radio buttons so that you get a WM_NOTIFY (or whatever the pertinent message is) whenever the active radio-button in the group changes. 2. Call method of object 2 with the selected option passed in as a variable somehow - either as text, or (preferably) as an INT that represents the choice number. Here's a dialog-based app that demonstrates the principle. Whether you call the method of the second object when the selection changes or when an 'action' button is pressed, is up to you. (And yes, I do realize that as they're radio-buttons, one of the options should be checked by default - just can't remember how just this minute - it was easier and quicker to add a test case to check if an option had been selected yet or not :-O ) resource.rc
// Generated by ResEdit 1.5.8
// Copyright (C) 2006-2011
// http://www.resedit.net#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOGEX 6, 5, 194, 106
STYLE DS_3DLOOK | DS_CENTER | DS_SETFONT | WS_CAPTION | WS_VISIBLE | WS_GROUP | WS_THICKFRAME | WS_SYSMENU
CAPTION "Code::Blocks Template Dialog App"
FONT 8, "Tahoma", 0, 0, 1
{
PUSHBUTTON "&Test", IDC_BTN_TEST, 138, 5, 46, 15
PUSHBUTTON "&Quit", IDC_BTN_QUIT, 138, 29, 46, 15
GROUPBOX "Static", IDC_STATIC, 13, 16, 114, 68
AUTORADIOBUTTON "Radio Button 1", IDC_RADIO1, 38, 32, 63, 8
AUTORADIOBUTTON "Radio Button 2", IDC_RADIO2, 38, 45, 63, 8
AUTORADIOBUTTON "Radio Button 3", IDC_RADIO3, 38, 59, 63, 8
}resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif#define DLG_MAIN 101
#define IDC_BTN_TEST 1000
#define IDC_BTN_QUIT 1001
#define IDC_RADIO1 1002
#define IDC_RADIO2 1003
#define IDC_RADIO3 1004main.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>#include "resource.h"
HINSTANCE hInst;
int curRadioOption = -1;BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ -
Thanks for your replay , I didnt understand where is the calling to the method of object 2, if you can mark this section it will be helpfull for me. by the way I forgot to specify that I am using MFC but as I realized it doesnt matter. thanks
That's okay. In fact - I didn't make any call to the method of object 2. Please accept my apology for being unclear. As I alluded to earlier, I can see two places where such a call would make sense. In the first case, there would be action immediately after a radio button was pressed. I imagine you would not have the same need to keep track of the currently selected option, so I've omitted that code from the following case statements. Example 1: Immediate response
case IDC_RADIO1:
object2.handleSelectedRadioButton(1);
return true;case IDC_RADIO2:
object2.handleSelectedRadioButton(2);
return true;case IDC_RADIO3:
object2.handleSelectedRadioButton(3);
return true;In this second example, you'll need to remember which radio-button is selected somewhere, before taking the appropriate action when the trigger is received (in this case, the pressing of the button labelled Test) Example 2: Action taken in response to another input
case IDC\_BTN\_TEST: if (curRadioOption == -1) MessageBox(hwndDlg, "Select a radio button first!", "Error", MB\_ICONERROR); else object2.handleSelectedRadioButton(curRadioOption); return TRUE;
Something else I should mention, is that MFC will most likely allow the state of any control to be quickly and easily queried (not sure if there's an object used to represent a group of radio buttons - I'd imagine that there was) This will make my global variable
curRadioOption
redundant. -
Hello, I have a dialog that contains some radio buttons and a list box I wondered how can I pass a message to another object that the user chaghed his selection by press one of the redio buttons, I need to call to a method of the second object accordeing the user selction . thanks
Pass a pointer to the object you want to notify in the constructor of the dialog, and have the dialog save that in a member variable. Then when events occur, make a call to that object.