设为首页 加入收藏

TOP

21.4.6 实现对话框切换(2)
2013-10-07 12:43:02 来源: 作者: 【 】 浏览:69
Tags:21.4.6 实现 对话 切换

21.4.6  实现对话框切换(2)

该函数首先获得指向该视图的父框架(应用程序的CMainFrame对象)的指针,然后使用该指针调用SelectView()函数,选择新订单处理对话框。源文件中必须添加嵌入MainFrm.h的#include指令,以获得CMainFrame的定义。MainFrm.h文件中还需要有嵌入ViewConstants.h头文件的#include指令,使NEW_ORDER在此处也可用。

CCustomerView类中的Select Products按钮的处理程序使屏幕切换到CProductsView的对话框:

  1. void CCustomerView::OnSelectproducts()  
  2. {  
  3. static_cast<CMainFrame*>(GetParentFrame())->SelectView(SELECT_PRODUCT);  

同一个类中Cancel按钮的处理程序只是切换到前一个视图:

  1. void CCustomerView::OnCancelorder()  
  2. {  
  3. static_cast<CMainFrame*>(GetParentFrame())->SelectView(ORDER_DETAILS);  

不要忘记在CustomerView.cpp文件中添加嵌入MainFrm.h头文件的#include指令。

最后要实现的切换操作在CProductView类的OnDone()处理程序中:

  1. void CProductView::OnDone()  
  2. {  
  3. static_cast<CMainFrame*>(GetParentFrame())->SelectView(ORDER_DETAILS);  

该函数切换到原来的允许浏览和编辑订单细节的应用程序视图。如果愿意,也可以切换到CCustomerView对话框继续录入新订单。再次提醒一下,不要忘记添加嵌入MainFrm.h头文件的#include指令。

从最初允许浏览订单细节的对话框到编辑订单细节对话框的切换操作,现在必须控制New Order按钮的可见性;否则Cancel按钮在编辑对话框中将藏在New Order按钮下面。在COrderDetailsView类中添加一个对应于IDC_NEWORDER ID的控件变量m_NewOrderCtrl,然后将OnEditorder()处理程序修改成:

  1. void COrderDetailsView::OnEditorder()  
  2. {  
  3. if(m_pSet->CanUpdate())  
  4. {  
  5. try  
  6. {  
  7. if(m_Mode == UPDATE)  
  8. { // When button was clicked we were in update  
  9. // Disable input to edit controls  
  10. m_QuantityCtrl.SetReadOnly();  
  11. m_DiscountCtrl.SetReadOnly();  
  12.  
  13. // Change the Update button text to Edit Order  
  14. m_EditOrderCtrl.SetWindowText(_T("Edit Order"));  
  15.  
  16. // Make the Cancel button invisible  
  17. m_CancelEditCtrl.ShowWindow(SW_HIDE);  
  18.  
  19. // Show the new order button  
  20. m_NewOrderCtrl.ShowWindow(SW_SHOW);  
  21.  
  22. // Complete the update  
  23. m_pSet->Update();  
  24. m_Mode = READ_ONLY; // Change to read-only mode  
  25. }  
  26. else  
  27. { // When button was clicked we were in read-only mode  
  28.  
  29. // Enable input to edit controls  
  30. m_QuantityCtrl.SetReadOnly(FALSE);  
  31. m_DiscountCtrl.SetReadOnly(FALSE);  
  32.  
  33. // Change the Edit Order button text to Update  
  34. m_EditOrderCtrl.SetWindowText(_T("Update"));  
  35.  
  36. // Hide the new order button  
  37. m_NewOrderCtrl.ShowWindow(SW_HIDE);  
  38.  
  39. // Make the Cancel button visible  
  40. m_CancelEditCtrl.ShowWindow(SW_SHOW);  
  41.  
  42. // Start the update  
  43. m_pSet->Edit();  
  44. m_Mode = UPDATE; // Switch to update mode  
  45. }  
  46. }  
  47. catch(CException* pEx)  
  48. {  
  49. pEx->ReportError();     // Display the error message  
  50. }  
  51. }  
  52. else  
  53. AfxMessageBox(_T("Recordset is not updatable."));  

现在,根据m_UpdateMode存储的值是READ_ONLY还是UPDATE,相应地隐藏或显示New Order按钮。还必须在OnCancel()处理程序中使该按钮可见:

  1. void COrderDetailsView::OnCancel()  
  2. {  
  3. m_pSet->CancelUpdate();     // Cancel the update operation  
  4. m_EditOrderCtrl.SetWindowText(_T("Edit"));  // Switch button text  
  5. m_CancelEditCtrl.ShowWindow(SW_HIDE);   // Hide the Cancel button  
  6. m_NewOrderCtrl.ShowWindow(SW_SHOW); // Show the New Order button  
  7. m_QuantityCtrl.SetReadOnly(TRUE); // Set state of quantity edit control  
  8. m_DiscountCtrl.SetReadOnly(TRUE); // Set state of discount edit control  
  9. m_UpdateMode = !m_UpdateMode; // Switch the mode  
我们迄今所做的事情是为了实现基本的视图切换机制。还需要回头添加处理数据库更新操作的代码;但此刻应该尝试编译并运行一下已有的代码,以消除任何引入的打字错误或其他错误。在程序可以工作之后,应该能够滚动客户和产品记录。一定要确保检查了所有切换路径。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇21.4.2 创建资源 下一篇21.4.6 实现对话框切换(1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: