tags:

views:

61

answers:

3

For example, in an MFC program, I have my main application and a 'class'. What should I do if I want to update a control (say, a listbox) that is situated on my main application from that 'class'?

A: 

Your class can be designed to trigger an event which your main application can listen for. Then, a listener/event handler/delegate can be called to handle the event and update the listbox. Typically, most event formats pass a reference of the sender, in this case your 'class', as well as an object containing event arguments. These arguments can be used to pass the list of items you want to add to your listbox.

Soviut
How do i do that? Sorry just a beginner here.
Soviut is talking in C# terms, not MFC.
Jason Williams
Jason is correct. I'm doing my best to give you the gist on how to communicate between two separate entities. In the C#/ActionScript/Java world that usually means employing custom events.
Soviut
A: 

If you have the handle to dialog object in your class, then you can use GetDlgItem(ResourceID) to get list control object.

Naveen
This only applies to controls in dialogs - not in main windows.
Jason Williams
But the MSDN documentation says that the first parameter (window handle) need not be a dialog box.
Naveen
A: 

The easiest approach is to expose the listview from your application form/window to the classes that use it. You can do this either by passing the listview object (or parent window) to the class constructor, or storing it in a static variable that is accessible by the class.

For better encapsulation, you can put a method in the application that the class can call, e.g. "AddItemToListBox()". This allows the application object to remain in control of how you access the listbox. Again you can do this as a static method, or pass the main program object's 'this' pointer into the class constructor.

i.e.

class CApplication
{
    CListBox m_ListBox;

public:
    static void CApplication::AddItemToListBox(CString itemText)
    {
        // Add the item as you wish here
    }

}

class CMyClass
{
    afx_msg void CMyClass::OnMouseDown(...)
    {
          CApplication::AddItemToListBox("This is a test");
    }
}
Jason Williams