C#按钮与DialogResult重试

时间:2018-10-03 09:02:26

标签: c# dialogresult

我将使用两个具有DialogResult重试功能的按钮。当您按下按钮时,winform将隐藏,执行某些操作,然后再次弹出。我为此使用While方法。但是,如果您有两个按钮进行重试,则除非将一个按钮DialogResult设置为“是”并执行While方法,否则此方法将不起作用。但是,是否有更好的方法来执行此操作(例如大小写切换)?

Image of buttons

请注意,这是在班级内

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;

3 个答案:

答案 0 :(得分:0)

您可以在代码中而不是在表单设计器中设置DialogResult。只需双击按钮并添加如下内容:

private void button1_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.Retry;
}

这样,两个按钮将具有相同的DialogResult

仅通过检查DialogResult.Retry,循环就可以了。

答案 1 :(得分:0)

确实,您可以尝试使用以下代码来管理DialogResult,例如:

switch(MessageBox.Show("Text", "Title", MessageBoxButtons.YesNo))
{
    case DailogResult == DialogResult.Yes:
         //Do something
    case DailogResult == DialogResult.Retry:
         //Do something
}

实际上,对于两个Button对象,您必须具有两个event-handler对象,并且可以设置:

DialogResult = DialogResult.Retry;

如果您想Retry.

答案 2 :(得分:0)

您可以尝试以下方法:

var dialogResult =      DialogResult.Retry;
while (dialogResult == DialogResult.Retry) {
    try {
        CheckSomething();
        break;
    }
    catch {

        if (dialogResult == DialogResult.Abort) {secondDialog.DialogResult = Retry;}
        throw;
    }
}

您还可以使用如下枚举:

enum Result {Ignore, Abort,Retry};