Multiple Modal Dialogs in VC6? - c++

I'm working on an application that allows users to edit invoices generated in other parts of the app. When viewing an invoice, if Edit>Edit Invoice is selected, a modal dialog is launched
void CViewInvoiceView::OnEditEditinvoice()
{
CEditInvoiceDlg dlg;
if (dlg.DoModal() == IDOK)
{
// Do Stuff
}
}
This works fine. However, due to a recent spec change, I now need to extract the fields related to shipping information, and make them editable in a separate dialog accessible by clicking a Edit Shipping button contained in the first dialog.
void CEditInvoiceDlg::OnButtonEditshipping()
{
CEditInvoiceShippingDlg shippingDlg;
shippingDlg.m_shipToList = &m_shipToList;
if (shippingDlg.DoModal() == IDOK)
{
// Do Stuff
}
}
My problem is that I can't get the second dialog (CEditInvoiceShippingDlg) to open. The message map looks ok
BEGIN_MESSAGE_MAP(CEditInvoiceDlg, CDialog)
...
ON_BN_CLICKED(IDC_BUTTON_EDITSHIPPING, OnButtonEditshipping)
...
END_MESSAGE_MAP()
but if I place a break point in my OnButtonEditshipping() function, it never stops on that point. Clicking the Edit Shipping button actually closes the dialog it's contained in instead of opening a second.

Look in your resource.h file and make sure there aren't two IDs assigned to the same number. You should also check to make sure none of them are in reserved ranges: MSDN
TN020: ID Naming and Numbering Conventions

Related

How to manually show CMFCToolBarComboBoxButton sub-menu?

Standard behaviour for CMFCToolBarComboBoxButton is to have a clickable button plus a drop-down arrow for displaying a submenu. I want to show the submenu independently of where the click was made. How can I do it?
My code to create the button is, more or less, the following (it has been extracted from a larger project, so I apologize for any missing not-too-important piece of code):
// In class declaration:
CMenu m_menu;
CMFCToolBar m_toolbar;
// Where toolbar initialization takes place:
m_menu.CreateMenu();
// ... populate menu
// ID_BUTTON is the ID in the resource file for the toolbar button, 0 is the index for the button icon
CMFCToolBarMenuButton button(ID_BUTTON, m_menu.GetSafeHmenu(), 0);
m_toolbar.ReplaceButton(ID_BUTTON, button);
I've been looking around for awhile and cannot find a related answer.
The solution happened to be very straightforward, just call the OnClick function of the CMFCToolBarComboBoxButton button from its associated ON_COMMAND.
// ... message map
ON_COMMAND(ID_BUTTON, OnToolbarMenuButtonClicked)
// ...
void MyWnd::OnToolbarMenuButtonClicked()
{
const int index = m_toolbar.CommandToIndex(ID_BUTTON);
auto button = (CMFCToolBarComboBoxButton*)m_toolbar.GetButton(index);
button->OnClick(NULL, TRUE);
}
This behaviour is not documented and, contrary to what common sense told me, it doesn't create an infinite recursive call. It seems that the "main" button is still controlled by CMFCToolBarButton, while just the "arrow-button" is controlled by the CMFCToolBarComboBoxButton.
PS: obviously, and out of the scope of the question, the OnToolbarMenuButtonClicked can be used for a very different purpose, such as the default action while the sub-menu contains other less-frequent options.

MFC - Changing dialog item focus programmatically

I have a Modeless dialog which shows a bunch of buttons; some of these are customized to draw stuff with GDI.
Now, when the user clicks on a customized one under certain conditions, a message box appears to alert user of the error and this is fine.
The problem is that after accepting the Message Box (showed as MB_ICON_ERROR), everywhere I click in the dialog, I always get the error message as if the whole dialog send the message to the customized button and the only way to get rid this is to press tab and give the focus to another control.
This is a strange behaviour and knowing why happens wouldn't be bad, but a simple workaround for now should do the job.
Since the moment that is probably a matter of focus, I've tried to set it on another control (in the owner dialog) by doing:GetDlgItem( IDC_BTN_ANOTHER_BUTTON )->SetFocus();
and then, inside the customized control by adding:KillFocus( NULL );but had no results.
How should I use these functions?
Thanks in advance.
PS: if I comment the AfxMessageBox, the control does not show this bizarre behaviour.
EDITI'll show some code as requested.
// This is where Message Box is popping out. It is effectively inside the dialog code.
void CProfiloSuolaDlg::ProcessLBtnDownGraphProfilo(PNT_2D &p2dPunto)
{
// m_lboxProfiles is a customized CListBox
if(m_lboxProfiles.GetCurSel() == 0)
{
// This profile cannot be modified.
/*
CString strMessage;
strMessage.Format( _T("Default Profile cannot be edited.") );
AfxMessageBox( strMessaggio, MB_ICONERROR );
*/
return;
}
// Selecting a node from sole perimeter.
SelectNodo(p2dPoint);
}
Actually, the message is commented to keep the dialog working.
// This is inside the customization of CButton
void CMyGraphicButton::OnLButtonDown(UINT nFlags, CPoint point)
{
PNT_2D p2dPunto;
CProfiloSuolaDlg* pDlg = (CProfiloSuolaDlg*)GetParent();
m_pVD->MapToViewport(point,p2dPunto);
switch(m_uType)
{
case GRF_SEZIONE:
pDlg->ProcessLBtnDownGraphProfilo(p2dPunto);
break;
case GRF_PERIMETRO:
pDlg->ProcessLBtnDownGraphPerimetro(p2dPunto);
break;
}
CButton::OnLButtonDown(nFlags, point);
}
Since you are handling the button down event in the button handler for the custom control, you don't need to call the base class. Just comment out CButton::OnLButtonDown(nFlags, point).

