1

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;
Draken
  • 3,134
  • 13
  • 34
  • 54
vanlion
  • 111
  • 8

3 Answers3

1

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.

Draken
  • 3,134
  • 13
  • 34
  • 54
Satory
  • 34
  • 1
  • 5
  • Hi, Thanks! but what i mean is when i press the button "load door" the openFileDialog shows up. When finished mainForm returns. When i press "download door" the saveFileDialog shows up and after finish it you return to mainForm. When both have set to Retry it's always using the first dialog. I will edit my question a little bit – vanlion Oct 03 '18 at 10:03
0

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.

Draken
  • 3,134
  • 13
  • 34
  • 54
AmirReza-Farahlagha
  • 1,204
  • 14
  • 26
0

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};
Draken
  • 3,134
  • 13
  • 34
  • 54
Gauravsa
  • 6,330
  • 2
  • 21
  • 30