Jump to content
  • Advertisement
Sign in to follow this  
da_cobra

MFC : 2 dialogs : PLEASE help me!

This topic is 4913 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

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.h

class CWhatever
{
// constructors/methods and so on

private:
CRecordListyThingDlg *m_pListyThingDlg;
};



and in the implementation

//Whatever.cpp

BOOL 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 up
void CWhatever::~CWhatever()
{
delete m_pDlg;
}



hope that helps


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use QT [grin]

http://www.trolltech.com/products/qt/index.html

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
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. 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.h

class CNewDialog : public CDialog
{
CNewDialog(const CYourDBClass& db)
// dialog stuff

private:
// make it const if you only want to display data
const CYourDBClass& m_db;
}

// NewDialog.cpp

CNewDialog::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.



Share this post


Link to post
Share on other sites
ok, thx for the help, I'll try that last solution first tonight and I will keep you guys updated on if it works or not

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!