I have a main MDI form in my application and a login form. I'm opening my application like this. I want to open the login as the active form in which user should not be able to click any control of the MDI parent form while my login form is open.
I open my login form like this in the MDI form.
Form newLogin = new FormControllers.FrmLogin();
newLogin.StartPosition = FormStartPosition.CenterScreen;
newLogin.Show(this);
newLogin.Focus();
newLogin.TopMost = true;
newLogin.Activate();
Then this is the code I have used in my login form:
public void activateParent()
{
if (this.Owner != null)
{
this.Owner.Enabled = true;
}
}
private void FrmLogin_Activated(object sender, EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Enabled = false;
}
}
private void FrmLogin_Deactivate(object sender, EventArgs e)
{
activateParent();
}
private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
activateParent();
}
Still when I run the program login form opens, but not as active form. Just after I run my program my MDI form controllers can be accessed.
My MDI main disables only when I click on login form.I want to overcome this.
I want to open my MDI form disabled and login form activated.
How to achieve this?
use ShowDialog() instead of Show().
E.g.
newLogin.ShowDialog()
And then you should add a check on login form if it is closed without authenticating a user it should close the whole application, or alternatively, you can hide title bar.
Related
I am creating a C# application . I am new to .NET programming. It's basically a big Windows Forms Application which displays various forms, all interlinked with each other based on user control. My main form is login page to validate the user to go to the Menu Form(second form), where there are options for the user to decide. So the main activity starts from the Menu Form(second form), it contains a label holding the username. From the Menu Form(second form), it goes to the third form which is a pop-up form, which leads to the fourth form. Basically, hide the second form when moving to each form. Now from the fourth form, I want to go back to Menu Form(second form) without creating a new instance. I tried to do this without new instances but no luck. See code below:
Second form (Menu):
private void button_Click(object sender, EventArgs e)
{
PopUp form3 = new Popup();
form3.Show();
// Hides the Menu Form(second form)
this.Hide();
}
Third Form:
private void button_Click(object sender, EventArgs e)
{
var menu = new Menu();
menu.Hide();
// Hide Form #3
Hide();
form4.Show();
// Hide Form #3
Close();
}
Fourth Form:
private void button_Click(object sender, EventArgs e)
{
if(grpSaved == false)
{
Form5 form5 = new Form5();
form5.Show();
form5.FormClosed += new FormClosedEventHandler(unsaved_FormClosed);
grpSaved = true;
}
else
{
var menu = new Menu();
Close();//closes fourth form
menu.Show();
}
}
This code creates a new instance of the Menu Form(second form). Please help me to get around this problem.
While on the second form (Your main form) if you select a button or click a check box to access the third and fourth forms, try using a show dialog cod instead for the new form and then place you this.Show(); after the show dialog to ensure that when the form you opened is closed it will return you to the form you opened it from.
Example:
Instead of:
PopUp form3 = new Popup();
form3.Show();
//hides the second (Menu) form
this.Hide();
Try:
//Hide the second form
this.Hide();
//Bring up your PopUp form
using (PopUp form3 = new PopUp())
form3.ShowDialog();
//When your PopUp form closes the code should continue and show the second form again.
this.Show();
So to stack the forms try the following:
//Menu Form
private void button_Click(object sender, EventArgs e)
{
this.Hide();
using(PopUp form3 = new Popup())
form3.ShowDialog();
this.Show();
}
//PopUp Form
private void button_Click(object sender, EventArgs e)
{
this.Hide();
using(Task form4 = new Task())
form4.ShowDialog();
this.Close();
}
//Task Form
private void button_Click(object sender, EventArgs e)
{
this.Close();
}
The last button only needs to close the task form. Once it closes the code on the PopUp form will continue which will close the PopUp form and return you to your original Menu form without loading a new form, and clearing the form stack in the process.
It's a clean solution except that it does not inherently save the data from the other two forms. Clicking on the button on the Menu page again will bring up a new PopUp form.
I want to close two forms at the same time. I have one main form that starts with program. when user click button, the main form will hide and other form will pop up. on the second form if user click "back to main " button, it should hide second form and show main form. But the problem is if user tries to close the second form it should close the main form as well. How can i close the main form as well
I would just use the Application.Exit() for what is requested by this thread.
Application.Exit();
UPDATE: corrected
I had said this will not call the form closing events but in documentation it does actually call it here is a link to the documentation
http://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx
It was better if you specified what codes you wrote for going back to main form, so I could help you by changing your codes. But now because I don't know how you did it, I have to write codes for both of those tasks.
It can be possible using a Boolean variable to do what you want. Follow bellow codes:
public partial class MainForm : Form
{
//"Click" event of the button that should opens the second form:
private void goToSecondForm_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //Or you can write it out of this method.
this.Hide(); //Hides the main form.
f2.ShowDialog(); // Shows the second form.
this.Show(); // Shows the main form again, after closing the second form using your own button.
}
}
public partial class Form2 : Form
{
bool selfClose = false; //False shows that user closed the second form by default button and true shows that user closed it by your own button.
//"Click" event of the button that should closes just the second form and returns user to the main form:
private void ownCloseButton_Click(object sender, EventArgs e)
{
selfClose = true; //Means user clicked on your own button.
this.Close(); //So the program closes the second form and runs f2_FormClosed method, but because selfClose became true here, happened nothing there and program will go back to goToSecondForm_Click method in the main form and will run this.Show() .
}
//"FormClosed" event of the second form :
//Whether user clicked on your own button or on the default one, this method will run.
private void f2_FormClosed(object sender, FormClosedEventArgs e)
{
if (!selfClose) //It means user didn't click on your own button and both of forms must be closed .
Application.Exit(); //So the program closes all of forms (actually closes the program) and couldn't access to any other commands (including this.Show() in goToSecondForm_Click method).
}
}
As others have said, you need to somehow call .Close() on the main form when your child form is closed. However, as you've pointed out, you don't have a reference to the main form automatically in your child form! That leaves you with a few options.
1. Exit the application immediately.
This is done by calling Application.Exit(); in your child form's "back to main" button's click event handler. It will immediately close all forms, which might simply be what you want.
// .. ChildForm code ..
void OnBackToMainClicked(object sender, EventArgs e)
{
Application.Exit();
}
2. Pass a reference to the main form to the child form.
This is probably the most common way to solve this problem in general. When you create your child form in your main form, you'll need to pass a reference as follows:
// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
var childForm = new ChildForm(this);
childForm.Show();
}
// .. ChildForm code ..
private MainForm mainForm; // This is where the child form will keep a reference to
// the main form that you can use later
public ChildForm(MainForm mainForm)
{
// This is the child form's constructor that we called above,
// and it's where we'll save the reference to the main form
this.mainForm = mainForm;
}
// This also needs to be the event handler for the close event
void OnBackToMainClicked(object sender, EventArgs e)
{
this.Close();
mainForm.Close();
}
3. Add an event handler on the child form's FormClosed event. This is a safe way to solve the problem if you are concerned about keeping your main application logic under the control of the main form. It's similar to the solution suggested by Lamloumi above, but it's all done in the main form's code.
// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
var childForm = new ChildForm(this);
childForm.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
childForm.Show();
}
void SecondForm_FormClosed(object sender, EventArgs e)
{
// Perform any final cleanup logic here.
this.Close();
}
Form1 _FirstForm = New Form1();
Form2 _SecondForm = New Form2();
MainForm _MainForm = new MainForm();
_FirstForm.Close();
_SecondForm.Close();
_MainForm.Show();
Normally , in the Home form you have some ting like this :
SecondForm second= new SecondForm ();
second.Show();
this.Hide();
In the SecondForm you must ovveride the event of closure like this :
public class SecondForm :Form{
public SecondForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
}
void SecondForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
To be sure that your application is closed after you close the form. because the Home from is still active and hidden if you don't.
use this in Form2
private void button1_Click(object sender, EventArgs e)
{
Form1.FromHandle(this.Handle);
}
There Handle are the same now ;
hope this work.
I want to show two forms one above the other. Sample form is below. I want a url should be loaded after authenticating for login. Otherwise the fornm behind should be disabled. After Login the form and its controls should be enabled.
I am using MDI for this. One is MDIParent and other as its child. There is a webbrowser control on a MDI Form . I want to show parent(web browser) and child(login) form together.
Code behind the login button:
private void btnLogin_Click(object sender, EventArgs e)
{
String lusername = txtLogin.Text;
String lPassword = txtPassword.Text;
if (lusername.Equals ("admin") && lPassword.Equals ("admin"))
{
_Authenticated = true;
this.Close();
frmKioskBrowserMain _mainform = new frmKioskBrowserMain();
_mainform.fnNavigate();
}
else
{
lblErrorMessage.Text = "Login Id or Password Mismatch??";
}
}
fnNavigate is the function on the Parent form
public void fnNavigate()
{
wbKioskBrowse.Navigate("www.google.com");
}
I searched for a solution but didnt find a suitable one.Also I would like to know if there any other option which I can use??
I've looked at all the suggested answers and nothing seems to fit what I'm looking for. I want to call a second form from my main form, hide my main form while the second form is active, and then unhide the main form when the second form closes. Basically I want to "toggle" between the two forms.
So far I have:
In my main form:
private void countClick(object sender, EventArgs e)
{
this.Hide();
subForm myNewForm = new subForm();
myNewForm.ShowDialog();
}
and in my second form I have:
private void totalClick(object sender, EventArgs e)
{
this.Close();
}
How do I get the main form to show?
ShowDialog opens your secondary Form as Modal Dialog, meaning that the MainForm's code execution will stop at that point and your secondary Form will have the focus. so all that you need to do is put a this.Show after your ShowDialog call.
From above link:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
private void countClick(object sender, EventArgs e)
{
this.Hide();
subForm myNewForm = new subForm();
myNewForm.ShowDialog();
this.Show();
}
Let's say in Form1 you click a Button to show Form2
Form2 frm2 = new Form2();
frm2.Activated += new EventHandler(frm2_Activated); // Handler when the form is activated
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed); // Hander when the form is closed
frm2.Show();
Now, this one is when the Form2 is shown or is Activated you hide the calling form, in this case the Form1
private void frm2_Activated(object sender, EventArgs e)
{
this.Hide(); // Hides Form1 but it is till in Memory
}
Then when Form2 is Closed it will Unhide Form1.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show(); // Unhide Form1
}
This is difficult to do correctly. The issue is that you must avoid having no window at all that can get the focus. The Windows window manager will be forced to find another window to give the focus to. That will be a window of another application. Your window will disappear behind it.
That's already the case in your existing code snippet, you are hiding your main window before showing the dialog. That usually turns out okay, except when the dialog is slow to create. It will definitely happen when the dialog is closed.
So what you need to do is hide your window after you display the dialog and show it again before the dialog closes. That requires tricks. They look like this:
private void countClick(object sender, EventArgs e)
{
this.BeginInvoke(new Action(() => this.Hide()));
using (var dlg = new subForm()) {
dlg.FormClosing += (s, fcea) => { if (!fcea.Cancel) this.Show(); };
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
The BeginInvoke() call is a trick to get code to run after the ShowDialog() method runs. Thus ensuring your window is hidden after the dialog window is shown. The FormClosing event of the dialog is used to get the window to be visible again just before the dialog closes.
You need to find some way to pass a reference to the main form to the second form click event handler.
You can do this either by setting the form as a member variable of the second form class or pass it via the event arguments.
If you are working in the same namespace, you have the context, using mainform or the name you gave the "main form", try:
mainform.show();
My C# Winform application encounters the situation where it cannot access a disposed object. The disposed object is a form (frmQuiz) that is opened from a button on the login form.
The Situation:
My application typically has two or three forms open at the same time. The Program.cs file runs form frmLoginBackground, which is just a semi-transparent background that covers the computer screen. The load event for this form opens the second form, frmLogin, which includes a button that opens frmQuiz, which is a simple form with a few math questions on it.
The code in frmLogin that opens frmQuiz looks like this:
private void btnTakeQuizNow_Click(object sender, EventArgs e)
{
frmQuiz quiz = new frmQuiz();
quiz.TakeQuizNow("take_quiz_now", Convert.ToInt32(comboQuizMeNow.SelectedValue)); //Pass the form a quiz id number.
quiz.Show();
}
When frmQuiz opens both it and frmLogin are open and accessible.
The frmLogin also contains a password control that opens the administration form by first opening frmSplash, which is a "Please Wait..." splash form based on a timer. The timer Tick event launches frmAdmin, which is the administration form. The code in frmLogin looks like this:
private void btnPasswordSubmit_Click(object sender, EventArgs e)
{
//Password verification code snipped.
frmSplash objSplash = new frmSplash();
objSplash.Show();
//this.Hide();
this.Close();
}
And the code in frmSplash looks like this:
private void timer1_Tick(object sender, EventArgs e)
{
frmAdmin objfrmAdmin = new frmAdmin ();
objfrmAdmin.Show();
this.Close();
}
When frmAdmin opens then frmLogin is no longer accessible; however, frmAdmin contains a 'Return to Login Screen' button with code like this:
private void btnReturnToLogin_Click(object sender, EventArgs e)
{
exitWarnings("return_to_login");
}
private void exitWarnings(string action)
{
//Warning message code snipped.
if (action == "return_to_login")
{
frmLogin objLogin = new frmLogin();
objLogin.Show();
}
}
The frmLoginBackground remains open until the application exits.
The Problem:
Everything works fine when frmLogin first opens and the button is clicked to open frmQuiz. The quiz form opens a runs fine. However, after logging into the administration form (which closes or hides the login form) and then clicking the 'Return to Login Screen' link, then, after frmLogin reappears, the object disposed exception occurs when clicking the button to open frmQuiz. Visual Studio highlights in yellow the "quiz.Show();" line of code. The exception occurs regardless of weather I use "this.Close();" or "this.Hide();" in the btnPasswordSubmit_Click event.
Can anyone suggest a solution that allows me to open frmQuiz after returning to frmLogin from frmAdmin.
Cheers, Frederick
Since you create a new instance for quizz just before the quizz.Show() it cannot be quizz itself that throws the exception.
Take a good look at the constructor and FormCreate event of frmQuiz. It looks like that is where the dead horse is being kicked.