Click here to Skip to main content
15,902,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using VS2008 and I have a MFC Dialog application that contains a button and a List Control. The List Control has a control variable m_listctrl. The m_listctrl variable is declared in the public section of dialog.h.

When I click the button I call a function in another class. From within that function I need to access the m_listctrl variable so I can modify the contents of the List Control based on data in other functions within the class I am in.

If I insert a line in the class like this:
m_ListCtrl.InsertItem(0, _T("TESTING"))
I get a compile error stating the m_ListCtrl is an undeclared identifier. In my class file I have #defined the dialog.h. I have tried multiple work arounds with no luck.

I realize I could avoid all this by just working in the CDialog class. But that code is already over 800 lines long. I am just trying to break the code down into smaller pieces.

What is the proper way to access the control variable from outside the CDialog class?
Posted
Updated 5-Nov-10 9:17am
v3

1 solution

You would also need the reference to that CDialog object because m_ListCtrl is a declared identifier, and also instantiated object, there.

This is also quite logical if you consider that it would be impossible for the compiler to guess which m_ListCtrl you mean if at any time you got more than one object of CDialog. The reference to the specific CDialog object is therefor required.

It should be something like:
m_CDialog.m_ListCtrl.InsertItem(0, _T("TESTING"))


By the way, it would be much more safe to provide a method for adding a value from the dialog itself. This means you would create a CDialog.AddListItem(...). If you expose m_ListCtrl directly, other code can manipulate this control completely. With the your own method exposed from CDialog you prevent this and external code can only add a value, but not delete items, change the font or make any other unwanted changes to the list control.

Good luck!
 
Share this answer
 
Comments
2buck56 5-Nov-10 17:48pm    
I'm sorry but I don't understand what you mean. Where did you get m_CDialog from?

My actual dialog class is named CSmartBlockDlg. And m_ListCtrl is the identifier within that class.

I could add something like CSmartBlockDlg.AddListItem. But I will also be removing items. When that happens I will need to re-read the entire list back into the class I am in so I can use the results to repopulate an array that is being sent to a piece of hardware through the USB port. So I really need full access to the List Control from within my other class.
E.F. Nijboer 5-Nov-10 18:30pm    
The m_CDialog I used as placeholder variable for the CDialog (or in your case CSmartBlockDlg) and assumed that it would be clear that this was used as the instance of CSmartBlockDlg like m_ListCtrl. So you need to begin at the instance of CSmartBlockDlg as reference like CSmartBlockDlg_object.m_ListCtrl.InsertItem.
Also, I would still advice to expose only a limited set of methods and properties for accessing the list with methods that make it all as easy as possible. Code that looks like
MyDialog.MyListControl.Items.Add(0, _T("TESTING"));
MyDialog.MyListControl.Items[0].#setting some properties#
MyDialog.MyListControl.Items[0].#calling some methods#

It is way easier to have some methods doing this. Changing the list control is always done at one place and if you add some items somewhere else you won't get redundant and ugly code like above all scattered through out your application. This is of course your choice :-D
2buck56 5-Nov-10 21:55pm    
I couldn't get the reference method to work. I'm not sure exactly how that would be coded. I did this:

CSmartBlockDlg test;
test.m_ListCtrl.InsertItem(0, _T("TESTING"));

It compiles but when I run it I get a "Debug Assertion Failed" error.

I created a function in CSmartBlockDlg named CListAdd(char Data). When I run test.CListAdd(char) to call the function and pass it a char it runs with no problem and passes the char. So I guess I will create a CListDel and CListGet function and do it that way since it appears impossible to directly access the original m_ListCtrl variable.
E.F. Nijboer 7-Nov-10 9:08am    
CSmartBlockDlg test;
This is just an empty declaration, a placeholder for a new CSmartBlockDlg. You either have to set it to an instance or create a new instance. The way you are using it now the variable test is empty so will indeed give an error. You need a reference to the actual CSmartBlockDlg instance.
void DisplayDialog()
{
// Create and display an instance of the dialog box
CSmartBlockDlg^ dlg = gcnew CSmartBlockDlg();
// Fill m_ListCtrl
// Show the dialog and determine the state of the
// DialogResult property for the form.
if (dlg->ShowDialog() == DialogResult::OK )
{
// Do something here to handle data from dialog box.
}
}
For more info have a look here: http://msdn.microsoft.com/en-us/library/2chz8edb.aspx

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