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
枚举类型定义了下面几个值:
OK、OKCancel、YesNo、
YesNoCancel、RetryCancel、
AbortRetryIgnore |
|
MessageBoxIcon |
指定消息框中出现哪个图标的
枚举常量。MessageBoxIcon枚举类
型定义了下面几个值:Asterisk、
Exclamation、Error、Hand、Information、
None、Question、Stop、Warning |
静态Show()函数有大量重载的版本,有的非常简单,只有一个String^类型的形参,而有的相当复杂,包含多达10个形参。
如果编译并执行该示例,然后不恰当地设定极限值,那么将看到与图22-18类似的窗口。
|
| (点击查看大图)图 22-18 |
如您所见,自动获得用于滚动列表框中项列表的滚动栏。注意,滚动到给定的项并不是选中该项。必须单击该项以选择它,然后才可以单击OK按钮。中间有白色交叉线的圆形红色图标是Show()函数的第四个实参指定的,只有一个OK按钮是第三个实参造成的结果。
为显示消息框而调用的Show()函数返回DialogResult类型的值,该值指出关闭消息框的是哪一个按钮。可以使用该返回值来决定在消息框关闭之后做什么。在设置极限值对话框OK按钮的lottoOK_Click()处理程序中,可以使用显示消息框的Show()函数返回的值,决定是否关闭设置极限值的对话框:
- System::Void lottoOK_Click(System::Object^ sender, System::EventArgs^ e)
- {
- int upper = 0;
- int lower = 0;
- // If there's a currently selected upper limit item, save it
- if(lottoUpperList->SelectedItem != nullptr)
- upper = safe_cast<Int32>(lottoUpperList->SelectedItem);
-
- // If there's a currently selected lower limit item, save it
- if(lottoLowerList->SelectedItem != nullptr)
- lower = safe_cast<Int32>(lottoLowerList->SelectedItem);
-
- if(upper - lower < 5)
- {
- ::DialogResult result =
- MessageBox::Show(L"Upper limit:" + upper + L" Lower limit:" + lower +
- L"\nUpper limit must be at least 5 greater that the lower
- limit."+
- L"\nTry Again.",
- L"Limits Invalid",
- MessageBoxButtons::OKCancel,
- MessageBoxIcon::Error);
- if(result == ::DialogResult::OK)
- DialogResult = ::DialogResult::None;
- else
- DialogResult = ::DialogResult::Cancel;
- }
- else
- {
- upperupperLimit = upper;
- lowerlowerLimit = lower;
- }
- }
因为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菜单项的事件处理程序:
- System::Void resetMenuItem_Click(System::Object^ sender,
- System::EventArgs^ e)
- {
- if(lottoTab->Visible)
- {
- // Reset user limits for Lotto
- lottoUserMaximum = lottoUpperLimit;
- lottoUserMinimum = lottoLowerLimit;
- lottoLimitsDialog->UpperLimit = lottoUpperLimit;
- lottoLimitsDialog->LowerLimit = lottoLowerLimit;
- }
- else if(euroTab->Visible)
- {
- // Reset user limits for Euromillions
- euroUserMaximum = euroUpperLimit;
- euroUserMinimum = euroLowerLimit;
- euroStarsUserMaximum = euroStarsUpperLimit;
- euroStarsUserMinimum = euroStarsLowerLimit;
-
- // Code to update Euromillions limits dialog...
- }
- }
该函数只是重置Form1对象相应字段中的极限值,然后相应更新对话框对象中的属性。我们还必须给该函数添加代码来处理第二个对话框的重置问题,此对话框还没有添加到应用程序中,将用来处理Euromillions彩票极限值的输入问题。
现在可以重新编译该程序,并试着在修改Lotto彩票的极限值之后对其进行重置。选择Limits | Reset菜单项可将两个极限值复位到初始值。