设为首页 加入收藏

TOP

21.4.9 为订单选择产品
2013-10-07 12:47:00 来源: 作者: 【 】 浏览:64
Tags:21.4.9 订单 选择 产品

21.4.9  为订单选择产品

当显示选择产品的视图时,希望显示订单ID和客户名称的控件所对应的变量已经被设置为适当的数值。我们打算使用文档对象的Order成员获得这些值。为此,可以给CProductView类添加一个函数。可以将其命名为InitializeView(),该函数的返回类型为void。可以从应用程序的CMainFrame对象拥有的SelectView()成员中调用该函数。这样可以确保这两个控件总是在显示产品选择对话框之前被初始化。

在实现InitializeView()函数之前,需要考虑另外一件事情。新的Orders表记录仅当为了初次给该订单添加产品而单击Select Product按钮时才应该添加。以后的按钮单击只应该给该订单添加另一种产品,因此需要某种方法来确定单击Select Product按钮时Orders表是否已经添加过新订单。要做到这一点,可以给CProductView类添加一个bool类型的变量m_OrderAdded。该变量最初为false,由Select Product按钮的处理程序将其设置为true。因此,给CProductView类添加该变量。可以在InitializeView()成员中初始化m_OrderAdded变量,InitializeView()函数的实现如下所示:

  1. void CProductView::InitializeView()  
  2. {  
  3. // Get a pointer to the document  
  4. CDBSimpleUpdateDoc* pDoc =   
  5. static_cast<CDBSimpleUpdateDoc*>(GetDocument());  
  6. m_OrderID = pDoc->m_Order.m_OrderID;  
  7. m_CustomerName = pDoc->m_Order.m_ShipName;  
  8. m_Quantity = 1;         // Must order at least 1  
  9. m_Discount = 0;         // No default discount  
  10. m_OrderAdded = false; // Order not added initially  
  11. UpdateData(FALSE); // Transfer data to controls  

该函数通过复制文档对象中Order成员的适当成员的数值,初始化视图类中对应订单ID和客户名称控件的成员。该函数还可以确保订购数量和折扣控件最初拥有适当的初值。任何产品的订购数量都必须至少是1,而折扣值默认是0。正如先前所看到的那样,以实参值FALSE调用继承的UpdateData()成员,使数据从类变量传输到控件上。需要在该源文件的开始处添加嵌入DBSimpleUpdateDoc.h头文件的#include指令,从而使Document类定义可用。

为了使该函数投入使用,只需在切换到产品选择对话框时调用InitializeView()即可。做这件事的位置显然是在CMainFrame类的SelectView()成员中:

  1. void CMainFrame::SelectView(ViewID viewID)  
  2. {  
  3. CView* pOldActiveView = GetActiveView();        
    // Get current view  
  4.  
  5. // Get pointer to new view if it exists  
  6. // if it doesn't the pointer will be null  
  7. CView* pNewActiveView = static_cast<CView*>(GetDlgItem(viewID));  
  8.  
  9. // If this is first time around for the new view, the new view  
  10. // won't exist, so we must create it  
  11. // The Order Details view is always created first so we don't need  
  12. // to provide for creating that.  
  13. if (pNewActiveView == NULL)  
  14. {  
  15. switch(viewID)  
  16. {  
  17. case NEW_ORDER: // Create view to add new order  
  18. pNewActiveView = new CCustomerView;  
  19. break;  
  20. case SELECT_PRODUCT:        // Create view to add product to order  
  21. pNewActiveView = new CProductView;  
  22. break;  
  23. default:  
  24. AfxMessageBox(_T("Invalid View ID"));  
  25. return;  
  26. }  
  27.  
  28. // Switching the views  
  29. // Obtain the current view context to apply to the new view  
  30. CCreateContext context;  
  31. context.m_pCurrentDoc = pOldActiveView->GetDocument();  
  32. pNewActiveView->Create(NULL, NULL, 0L, CFrameWnd::rectDefault,  
  33. this, viewID, &context);  
  34. pNewActiveView->OnInitialUpdate();  
  35. }  
  36. SetActiveView(pNewActiveView);      // Activate the new view  
  37. if(viewID==NEW_ORDER)  
  38. static_cast<CCustomerView*>(pNewActiveView)->SetNewOrderID();  
  39. else if(viewID == SELECT_PRODUCT)  
  40. static_cast<CProductView*>(pNewActiveView)->InitializeView();  
  41.  
  42. pOldActiveView->ShowWindow(SW_HIDE);        // Hide the old view  
  43. pNewActiveView->ShowWindow(SW_SHOW);        // Show the new view  
  44. pOldActiveView->SetDlgCtrlID(m_CurrentViewID);    
    // Set the old view ID  
  45. pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);  
  46. m_CurrentViewID = viewID; // Save the new view ID  
  47. RecalcLayout();  

当形参viewID的值是SELECT_PRODUCT时,CProductView类中对应订单ID和客户名称控件的变量将被初始化,同时被初始化的还有控制着是否在Orders表中创建新记录的bool变量。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇22.1 创建应用程序API 下一篇21.4.10 添加新订单(1)

评论

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