【问题标题】:C# Button with DialogResult Retry带有 DialogResult 重试的 C# 按钮
【发布时间】:2019-03-08 11:14:06
【问题描述】:

我将使用两个具有DialogResult Retry 的按钮。当您按下按钮时,winform 将隐藏,做一些事情,它会再次弹出。为此,我使用 While 方法。但是,如果您有两个带有重试的按钮,除非您将一个按钮 DialogResult 设置为 Yes 并执行 While 方法,否则这将不起作用。但是有没有更好的方法来做到这一点,大小写切换之类的?

注意这是在一个类中

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;

【问题讨论】:

  • 你能多解释一下吗?我不明白你有什么问题!
  • 不要使用 DialogResult,而是简单地引发一个事件。如果您需要帮助,请尝试使用谷歌搜索“c# 如何声明自己的事件”。

标签: c# dialogresult


【解决方案1】:

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

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

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

只检查DialogResult.Retry,循环就OK了。

【讨论】:

  • 您好,谢谢!但我的意思是当我按下按钮“加载门”时,openFileDialog 就会出现。完成后 mainForm 返回。当我按下“下载门”时,会显示 saveFileDialog,完成后返回 mainForm。当两者都设置为重试时,它始终使用第一个对话框。我会稍微修改一下我的问题
【解决方案2】:

您可以尝试使用此代码来管理您的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.

【讨论】:

    【解决方案3】:

    你可以试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      相关资源
      最近更新 更多