簡體   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