设为首页 加入收藏

TOP

22.1.10 使用对话框(2)
2013-10-07 12:45:32 来源: 作者: 【 】 浏览:73
Tags:22.1.10 使用 对话

22.1.10  使用对话框(2)

现在,该函数把在ListBox对象中选中的数值存入局部变量lower和upper中。如果两个值的差小于5,则显示一个消息框,然后通过将DialogResult属性的值设置为None,禁止关闭该对话框。MessageBox类中静态的Show()函数显示由该函数的实参定制的消息框。这里使用的Show()函数版本接受4个如表22-1所述的实参。

表  22-1

< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

   

String^

消息框中显示的文本

String^

消息框的标题栏中出现的文本

MessageBoxButtons

指定消息框中出现哪些按钮

的枚举常量。MessageBoxButtons

枚举类型定义了下面几个值:

OKOKCancelYesNo

YesNoCancelRetryCancel

AbortRetryIgnore

MessageBoxIcon

指定消息框中出现哪个图标的

枚举常量。MessageBoxIcon枚举类

型定义了下面几个值:Asterisk

ExclamationErrorHandInformation

NoneQuestionStopWarning


静态Show()函数有大量重载的版本,有的非常简单,只有一个String^类型的形参,而有的相当复杂,包含多达10个形参。

如果编译并执行该示例,然后不恰当地设定极限值,那么将看到与图22-18类似的窗口。

 
(点击查看大图)图  22-18

如您所见,自动获得用于滚动列表框中项列表的滚动栏。注意,滚动到给定的项并不是选中该项。必须单击该项以选择它,然后才可以单击OK按钮。中间有白色交叉线的圆形红色图标是Show()函数的第四个实参指定的,只有一个OK按钮是第三个实参造成的结果。

为显示消息框而调用的Show()函数返回DialogResult类型的值,该值指出关闭消息框的是哪一个按钮。可以使用该返回值来决定在消息框关闭之后做什么。在设置极限值对话框OK按钮的lottoOK_Click()处理程序中,可以使用显示消息框的Show()函数返回的值,决定是否关闭设置极限值的对话框:

  1. System::Void lottoOK_Click(System::Object^ sender, System::EventArgs^ e)  
  2. {  
  3. int upper = 0;  
  4. int lower = 0;  
  5. // If there's a currently selected upper limit item, save it  
  6. if(lottoUpperList->SelectedItem != nullptr)  
  7. upper = safe_cast<Int32>(lottoUpperList->SelectedItem);  
  8.  
  9. // If there's a currently selected lower limit item, save it  
  10. if(lottoLowerList->SelectedItem != nullptr)  
  11. lower = safe_cast<Int32>(lottoLowerList->SelectedItem);  
  12.  
  13. if(upper - lower < 5)  
  14. {  
  15. ::DialogResult result =  
  16. MessageBox::Show(L"Upper limit:" + upper + L" Lower limit:" + lower +  
  17. L"\nUpper limit must be at least 5 greater that the lower   
  18. limit."+  
  19. L"\nTry Again.",  
  20. L"Limits Invalid",  
  21. MessageBoxButtons::OKCancel,  
  22. MessageBoxIcon::Error);  
  23. if(result == ::DialogResult::OK)  
  24. DialogResult = ::DialogResult::None;  
  25. else  
  26. DialogResult = ::DialogResult::Cancel;  
  27. }  
  28. else  
  29. {  
  30. upperupperLimit = upper;  
  31. lowerlowerLimit = lower;  
  32. }  

因为Show()函数的第三个实参是MessageBoxButtons::OKCancel,所以该消息框现在有两个如图22-19所示的按钮。

 
(点击查看大图)图  22-19

在设置极限值对话框OK按钮的Click事件处理程序中, 将Show()函数的返回值存入result。result的类型必须使用作用域解析运算符指定,否则将被编译器解释为lottoLimitsDialog对象的DialogResult属性,程序代码将不能编译。如果result包含::DialogResult::OK值,就把lottoLimitsDialog对象的DialogResult属性设置为::DialogResult::None,这样可避免对话框关闭,使用户能够修改极限值。否则,把对话框的DialogResult属性设置为::Dialog::Cancel,这与单击对话框的Cancel按钮具有相同的结果,因此对话框将关闭。

2. Reset菜单项的事件处理程序

可以像下面这样实现Reset菜单项的事件处理程序:

  1. System::Void resetMenuItem_Click(System::Object^ sender,   
  2. System::EventArgs^ e)  
  3. {  
  4. if(lottoTab->Visible)  
  5. {  
  6. // Reset user limits for Lotto  
  7. lottoUserMaximum = lottoUpperLimit;  
  8. lottoUserMinimum = lottoLowerLimit;  
  9. lottoLimitsDialog->UpperLimit = lottoUpperLimit;  
  10. lottoLimitsDialog->LowerLimit = lottoLowerLimit;  
  11. }  
  12. else if(euroTab->Visible)  
  13. {  
  14. // Reset user limits for Euromillions  
  15. euroUserMaximum = euroUpperLimit;  
  16. euroUserMinimum = euroLowerLimit;  
  17. euroStarsUserMaximum = euroStarsUpperLimit;  
  18. euroStarsUserMinimum = euroStarsLowerLimit;  
  19.  
  20. // Code to update Euromillions limits dialog...  
  21. }  

该函数只是重置Form1对象相应字段中的极限值,然后相应更新对话框对象中的属性。我们还必须给该函数添加代码来处理第二个对话框的重置问题,此对话框还没有添加到应用程序中,将用来处理Euromillions彩票极限值的输入问题。

现在可以重新编译该程序,并试着在修改Lotto彩票的极限值之后对其进行重置。选择Limits | Reset菜单项可将两个极限值复位到初始值。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇22.1.6 添加上下文菜单 下一篇22.1.10 使用对话框(1)

评论

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