【问题标题】:How to send a "message" to parent form from child form?如何从子表单向父表单发送“消息”?
【发布时间】:2019-01-19 18:46:46
【问题描述】:

我有两种形式: Form1:父级:登录屏幕 Form2:子:申请

当应用程序启动时显示 Form1。用户输入密码,如果这个密码与一个常数匹配,Form1 被隐藏,Form2 显示出来。 现在,当我使用“注销”按钮关闭 Form2 时,我将向 Form1 发送一条“消息”,这样它就可以再次出现。将其视为信使应用程序。当您注销时,它会将您送回登录屏幕。

我正在搜索两天,但找不到任何对我有用的东西。对不起,如果这个问题是重复的。

【问题讨论】:

  • 我通常做的是创建一个带有静态表单实例的类,并在它们之间切换
  • 您可以创建一个包含 2 个表单的项目。此外,在静态类中创建静态属性。使用此属性来存储 2 个表单之间的公共信息。此处解释了另一种方法:stackoverflow.com/questions/43219637/… 和此处:social.msdn.microsoft.com/Forums/en-US/…
  • 在对象语言中,从对象A发送到对象B的“消息”通常被实现为方法调用,A调用B的方法。唯一的问题是你是否要引入另一个两者之间的中介对象 C,或者更确切地说,它们之间存在直接依赖关系,没有任何额外的实体。

标签: c# .net winforms


【解决方案1】:

Form1,可以在打开第二个表单之前订阅FormClosed事件:

var form2 = new Form2();
form2.FormClosed += Form2_FormClosed;
form2.Show();

然后你可以在Form1的事件处理程序中做任何有用的事情:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("Form 2 closed");
}

【讨论】:

    【解决方案2】:

    我建议您将主/应用程序表单用作父项并将登录表单显示为子项。在主/父窗体的“显示”事件中使用 ShowDialog() 显示登录窗体(例如)。根据您的表单类的智能程度,您可能需要更改下面的一些代码。下面的代码示例暗示,登录表单执行所有登录逻辑。然后,它会在关闭时使用 DialogResult 属性向其父级发送登录是否成功的信号。

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
    
        private void ButtonLogoutClick(object sender, EventArgs e)
        {
            LoginUser();
        }
    
        private void MainFormShown(object sender, EventArgs e)
        {
            LoginUser();
        }
    
        private void LoginUser()
        {
            using (var loginForm = new LoginForm())
            {
                var loginResult = loginForm.ShowDialog();
                if (loginResult == DialogResult.OK)
                {
                    //Login Success
                    var userId = loginForm.User.ID; //Query user ID from Login Form for example
                }
                else
                {
                    //Login Failed
                    Close(); //Close Program for example
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      显然LoginForm 是一个创建并显示您的ChildForm 的表单。在显示 ChildForm 时,LoginForm 不可见,也无法获得焦点。 ChildForm 关闭后,LoginForm 应该再次可见并获得焦点。

      这在我看来就像标准的模态对话框行为,除了不可见性。

      class LoginForm : Form
      {
          private void ShowChild()
          {
              using (var childForm = new ChildForm(...))
              {
                  // if needed: set ChildForm properties
                  ...
      
                  // just before showing the childForm, hide this loginForm
                  this.Visible = false;
      
                  // show the childForm until it is closed
                  DialogResult dlgResult = childForm.ShowDialog(this);
      
                  // interpret the results of the childForm,
                  // for instance something like this:
                  switch (dlgResult)
                  {
                      // operator pressed OK; process the result and show the login
                      case DialogResult.OK:
                          string savedFileName = childForm.FileName;
                          this.Process(savedFileName);
                          break;
      
                      // operator indicates that program can close
                      case DialogResult.Cancel:
                          this.Close();
                          break;
      
                      // all other solutions: do nothing but show the login screen
                      default:
                           break;
                  }
      
                  // show the login screen
                  this.Visible = true;
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        表格1:

        创建静态和公共变量

        static public string message = "Message from Form1";
        

        Form2

        使用这个变量

        MessageBox.Show(Form1.Message, " Message");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-10
          • 1970-01-01
          • 2014-10-08
          • 2014-04-30
          • 1970-01-01
          • 2015-02-11
          • 1970-01-01
          • 2015-08-18
          相关资源
          最近更新 更多