C# Button with DialogResult Retry

1.7k views Asked by At

I'm going to use two buttons that have a DialogResult Retry. When you push the button the winform will hide, do something and it pops-up again. I use the While method for this. But if you have two buttons with a retry this won't work unless you set one button DialogResult to Yes and do a While method. But is there a better way to do this, case switch or something?

Image of buttons

Note this is within a Class

try
{
    // Create a form to select objects.
    DialogResult result = System.Windows.Forms.DialogResult.None;
    while (result == DialogResult.None || result == DialogResult.Retry)
    {
        // Picking Objects.
        if (result == DialogResult.Retry)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.FileName = "test";
            saveFileDialog1.Filter = "Family Files (*.rfa)|*.rfa|All Files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;

            var dialogResult = saveFileDialog1.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string address = "http://www.autodesk.com/revit-basic-sample-family-2017-enu?_ga=2.28765692.1750602280.1538397390-459409917.1521646598";

                System.Net.WebClient webClient = new System.Net.WebClient();
                webClient.DownloadFile(address, saveFileDialog1.FileName);

                Autodesk.Revit.DB.Family family = null;
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Load Family");
                    if (doc.LoadFamily(saveFileDialog1.FileName, out family))
                    {
                        String name = family.Name;
                        TaskDialog.Show("Revit", "Family file " + name + " has been loaded ");
                    }
                    else
                    {
                        TaskDialog.Show("Revit", "Can't load the family file or already exists.");
                    }
                    tx.Commit();
                }

            }

            if (dialogResult == DialogResult.Cancel)
            {

            }

        }

        // Show the dialog.
        using (testForm selectionForm = new vuurenForm(commandData))
        {
            result = selectionForm.ShowDialog();
        }
    }

    return Result.Succeeded;
3

There are 3 answers

1
Satory On

You may set the DialogResult in the code, not in the form designer. Just double click the buttons and add something like:

private void button1_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.Retry;
}

That way both buttons will have the same DialogResult.

Than the loop is OK with only checking for the DialogResult.Retry.

0
AmirReza-Farahlagha On

Exactly you can try this code to manage your DialogResult like:

switch(MessageBox.Show("Text", "Title", MessageBoxButtons.YesNo))
{
    case DailogResult == DialogResult.Yes:
         //Do something
    case DailogResult == DialogResult.Retry:
         //Do something
}

Actually for two Button objects you have to have two event-handler objects and you can set:

DialogResult = DialogResult.Retry;

In the event that you want to Retry.

0
Gauravsa On

You can try this:

var dialogResult =      DialogResult.Retry;
while (dialogResult == DialogResult.Retry) {
    try {
        CheckSomething();
        break;
    }
    catch {

        if (dialogResult == DialogResult.Abort) {secondDialog.DialogResult = Retry;}
        throw;
    }
}

You can also use enums like below:

enum Result {Ignore, Abort,Retry};