CheckBox value in different file

I need to access the checkbox value in a different program. Check box is initiated in ToolDlg.cpp
DDX_Control(pDX, IDC_CalculateTBA, m_CalculateTBA);
in the oninitdialog initiated like this:
m_CalculateTBA.SetCheck(0);
CalculateAnalyticTBA = false;
void CToolDlg::OnBnClickedCheck3()
{
CalculateAnalyticTBA = m_CalculateTBA.GetCheck();
}
I need checkbox value in SetCal.cpp program. Here is the code i am trying in this program:
CToolDlg dialog;
if( dialog.CalculateAnalyticTBA )
{
Do some thing
}
But the dialog.CalculateAnalyticTBA is always tru even though i don't check the check box.
Plz let me know if you need any other info. Thanx for help.
It looks like you're creating a dialog, and never showing it. So CalculateAnalyticTBA has whatever value you gave it in the constructor of your CToolDlg class (or, if you didn't, whatever value the compiler gave it).
In order for this value to be set you must at least create the dialog so that the Data Exchange code (which invokes the DDX_Control and handles the binding of the checkbox and the variable) has a chance to run.
The correct way is to create and display the CToolDlg dialog and wait for the user to select his choices then only process the choices when the user finally click the "OK" button.
CToolDlg dialog;
// create and display the dialog
if (dialog.DoModal()==IDOK)
{ // user clicked the ok button, now do the work
....
}

Sharing variable among class instances

class MyApp : public CWinApp {
afx_msg OnPrefrences();
};
OnPrefrences() get called when user selects tools->Preference from the menubar.
Now in one dialog(Say DlgX) there is one button, on clicking this I need to open the Preference dialog which has in fact many panes, but here I need to open the Preference dialog by selecting one the these pane as active. Also in that particular pane I need to hide some of the controls only when It gets open through this the dialog not through menu.
So I have created one variable(Say m_varX) in MainFrm class.
void DlgX::OnButtonXClick()
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if(pFrame)
{
pFrame->m_varX = TRUE;
((CMyApp*)(AfxGetApp()))->OnPrefrences();
pFrame->m_varX = FALSE;
}
}
And in button handler of DlgX I have made this m_varX TRUE and call the OnPreference() and after closing of this preference dialog I have made m_varX FALSE.
All this is working fine... But the problem is that things gets clutter in mainFrm. Also the project I am working on is legacy one so I cant make much changes.
Is there any patter available for handling such case?
Thanks
You could solve this with a custom dialog (if you don't have it already)
When you show the dialog from the main menu i.e. onPreferences() you fill and show all 'panes'. you would have to do a custom dialog where the ctor takes some arguments.
E.g.
enum { all, part };
void MainFrame::OnPreferences()
{
CMyPreferences dlg( GetDocument(), all );
dlg.DoModal();
}
but when you call it from within a dialog you only fill in the parts you need.
void YourDialog::OnPreferences()
{
CMyPreferences dlg( GetDocument(), part );
dlg.doModal();
}
The argument could be something more sophisticated for more fine tuned configuration of what to show/allow to edit.
I think for that special case, even if sometimes is no more considered a pattern, the singleton pattern would work for you.

Best way to Enable/Disable CMenu items in real time

I'm working on a project using Visual C++ 6.0, and I need to be able to enable or disable certain menu items depending on the permissions assigned to the currently logged in user. This is the code I'm using:
// If the currently logged in user doesn't have permission to edit invoices
if (!((CMyApp *)AfxGetApp())->UserHasPermission(PERMISSION_EditInvoice))
{
// Disable the Edit Menu
pMain->EnableMenuItem(1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
}
else
{
// Enable the Edit Menu
pMain->EnableMenuItem(1, MF_BYPOSITION | MF_ENABLED);
}
It does exactly what I want it to do, however I'm trying to find the best place to put it. If I put it in OnInitialUpdate(), I get the results I want, but only for the first invoice opened. If you open a second invoice without closing and re-opening the dialog, the code is not executed again. OnUpdate() isn't called when opening a different invoice, and the only other place I've found that works is OnDraw(), the problem with OnDraw() is that the menu item doesn't visually change state from Grayed out to Enabled or vice versa until you try to click it.
I think you must include this code in a procedure
void check_user_permission();
than you must call it when this events occur:
- OnInitialUpdate()
- new user login (if your software permits user login/logout during the same session)
- new invoice opened
Can it help?
I ended up deciding to disable the Edit Invoice menu item, instead of the Edit menu itself. This proved much easier and cleaner, as it determines permission and enables or disables item every time the main 'Edit menu is opened.
void CViewInvoiceView::OnUpdateEditEditinvoice(CCmdUI* pCmdUI)
{
// If the currently logged in user doesn't have permission to edit invoices
if (!((CJ3App *)AfxGetApp())->UserHasPermission(PERMISSION_EditInvoice))
{
// Disable the Edit Menu
pCmdUI->Enable(false);
}
else
{
// Enable the edit menu
pCmdUI->Enable();
}
}