C# 通知模态表格';家长认为它需要采取行动
我有一个父窗体打开一个模式窗体,它基本上允许用户更改应用程序的数据库设置 当用户单击模式(子)窗体上的保存按钮时,它将使用新设置保存设置对象,但我需要让主窗体检查数据库设置是否正确 我目前通过一个函数来实现这一点,该函数尝试简单地连接到数据库,如果成功,则返回true,如果失败,则返回false。我在应用程序构造函数中执行的函数,因此每当应用程序关闭并重新启动时,它都可以正常运行 在保存设置后,我在模式窗体中尝试了以下操作,但对象myManager出现了NullReference异常 此函数用于获取新设置并保存它们,然后尝试调用父窗体CheckDatabaseIsSetup()公共函数来测试db连接C# 通知模态表格';家长认为它需要采取行动,c#,.net,winforms,events,modal-dialog,C#,.net,Winforms,Events,Modal Dialog,我有一个父窗体打开一个模式窗体,它基本上允许用户更改应用程序的数据库设置 当用户单击模式(子)窗体上的保存按钮时,它将使用新设置保存设置对象,但我需要让主窗体检查数据库设置是否正确 我目前通过一个函数来实现这一点,该函数尝试简单地连接到数据库,如果成功,则返回true,如果失败,则返回false。我在应用程序构造函数中执行的函数,因此每当应用程序关闭并重新启动时,它都可以正常运行 在保存设置后,我在模式窗体中尝试了以下操作,但对象myManager出现了NullReference异常 此函数用于
/// <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();
}
//
///保存设置,然后隐藏“设置”窗口
///
///
///
私有无效btn\u保存\u单击(对象发送者,事件参数e)
{
//TRUE:用户指示我们使用可信连接进行连接
//错误:用户希望使用集成安全性进行连接。
if(rb_usedtrustedconnection.Checked)
{
AppSettings.DatabaseName=tb\u Trusted\u DbName.Text;
AppSettings.Server=tb\u Trusted\u Server.Text;
AppSettings.UseIntegratedSecurity=false;
}
其他的
{
AppSettings.DatabaseName=tb\u Secure\u DbName.Text;
AppSettings.Server=tb\u Secure\u Server.Text;
AppSettings.Username=tb\u Secure\u Username.Text;
AppSettings.Password=tb\u Secure\u Password.Text;
AppSettings.UseIntegratedSecurity=true;
}
尝试
{
AppSettings.SaveSettings();
BushBreaksLodgeManagerMain myManager=(BushBreaksLodgeManagerMain)this.ParentForm;
myManager.CheckDatabaseIsSetup();
}
捕获(例外情况除外)
{
log.LogAppendWithException(ex);
}
this.Hide();
}
最好在子窗体中定义事件,并在主窗体中处理此事件
当您在子窗体中引发此事件时,mainform可以完成自己的工作
BushBreaksLodgeManagerMain myManager = (BushBreaksLodgeManagerMain)this.ParentForm;
您可以检查上一行
ParentForm
是否属于类型/can cast toBushBreaksLodgeManagerMain
。我假设这个案例没有成功,因此返回null
我通常会使用我的库提供的对象交互来实现这一点;设计是以这样一种方式使用通知,即任何知道需要处理这些通知的人都可以发送和处理请求,因此,例如,可以很容易地添加断开连接的额外事件处理程序
在这种情况下,用于检查数据库设置的代码将变为:
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);
然后实现接口接收:
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事件执行此操作,但这种方式更清晰,并且允许与不一定有Windows的对象进行交互操作。您应该在模式表单中使用Owner属性,而不是ParentForm属性,如下所示:
BushBreaksLodgeManagerMain myManager = (BushBreaksLodgeManagerMain)this.Owner;
Owner属性定义所属(模式)表单和父(所有者)表单之间的实际关系。为什么不将CheckDatabaseIsSetup()中的代码放入某种设置管理器类中,然后您可以在任何地方调用它呢。表单中的代码应该与UI相关。Ben,很抱歉,我应该提到主表单的函数CheckDatabaseIsSetup()调用databaseutilities类的TestDatabaseConnection来测试连接。所以它是在一个单独的对象中。我在main函数中调用的原因是,如果连接失败,允许主窗体打开数据库设置窗体。