new to c# wandered how i can use a form to open a new form whilst also closing that initial form. I have a form that a user needs to fill out and click login on to move forward which at this point if the information is correct should open the next form and close the current one. All i seem to get is both forms closing. i can do it without closing the initial form but then it is obviously still open in the background. The form i'm trying to open is form encryption and the one 'im trying to close is form1 any help is much appreciated.
here's my code:
if (maskedTxtLogin != null)
{
FormEncryption Encryption = new FormEncryption();
Encryption.Show();
this.Close();
}
else
{
MessageBox.Show("Please input your initials");
}
Related
Forms.inDepthScootForm.passProductDetails(strProductName, strProductDescription);
Forms.inDepthScootForm.ShowDialog();
Forms.eScootForm.Hide();
I am trying to open inDepthScootForm and hide/close eScootForm. I'm trying to open this form as a ShowDialog(); the first time I open it but as Show(); for subsequent times I open this form.
If I follow this method, the system will crash the second time I open the form.
Any help would be much appreciated.
ShowDialog will only return to your function once it has been closed, so the hide function is being called after the show dialog is closed/finished.
First maybe create it as a new object, that way each instance of the form is unique (assuming you do not want to keep the data on it), then make sure it is closed and disposed once you are finished with it. This should fix the Error of the window already being open.
So first hide your first form, then display the one you wish to display. If you use a using here then if the showdialog throws an exception it will still dispose of it.
using(var win = new Forms.inDepthScootForm())) {
win.passProductDetails(strProductName, strProductDescription);
Forms.eScootForm.Hide();
win.ShowDialog();
} //win gets disposed as soon as the program leaves this
Forms.eScootForm.Show();
If this is not what you want, please be specific, ie: "When I show x window the second time I want it as a show, not a showdialog" but if you do, please make sure you show us the parent which is actually calling this window and is open while you do this.
Could you please edit your question to include the line and what window it is in that throws the error please?
I suppose you have the same instance of your 'eScootForm'. Maybe try to have a new instance when you show and dispose it when the dialog is closed as the example below :
public void ShowMyDialogBox()
{
var testDialog = new Form2();
// Show Form2 as a modal dialog
testDialog.ShowDialog(this);
// Do things when the dialog is closed
this.txtResult.Text = testDialog.TextBox1.Text;
// then dispose it
testDialog.Dispose();
}
Hello I'm making my first Windows Forms app in C# using Visual Studio and I have a little problem.
While I'm opening a new form and closing the previous one, when I run the app it looks like it's closing the previous form before it opens a new one.
It doesn't look good and I want to avoid it.
UserPanel MDIUserPanel = new UserPanel(Username);
MDIUserPanel.MdiParent = this.MdiParent;
MDIUserPanel.Show();
this.Close();
I don't know what is going wrong. I will be thankful for any help.
Wirth regards,
DarQScreaM
#Edit
This doesn't seem to be the case actually. Propably its that :
I have 3 forms MainForm, Login, LoggedUser.
MainForm is MDI container with FormBorderStyle set on Fixed Single
Login is child of MainForm with FormBorderStyle set on None
LoggedUser is child of MainForm with FormBorderStyle set on None
When application is runned Login form is created in MainForm. MainForm is never closed since its container.
But when i move from Login form to LoggedUser form and vice-versa its created with FormBorderStyle = Fixed Single (normal windows window) and after 0.5~second its changed into None.
Editing it into that didn't really help :
MDIUserPanel.FormBorderStyle = FormBorderStyle.None;
MDIUserPanel.Show();
#Edit2
This change fixed it for me. I don't know why setting it on Form properties didn't work properly. It looked like form was created as FormBorderStyle.FixedSingle and then it was changed into FormBorderStyle.None. If I made this manually in Load it worked but U had to fix the size of my window too. It doesn't seem to be good though. It should work from the beginning since Form properties in Designer are like that from the very beginning.
private void UserPanel_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(649, 357);
}
You can use the form's Shown event to ensure that the new form have been showed before you close the old one.
UserPanel MDIUserPanel = new UserPanel();
MDIUserPanel.Shown += ((s, ee) =>
{
this.Close();
});
MDIUserPanel.Show();
If this form you are trying to close, is the main form that opens when you run your application, then that's the reason. Closing the main form will exit your application. Try instead to just hide this form instead of closing it. And to make sure you can exit your application ('cause you've hidden your main form), just override the closing event of your current form, and put an "Application.Exit()" in it.
Hope this helps you !
First Step:
this.Hide();
Second Step:
MDIUserPanel.Show();
i have an administrator form and in this form lot of menu items and all open a new form. but when I open a new from again i will open a new from then new form will be open but old form will not be closed.there are many form open in administrator form. i wand when i open any form on click a menu item the other form close.
Simply you have to know each form object then you can close it, for example when you want to open one windows you write
new MyForm().ShowDialog();
Ok instead you can declare a form object in the top of your administrator form:
Form oldForm;
when user press one of your forms list:
if (oldForm != null)
oldForm.Close();
Form newOne = new UsersForm();
oldForm = newOne;
newOne.ShowDialog();
(C# / WindowsMobile 6)
Let's take an application with 3 STATIC forms: Form1, Form2, Form3, where Form1 opens Form2 by calling Form2.Show(), and Form2 does the same with Form3. Form2 and Form3 have a "Exit" button, that just hides the form (not "close", just hide).
So, we execute these steps:
open the application;
go to Form2, by clicking "Form2" button on Form1;
go to Form3, by clicking "Form3" button on Form2;
open File Explorer, and "re-open" application by clicking on it's file. Form3 appears;
hide Form3 by clicking on "Exit" button on Form3 ( this.Hide() ). That's the problem: file explorer appears instead Form2.
I don't want to call "callingform".Show() every time I hide a form. This "works", but file explorer screen appears after "this.Hide()" and before "callinform.Show()" and I need to "control" who's calling who.
How to solve this? Is there any way to bring all application's form to foreground in the same order they appeared?
Thanks in advance.
There really isn't a way. You could implement a way to store forms in a similar way to the first answer, but when you switch you need to do:
"callingform".BringToFront();
"callingform".Show();
That will put all of your forms in front of Explorer.
You might need to do some investigation into this, but off the top of my head you could try looking at the Application.Forms[] collection.
Maybe someone can confirm or deny this but I think usually, Application.OpenForms[0] will be the main/initial form with subsequent Form appearances being in Application.OpenForms[1], Application.OpenForms[2], etc...
So you could simply try navigating backwards through this Forms collections.
Something like (or a variation of),
public void BringLastOpenedFormToFront()
{
if(Application.OpenForms.Count > 0)
{
Form form = Application.OpenForms[Application.OpenForms.Count - 1];
BringToFront(form); // your bring to front method.
}
}
This would allow you to ensure the last Form that appeared was brought to the front and immediately visible to the user. Let me know if you need any clarification.
Link to MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx
I have a windows application I need to create a button that unload/close/restart the currunt form while its running and reload/reopen/restart the same form.
How ??
I tried the hide show thing but it keeps the old form running in the background
Application.Restart();
I found it ....
Application.Restart() will restart your entire application.
Hide() will only do what it says, hide the form. If you just want a fresh version of your form to reappear, you can just create a new one, Show() it, and Close() your current form.
public void Restart()
{
var window = new MyForm();
window.Show();
this.Close();
}
You'll have 2 forms open for a very short time, so if you have any data connections that need to be closed, do so before reopening the form. To the end-user, it will happen so fast that they won't know 2 forms were open.