简体   繁体   English

C#Winform重试

[英]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. 每次调用saveFileDialog1.ShowDialog()都会打开对话框。 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 : 我认为您可以轻松地使用if ... else语句,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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