设为首页 加入收藏

TOP

22.1.9 创建对话框(2)
2013-10-07 12:45:17 来源: 作者: 【 】 浏览:65
Tags:22.1.9 创建 对话

22.1.9  创建对话框(2)

我们需要能够更新这两个属性,因为Limits | Reset菜单项的Click事件处理程序要修改极限值;还需要使ListBox对象获得当前选中的上限或下限。不但要把极限值存入类对象中,而且还要更新ListBox对象来反映新的极限值。

现在,可以在LottoLimitsDialog类中创建两个公有的、设置两个ListBox控件的成员函数:

  1. public:  
  2. void SetLowerLimitsList(int min, int max, int selected)  
  3. {  
  4. SetList(lottoLowerList, min, max, selected);  
  5. lowerLimit = selected;  
  6. }  
  7.  
  8. void SetUpperLimitsList(int min, int max, int selected)  
  9. {  
  10. SetList(lottoUpperList, min, max, selected);  
  11. upperLimit = selected;  

两个函数都使用SetList()函数来设定相应ListBox对象中的数值范围,然后把selected值存入存储极限值的成员中。

2. 处理对话框按钮事件

现在为OK按钮对象添加Click事件的事件处理程序函数,因此返回到LottoLimitsDialog窗体的Design选项卡,双击OK按钮添加骨架代码。

不需要为Cancel按钮添加Click事件的处理程序。单击该按钮的结果只是关闭对话框,并不需要其他动作。

可以按照如下代码来实现OK按钮的Click事件的处理程序:

  1. System::Void lottoOK_Click(System::Object^ sender, System::EventArgs^ e)  
  2. {  
  3. // If there's a currently selected upper limit item, save it  
  4. if(lottoUpperList->SelectedItem != nullptr)  
  5. upperLimit = safe_cast<Int32>(lottoUpperList->SelectedItem);  
  6.  
  7. // If there's a currently selected lower limit item, save it  
  8. if(lottoLowerList->SelectedItem != nullptr)  
  9. lowerLimit = safe_cast<Int32>(lottoLowerList->SelectedItem);  

该函数首先将来自ListBox对象lottoUpperList的上限值存入为此目的而添加的成员变量中。ListBox对象的SelectedItem属性返回Object^类型的句柄,使当前的选中项可用。作为预防措施,该函数还验证返回的句柄是否为空。在存储选中项之前,必须将其转换为实际的类型Int32。然后,自动拆箱功能负责将该对象再转换为整数。接下来,该处理程序以相同的方式存储来自另一个ListBox对象的下限值。当该处理程序结束执行之后,对话框将自动关闭。

3. 控制ListBox对象的状态

在响应Limits | Upper和Limits | Lower这两个菜单项的Click事件时,使用的是同一个对话框对象,但在任何一种情况下都不希望两个列表框都被修改。对于Upper菜单项事件来说,希望禁止下限的选择;而就Lower菜单项而言,希望禁用选择上限的列表框。为了使之成为可能,可以给LottoLimitsDialog类添加一对公有函数成员。下面是为Upper菜单项设置两个ListBox对象状态的函数:

  1. void SetUpperEnabled()  
  2. {  
  3. lottoUpperList->Enabled = true; // Enable upper list box  
  4. lottoLowerList->Enabled = false;    // Disable lower list box  

将lottoUpperList对象的Enabled属性设置为true,以允许用户与其交互。把lottoLowerList对象的Enabled属性设置为false,使之成为只读对象。

为Lower菜单项做的事情刚好相反:

  1. void SetLowerEnabled()  
  2. {  
  3. lottoUpperList->Enabled = false;    // Disable upper list box  
  4. lottoLowerList->Enabled = true; // Enable lower list box  

为了使该对话框对象在应用程序中表现出符合需要的行为,我们已经做了大量工作, 但还没有创建对话框对象。这项任务由应用程序窗口对象负责。

4. 创建对话框对象

Form1类的构造函数可以创建对话框对象,还可以初始化对话框中的ListBox对象。给Form1类添加一个私有成员,以存储指向对话框的句柄:

  1. private: LottoLimitsDialog^ lottoLimitsDialog; 

给Form1构造函数的函数体添加下面几行代码:

  1. lottoLimitsDialog = gcnew LottoLimitsDialog;  
  2. lottoLimitsDialog->SetLowerLimitsList(1, lottoUpperLimit  
  3. -lottoValuesCount+1,lottoUserMinimum);  
  4. lottoLimitsDialog->SetUpperLimitsList(lottoValuesCount, lottoUpperLimit,  
  5. lottoUserMaximum); 

这几行代码非常简单。第一条语句创建对话框对象。后面两条语句调用初始化ListBox对象中列表的函数。设置下限的ListBox对象中最大值的计算依据是必须允许创建数量符合要求的彩票号码。如果最大的号码是49,而要求的彩票号码个数是6,则最大的下限值必须是44;如果是更大的一个数,就不能创建6个不同的号码。相同的原因适用于上限最小值的确定,该数值不能小于彩票记录中号码的数量。两个列表框的选中项是lottoUserMinimum和lottoUserMaximum。

因为在Form1类的构造函数中引用了LottoLimitsDialog类名,所以需要在Form1.h文件中添加嵌入LottoLimitsDialog类定义的#include指令:

  1. #include "LottoLimitsDialog.h" 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇22.1.10 使用对话框(1) 下一篇22.1.9 创建对话框(1)

评论

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