Confirm box for submission

Pankaj tripathi 185 Reputation points
2023-10-27T19:33:20.93+00:00

I have a form with some textboxes and a button . on button press , a new form opens as a showdialog() which has two buttons yes or no . Now i want to code in such a way in which if the user presses yes the data should be submitted to the database and the new form and the mainform both should close , but if he presses no , only the new form should close .

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. gekka 12,206 Reputation points MVP Volunteer Moderator
    2023-10-27T22:50:05.75+00:00

    DialogResult property of clicked button set to result from ShowDialog.

    namespace WinFormsApp1
    {
        using System;
        using System.Windows.Forms;
    
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form subform = new Form() { Width = 200, Height = 100, FormBorderStyle = FormBorderStyle.FixedToolWindow };
                {
                    Button yesButton = new Button() { Text = "&Yes" };
                    yesButton.DialogResult = DialogResult.Yes;
    
                    Button noButton = new Button() { Text = "&No" };
                    noButton.DialogResult = DialogResult.No;
                    noButton.Left = yesButton.Width + 5;
    
                    subform.Controls.Add(yesButton);
                    subform.Controls.Add(noButton);
                }
    
    
                DialogResult result = subform.ShowDialog();
                switch (result)
                {
                case DialogResult.Yes:
                    // your database code here
    
                    this.Close();
                    break;
                case DialogResult.No:
                    break;
                default:
                    break;
                }
            }
        }
    }
    
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-10-28T01:10:48.2866667+00:00

    Consider the following class and method Question. Full source.

    public class Dialogs
    {
        public static void Question(Control owner, string heading, Action yesAction, Action noAction)
        {
    
            TaskDialogButton yesButton = new("Yes") { Tag = DialogResult.Yes };
            TaskDialogButton noButton = new("No") { Tag = DialogResult.No };
    
            var buttons = new TaskDialogButtonCollection
            {
                yesButton,
                noButton
            };
    
    
            TaskDialogPage page = new()
            {
                Caption = "Question",
                SizeToContent = true,
                Heading = heading,
                Icon = new TaskDialogIcon(Properties.Resources.QuestionBlue),
                Buttons = buttons
            };
    
            var result = TaskDialog.ShowDialog(owner, page);
    
            if ((DialogResult)result.Tag! == DialogResult.Yes)
            {
                yesAction?.Invoke();
            }
            else
            {
                noAction?.Invoke();
            }
    
        }
    
    }
    

    Usage in a form. Two Actions and a button click to call the dialog above which executes the proper action dependent on the button clicked.

    public void DataOperations()
    {
        // do data operations
    }
    public void Terminate()
    {
        // here we are in the main form so Close terminates the app
        Close();
    }
    private void ActionQuestionButton_Click(object sender, EventArgs e)
    {
        Dialogs.Question(this, "Ask something", DataOperations, Terminate);
    }
    

    Screenshot

    dialog

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.