Reputation: 155
I have two forms: Form1: Parent: Login Screen Form2: Child: Application
When the application starts Form1 is shown. User enters password, and if this password matches with a constant, Form1 is hidden and Form2 shows up. Now when I close Form2 using a "log off" button, I shall send a "message" to Form1, so it can show up, again. Think this as a messenger application. When you log off, it will send you back to log in screen.
I am searching for two days, but can't find anything that worked for me. Sorry if this question is a duplicate.
Upvotes: 0
Views: 1510
Reputation: 30454
Apparently LoginForm
is a form that creates and shows your ChildForm
. While ChildForm is shown, LoginForm is not visible, nor can it have the focus. As soon as ChildForm is closed, LoginForm should become visible again and have the focus.
This looks like standard Modal Dialog behaviour to me, except the invisiblity.
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;
}
}
}
Upvotes: 1
Reputation: 112259
In Form1
, you can subscribe to the FormClosed
event of the second form before opening it:
var form2 = new Form2();
form2.FormClosed += Form2_FormClosed;
form2.Show();
Then you can do anything useful in the event handler in Form1
:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form 2 closed");
}
Upvotes: 2
Reputation: 81
I suggest that you use the Main/Application Form as Parent and Show the Login Form as Child. Show the Login Form with ShowDialog() in the "Shown" Event of the Main/Parent Form (for exemple). Depending on how smart your Form classes are you might have to change some code below. The code sample below implies, that the Login form does all the login logic. It then signals its parent if the login was successful or not with the DialogResult property upon closing.
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
}
}
}
}
Upvotes: 1
Reputation: 1
Form1:
create static and public variable
static public string message = "Message from Form1";
Form2
use this variable
MessageBox.Show(Form1.Message, " Message");
Upvotes: 0