简体   繁体   中英

C# Winform Retry

I have a problem with a Retry form. With this Form, I'm able to load a file from the internet. When it's done the main form pops up again. So far so good.

But when the user presses cancel, the save dialog pops up again. But what I want when the user presses the cancel button, he returns to the main form.

It Should be in this part, I think:

if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
{
    return Result.Cancelled;
}

This is a part of my code:

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;

            if (saveFileDialog1.ShowDialog() == 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 (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return Result.Cancelled;
            }
        }

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

    return Result.Succeeded;

Everytime you call saveFileDialog1.ShowDialog() the dialog is opened. You need to store the result of the call for later checks:

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;

    // store result of the dialog
    var dialogResult = saveFileDialog1.ShowDialog();

    if (dialogResult == DialogResult.OK)
    {
      // ...
    }

    // ...

    // compare the result without opening the dialog again.
    if (dialogResult == DialogResult.Cancel)
    {
        return Result.Cancelled;
    }

I think you can easily use if ... else statement for this like below :

System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    // your code
}
else // For Cancel
{
    return Result.Cancelled;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM