0
我試圖創建一個對話框,用戶可以在某些按鈕之間進行選擇,但現在遇到的問題是,當用戶關閉窗口時(不是選擇按鈕而是使用在右上角的x),應用程序顯示消息,但之後它崩潰。有人知道我在這裏做錯了嗎?錯誤時重新詢問對話框
MainWindow.xaml.cs
public partial class MainWindow : Window
{
string[,] suppliers = new string[3,2] {{"xxx", "xxx"}, {"yyy", "yyy"}, {"zzz" , "zzz"}};
public MainWindow()
{
InitializeComponent();
ButtonPrompt buttonPrompt = new ButtonPrompt(suppliers, "Select supplier.");
while (buttonPrompt.ShowDialog() != true)
{
MessageBox.Show("Please choose one of the suppliers!");
}
}
}
ButtonPrompt.xaml.cs:提前
public partial class ButtonPrompt : Window
{
public ButtonPrompt(string[,] buttons, string question)
{
InitializeComponent();
buttonStack.Children.Clear();
TextBlock questionBlock = new TextBlock();
questionBlock.Text = question;
buttonStack.Children.Add(questionBlock);
for (int i = 0; i < buttons.GetLength(0); i++)
{
Button inputButton = new Button();
inputButton.Name = buttons[i, 0];
inputButton.Content = buttons[i, 1];
inputButton.Width = 200;
inputButton.Height = 60;
inputButton.Click += inputButton_Click;
buttonStack.Children.Add(inputButton);
if (i == 0)
{
inputButton.Focus();
}
}
}
private void inputButton_Click(object sender, RoutedEventArgs e)
{
Button inputButton = (Button)sender;
this.DialogResult = true;
}
private void Window_Closed(object sender, EventArgs e)
{
this.DialogResult = false;
}
}
謝謝!
感謝您的迴應,我現在正在使用您推薦的第二種可能性,它的功能就像一個魅力! ;) – Kryptoxx 2013-05-06 11:46:28
@TomVandenbussche做得好! – 2013-05-06 11:52:48