繁体   English   中英

C#Winform重试

[英]C# Winform Retry

我有一个重试表格的问题。 通过此表格,我可以从互联网上加载文件。 完成后,主窗体再次弹出。 到现在为止还挺好。

但是,当用户按“取消”时,将再次弹出“保存”对话框。 但是,当用户按下“取消”按钮时,我想要的是他返回到主表单。

我认为应该在这一部分中:

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

这是我的代码的一部分:

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;

每次调用saveFileDialog1.ShowDialog()都会打开对话框。 您需要存储调用结果,以便以后检查:

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;
    }

我认为您可以轻松地使用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