我假设身份验证窗口与 ShowDialog() 一起使用,如下所示:
AuthenticationWindow auth = new AuthenticationWindow();
if (auth.ShowDialog(this) == DialogResult.Ok)
{
// we know it was successful
}
然后在AuthenticationWindow
你取得成功后,你会打电话给:
DialogResult = DialogResult.Ok;
Close();
获得上面的反馈,或者表示它失败了
DialogResult = DialogResult.Cancel;
Close();
或者,您可以在 AuthenticationWindow 上设置一个属性:
class AuthenticationWindow : Form
{
public bool Success { get; set;}
}
并在 AuthenticationWindow 代码中适当地设置 Success 的值。
最后,如果您希望立即将反馈发送到您的其他窗口,请考虑实施一个事件:
class AuthenticationWindow : Form
{
public event Action<bool> SignalOutcome;
private OnSignalOutcome(bool result)
{
Action<bool> handler = SignalOutCome;
if (handler != null) handler(result);
}
}
然后,您将必须订阅调用身份验证窗口的该事件:
AuthenticationWindow auth = new AuthenticationWindow();
auth.SignalOutcome += (outcome) => { /* do something with outcome here */ };
auth.ShowDialog(this);