MFC : 2 dialogs : PLEASE help me!
Hello, I really need help on this problem :
I'm trying to make a database program : On my main window I can already view/edit/add/change my records, but now I'm trying to create a second Dialog box on which I can also access my records.
I already created a new Dialog resource and derived this one from the class CRecordView.
But how can I open that dialog box in my program via a button?
In a tutorial I read that I need to use the following code :
CMyWnd *MyWnd = new CMyWnd() ;
myWnd->CreateEx(WS_EX_TOPMOST, strWndClass, "venster naam", WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME, CRect(100, 100, 200, 200), NULL, 0) ;
MyWnd->ShowWindow(SW_SHOWNORMAL) ;
MyWnd->UpdateWindow() ;
But I'm still left with some questions :
1) CMyWnd, which class is this? Is this the class of my new dialog box?
2) strWndClass, which string is this? according to msdn, I need to get this string via the function AfxRegisterWndClass(), but how do I do this?
Thanx in advance for any1 that can help me out here!
it really depends on what you want. do you want a modal dialog (takes control of app until dismissed) or modeless (sits happily in another window alongside main app).
also CRecordView is a view not a dialog. so just create a normal dialog class (derived from CDialog) and add a list control (or whatever) to display your records. let's call this dialog class CRecordListyThingDlg...
so for modal (the easy one)
in your button handler code for the main window
or modeless (slightly harder)
need more code, first in main window class declaration
and in the implementation
hope that helps
also CRecordView is a view not a dialog. so just create a normal dialog class (derived from CDialog) and add a list control (or whatever) to display your records. let's call this dialog class CRecordListyThingDlg...
so for modal (the easy one)
in your button handler code for the main window
void CWhatever::OnClickedShowListyThing(){ CRecordListyThingDlg dlg; // this line displays the dialog and will wait here until you close it if (dlg.DoModal() == IDOK) { // user has finished with dialog, do something useful }}
or modeless (slightly harder)
need more code, first in main window class declaration
// Whatever.hclass CWhatever{ // constructors/methods and so onprivate: CRecordListyThingDlg *m_pListyThingDlg;};
and in the implementation
//Whatever.cppBOOL CWhatever::OnInitDialog(){ // initialisation stuff // create the dialog but don't show it m_pDlg = new CRecordListyThingDlg(this); m_pDlg->Create(IDD_LISTY_DLG, this);}void CWhatever::OnClickedShowListyThing(){ m_pDlg->ShowWindow(SW_SHOW); m_pDlg->BringWindowToTop();}// make sure to clean upvoid CWhatever::~CWhatever(){ delete m_pDlg;}
hope that helps
Quote:Original post by Anonymous Poster
Use QT [grin]
http://www.trolltech.com/products/qt/index.html
wow that's helpful... the OP should abandon all the work he's done and switch to a new GUI. good idea!
well, my problem is, if I make a normal CDialog based dialog, I can't access my database anymore on that second dialog!
So if you know how I can access my database again in that second Dialog, I would be easier for me.
So if you know how I can access my database again in that second Dialog, I would be easier for me.
Quote:Original post by da_cobra
well, my problem is, if I make a normal CDialog based dialog, I can't access my database anymore on that second dialog!
So if you know how I can access my database again in that second Dialog, I would be easier for me.
The way I got around this problem is by having a global variable defined for the base dialog. Then you can access the data anywhere you want to. To do this, you will need to do a few things. I'll try to explain for now, but if you need code segments, I can put something together later, it's late now.
What you want to do is make an 'extern yourdialog *variable' in the .h file. In the new second dialog class, you will want to include that .h file, so you now have access to that variable. This is how you can access all the data. However, you will need to assign the pointer to the class in your main dialogs, init function, where it says to "Add additional initialization code". You can simply do variable = this; and be done. That's about it.
One thing to keep in mind is that the data you are accessing should be public - that is if you do not want to make a lot of Get/Set functions...
- Drew
Quote:Original post by Drew_BentonQuote:Original post by da_cobra
well, my problem is, if I make a normal CDialog based dialog, I can't access my database anymore on that second dialog!
So if you know how I can access my database again in that second Dialog, I would be easier for me.
The way I got around this problem is by having a global variable defined for the base dialog. Then you can access the data anywhere you want to. that is if you do not want to make a lot of Get/Set functions...
No offense, but IMO, that is not the best way to do it. Simply pass a reference or a pointer to your DB object when creating the new dialog. As for making everything public, I won't go into that, but there are any number of references which will tell you why this isn't the best idea.
To access the DB from your new dialog do this
// NewDialog.hclass CNewDialog : public CDialog{ CNewDialog(const CYourDBClass& db) // dialog stuffprivate: // make it const if you only want to display data const CYourDBClass& m_db;}// NewDialog.cppCNewDialog::CNewDialog(const CYourDBClass& db): m_db(db){}
m_db now refers to your db object without having to resort to a global and all the potential bugs that entails.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement