To display a confirmation dialog box in a WPF application using C#, you can use the MessageBox
class, which provides a static Show
method to display a message box with a specified message, title, buttons, and icons.
Here's an example of how to use the MessageBox
class to display a confirmation dialog box:
MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this item?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // Perform the delete operation } else { // Cancel the delete operation }
In this example, we call the MessageBox.Show
method to display a confirmation dialog box with a message asking the user if they want to delete an item. We specify the title of the message box using the "Confirm Delete" string, and the buttons and icon using the MessageBoxButton.YesNo
and MessageBoxImage.Question
values, respectively.
The MessageBox.Show
method returns a MessageBoxResult
value that indicates which button the user clicked. We can check the value of this result to determine if the user clicked the "Yes" or "No" button, and perform the appropriate action.
Note that the MessageBox
class is part of the System.Windows.MessageBox
namespace, which is only available in WPF applications. If you are developing a WinForms or console application, you can use the System.Windows.Forms.MessageBox
class instead.
// Code Implementation: MessageBoxResult result = MessageBox.Show("Do you want to proceed?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // Code for 'Yes' option } else { // Code for 'No' option or default behavior }
// Code Implementation: var confirmationDialog = new CustomConfirmationWindow("Do you want to proceed?"); confirmationDialog.ShowDialog(); if (confirmationDialog.DialogResult.HasValue && confirmationDialog.DialogResult.Value) { // Code for confirmation } else { // Code for cancellation or default behavior }
// Code Implementation (ViewModel): public ICommand ShowConfirmationCommand => new RelayCommand(ShowConfirmation); private void ShowConfirmation() { var result = MessageBox.Show("Do you want to proceed?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // Code for 'Yes' option } else { // Code for 'No' option or default behavior } }
// Code Implementation: var result = await ConfirmationDialog.ShowAsync("Do you want to proceed?"); if (result) { // Code for confirmation } else { // Code for cancellation or default behavior }
// Code Implementation: var result = dialogService.ShowConfirmation("Do you want to proceed?"); if (result) { // Code for confirmation } else { // Code for cancellation or default behavior }
// Code Implementation: var result = CustomMessageBox.Show("Do you want to proceed?", "Confirmation", MessageBoxButtonCustom.YesNoCancel); if (result == MessageBoxCustomResult.Yes) { // Code for 'Yes' option } else { // Code for 'No' or 'Cancel' option or default behavior }
// Code Implementation: var result = await MaterialDesignThemes.Wpf.DialogHost.Show(new ConfirmationDialog()); if ((bool)result) { // Code for confirmation } else { // Code for cancellation or default behavior }
// Code Implementation: var result = await AnimatedConfirmationDialog.Show("Do you want to proceed?"); if (result) { // Code for confirmation } else { // Code for cancellation or default behavior }
// Code Implementation: var result = TextBoxConfirmationDialog.Show("Please enter your comment:"); if (!string.IsNullOrEmpty(result)) { // Code for confirmation with user input } else { // Code for cancellation or default behavior }
// Code Implementation: var result = await TimeoutConfirmationDialog.Show("Do you want to proceed?", TimeSpan.FromSeconds(10)); if (result) { // Code for confirmation } else { // Code for timeout or default behavior }
connection-close safari thread-safety 32-bit division indexing ng2-admin viewcontroller amazon-rds codable