如何使用 Yes/No MessageBox 的结果?
How to use the result from a Yes/No MessageBox?
我正在尝试制作一个程序,为 "event" 预留和显示可用和不可用的座位。我得到的错误如下:
'System.Nullable' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)
("No" 也一样)和
'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'.
这是我目前拥有的:
private void btnSeat1_Click(object sender, RoutedEventArgs e)
{
if (!Seat1)
{
DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (Result == DialogResult.Yes)
{
MessageBox.Show("You Reserved this seat");
btnSeat1.Text = "Reserved";
}
else if (Result == DialogResult.No)
{
Environment.Exit(0);
}
注意:我只使用 Environment.Exit 作为占位符。这是故意的,将相应更改。这不是问题的根源。
这一行:
DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
实际上应该是这样的:
var Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
然后,如果您将鼠标悬停在 Result
上,您可以看到它不是 DialogResult
,而是 MessageBoxResult
,或者,如果您更愿意明确键入它,请尝试:
MessageBoxResult Result = MessageBox.Show(...
因此您可以在 if 语句中像这样使用它:
if (Result == MessageBoxResult.Yes)
{
MessageBox.Show("You Reserved this seat");
btnSeat1.Text = "Reserved";
}
else if (Result == MessageBoxResult.No)
{
Environment.Exit(0);
}
你收到错误是因为 DialogResult
实际上是 Window
class 的 属性,而你正试图像使用它的类型一样使用它(如编译器说)。
我正在尝试制作一个程序,为 "event" 预留和显示可用和不可用的座位。我得到的错误如下:
'System.Nullable' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)
("No" 也一样)和
'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'.
这是我目前拥有的:
private void btnSeat1_Click(object sender, RoutedEventArgs e)
{
if (!Seat1)
{
DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (Result == DialogResult.Yes)
{
MessageBox.Show("You Reserved this seat");
btnSeat1.Text = "Reserved";
}
else if (Result == DialogResult.No)
{
Environment.Exit(0);
}
注意:我只使用 Environment.Exit 作为占位符。这是故意的,将相应更改。这不是问题的根源。
这一行:
DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
实际上应该是这样的:
var Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
然后,如果您将鼠标悬停在 Result
上,您可以看到它不是 DialogResult
,而是 MessageBoxResult
,或者,如果您更愿意明确键入它,请尝试:
MessageBoxResult Result = MessageBox.Show(...
因此您可以在 if 语句中像这样使用它:
if (Result == MessageBoxResult.Yes)
{
MessageBox.Show("You Reserved this seat");
btnSeat1.Text = "Reserved";
}
else if (Result == MessageBoxResult.No)
{
Environment.Exit(0);
}
你收到错误是因为 DialogResult
实际上是 Window
class 的 属性,而你正试图像使用它的类型一样使用它(如编译器说)。