【问题标题】:Notify Modal forms' parent that it needs to action something通知模态表单的父级它需要采取行动
【发布时间】:2011-11-15 10:41:42
【问题描述】:

我有一个父表单打开一个模式表单,它基本上允许用户更改应用程序的数据库设置。

当用户单击模态(子)窗体上的保存按钮时,它会使用新设置保存设置对象,但我需要让主窗体检查数据库设置是否正确。

我目前通过一个尝试简单地连接到数据库的函数来执行此操作,如果成功则返回 true,如果失败则返回 false。我在应用程序构造函数中执行该函数,因此每当应用程序关闭和重新启动时它都能正常运行。

保存设置后,我在模态表单中尝试了以下操作,但对象 myManager 出现 NullReference 异常。

这是获取新设置并保存它们然后尝试调用父表单 CheckDatabaseIsSetup() 公共函数来测试数据库连接的函数。

/// <summary>
    /// Save the settings and then hide the Settings window
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_Save_Click(object sender, EventArgs e)
    {            
        // TRUE: User indicates that we are to connect using a trusted connection
        // FALSE: User wants to use Integrated security to connect.
        if (rb_UseTrustedConnection.Checked)
        {

            AppSettings.DatabaseName = tb_Trusted_DbName.Text;
            AppSettings.Server = tb_Trusted_Server.Text;
            AppSettings.UseIntergratedSecurity = false;
        }
        else
        {
            AppSettings.DatabaseName = tb_Secure_DbName.Text;
            AppSettings.Server = tb_Secure_Server.Text;
            AppSettings.Username = tb_Secure_Username.Text;
            AppSettings.Password = tb_Secure_Password.Text;
            AppSettings.UseIntergratedSecurity = true;
        }

        try
        {             
            AppSettings.SaveSettings();
            BushBreaksLodgeManagerMain myManager = (BushBreaksLodgeManagerMain)this.ParentForm;
            myManager.CheckDatabaseIsSetup();
        }
        catch (Exception ex)
        {
            log.LogAppendWithException(ex);
        }

        this.Hide();
    }

【问题讨论】:

  • 为什么不将 CheckDatabaseIsSetup() 中的代码放入某种设置管理器类中,然后您可以从任何您想要的地方调用它。表单中的代码应该与 UI 相关。
  • Ben,抱歉,我应该提到主窗体的函数 CheckDatabaseIsSetup() 调用测试连接的 databaseutilities 类的 TestDatabaseConnection。所以它在一个单独的对象中。我在主函数中调用的原因是如果连接失败,允许主窗体打开数据库设置窗体。

标签: c# .net winforms events modal-dialog


【解决方案1】:

您应该在模态表单中使用 Owner 属性而不是 ParentForm 属性,如下所示:

BushBreaksLodgeManagerMain myManager = (BushBreaksLodgeManagerMain)this.Owner;

Owner 属性定义了拥有(模态)表单和父(所有者)表单之间的实际关系。

【讨论】:

    【解决方案2】:

    最好在子窗体中定义一个事件并在主窗体中处理这个事件 当你在子窗体中提出这个事件时,mainform 可以做自己的工作

    【讨论】:

      【解决方案3】:
      BushBreaksLodgeManagerMain myManager = (BushBreaksLodgeManagerMain)this.ParentForm;
      

      您可以检查上面的行是否ParentForm 是类型/可以转换为BushBreaksLodgeManagerMain。我认为此案不成功,因此返回null

      【讨论】:

        【解决方案4】:

        我通常会通过使用对象相互通信来做到这一点,例如我的Emesary 库提供的;设计是以这样一种方式使用通知,即请求由知道需要处理这些通知的任何人发送和处理,例如,很容易添加断开连接的额外事件处理程序。

        在这种情况下,检查数据库设置的代码将变为:

        if (ReceiptStatus.OK == 
            GlobalNotifier.NotifyAll(new CheckDatabaseIsSetupNotification(tb_Secure_DbName.Text,
                                     tb_Secure_Server.Text,
                                     tb_Secure_Username.Text,
                                     tb_Secure_Password.Text,
                                     true))
        {
             // do something.
        }
        

        要完成这项工作,您需要在 BushBreaksLodgeManagerMain 和构造函数调用中实现 IReceiver

            GlobalTransmitter.Register(this);
        

        然后实现接口receive:

        public ReceiptStatus Receive(INotification _message)
        {
            if (_message is CheckDatabaseIsSetupNotification)
            {
                 var message = _message as CheckDatabaseIsSetupNotification;
                 if (connect_to(message.DatabaseName, message.Server, Message.Username, message.Password, message.UseIntergratedSecurity))
                    return ReceiptStatus.OK;
                 else
                    return ReceiptStatus.Fail;
            }
            return ReceiptStatus.NotProcessed;
        }
        

        您可以使用 Windows 事件执行此操作 - 但这种方式更清晰,并且允许与不一定有窗口的对象进行交互操作。

        【讨论】:

          猜你喜欢
          • 2012-03-13
          • 1970-01-01
          • 2020-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多