About form closing at runtime in C# - c#

I have two forms named frmRegistration & frmMain in my project in c#.
I have set frmRegistration as my start form.
User enters data in the frmRegistration form & presses submit button to get registered. Then, I want to close frmRegistration form & show frmMain form to the user.
I'm trying this by using Dispose() method of the frmRegistration. But, when I use this method, it disposes all my application execution because frmRegistration is the startup form.
I don't want this to happen. Can anyone solve this problem?
thanks.

Use Show() and Hide() methods.
private void btnSubmit_Click(object sender, EventArgs e)
{
...
var frm = new frmMain();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.Show();
this.Hide();
}
UPDATE:
If you don't want to have frmRegistration in memory, start your program in main form and add this in your MainForm's Shown event:
var frm = new frmRegistration();
frm.Location = this.Location;
frm.StartPosition = FormStartPosition.Manual;
frm.FormClosing += delegate { this.Show(); };
frm.Show();
this.Hide();
Now you can just close the registration form and automatically get back to main form.

Try setting frmMain as start up form and hiding it initialy, show frmRegistration, do what you have to do, and Dispose it.

You can also change you Program.cs main class with the Main() function to start frmRegistration and after positive DialogResult or another check it will then start with frmMain - as your main form and message loop.

At least there are two options:
1.Turn your Start up form into a singleton
When you need to hide it, call it's hide method
2.Have new different startup form, call it MainApp form or whatever, have it set to invisible, the you can do what ever you like with the other non-startup forms.

Related

Focus winform after application.run

I have 2 winforms in an application in visual studio. The first one is kind of a loading screen, making sure there are no connection problems, then a second winforms opens, which would be a login form. I use this method to close the first form and open the second one:
this.Close();
th = new Thread(opennewform); // [opennewform thread: Application.Run(new Login());]
th.SetApartmentState(ApartmentState.STA);
th.Start();
and it works fine, but when the second form opens it loses focus. I've tried adding this.Activate, this.BringtoFront, this.Show to the second form, but it doesn't work. And what I need is after the first form closes and the second one opens, this second form is the "active" form, so it always pops up. I also needed to use the close/thread method so that the first form really closes and not just this.hide(); but actually stays in the background. Thanks in advance for the help.
You do not need to create a new thread. Instead, change the start code to something like this
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = new Form1();
form1.Show();
Application.Run();
}
I.e., remove the first form from Application.Run(), so that the application does not exit when this form closes.
Then open the second form with
Close();
var form2 = new Form2();
form2.Show();
Now, the application will not exit automatically when you close any form. Therefore, you must exit it explicitly with Application.Exit();, e.g. in the FormClosed event of the second form.
try:
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Close();

How to close form and reopen it?

I am allowing user to change some settings (language - UI culture) and to show current form in new language, I need to close form and reopen it.
I am using Windows Forms. In Program.cs first I show Splash screen and then Login form and after this I run Main form with
Application.Run(new frmMain());.
I tried several solutions offered which includes:
Controls.Clear();
InitializeComponent();
also I tried
var form = new frmMain();
form.Show();
this.Hide();
and also
this.Close();// note I tried before and after.
var form = new frmMain();
form.Show();
I also tried in form closing event
Application.Run(new frmMain());
and one solution with opening new thread.
None of this worked, or it worked partially, had some issues like not closing program well, or some other.
Is there some new simple way to do this that works?
or I better show user dialog box message "Do you want to restart program to show new language?"
Thanks
To restart your application you can rely on Application.Restart() method. It shuts down the application and starts a new instance immediately:
Application.Restart();
You can create a new global form here:
public partial class Form1 : Form
{
frmMain form = new frmMain(); // add your form here
public Form1()
{
//something...
}
//some another functions ...
private void btnOpenYourForm_Click(object sender, EventArgs e)
{
form = new frmMain(); // open your form here
form.Show();
}
}
I hope it works for you.

Can't hide or close form after switching to another form

I'm trying to switch Windows forms after successful login at the login form, I move to the main form. The problem is once the user logs in by clicking the login button, the main form opens but the login forms stays in the background, won't go away. I tried this.hide() and this.close, but they don't work. Here's some of the code from the btnLogin event...
//connection to access database and checking if the user exist, ...
...
if (!rdr.Read())
{
MessageBox.Show("Wrong username or password.");
}
else
GlobalClass.GlobalVar = true;
GlobalClass.GlobalStr = rdr["user"].ToString();
MainScreen _main = new MainScreen();
_main.ShowDialog();
rdr.Close();
conn.Close();
this.Close();
}
you are using ShowDialog use Show method instead, here:
_main.Show();
When you use ShowDialog your program waits until you close your MainForm and doesn't go to the next line.So your second form doesn't close until you close the MainForm.
It's probably better to create and display the main form from Program.cs, and then show and dispose the LoginDialog from the OnLoad handler of the main form.
In particular if LoginDialog is created from Application.Run then closing it will exit the app, so not very useful that way around.
You can hide() then close() but you have to use an event in doing so and like others suggest I will use Show() method instead of ShowDialog().
I would probably declare globally the Main form first.
MainScreen _main;
Then when you are about to show the Main after login use event to Hide() the login Form and then an event to Close() login form once Main form is closed.
GlobalClass.GlobalVar = true;
GlobalClass.GlobalStr = rdr["user"].ToString();
_main = new MainScreen();
_main.Load += new EventHanlder(_main_Load);
_main.FormClosed += new FormClosedEventHandler(_main_FormClosed);
_main.Show();
Catch the event for Main Form loading to Hide the Login form, like:
private void _main_Load(object sender, EventArgs e)
{
this.Hide(); // Hides Login but it is till in Memory
}
Catch the event for Main Form closing to Close the Login form, like:
private void _main_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close(); // Closes Login and remove this time from Memory
}

Better way to Display forms one on top of another

I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}

change active form to show another form

I have a Form1 and another one that I added. Form1 is being run by program.cs at the start. I need to hide Form1 and show options form by the press of a button.
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
}
the above code opens the options form on top, but I need it to replace the current form. how can I achieve this?
Hide current form using this.Close() before showing new one and make sure you are using parameterless Application.Run so program won't close when you close it's main form.
You can use "Hide" and "Show" method :
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
this.Hide();
optionForm.Show();
}
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
this.Hide();
}
Similar solutions where one form calls and acts on another... Such as this one I answered for another. You could do a similar process... pass in your first form to the second... Then show the second... Then, you could HIDE your first form (via this.Hide() ). Then, in your second form, when you click whatever button to select your choice, and need to return back to the first form, you could then use the original form's reference passed INTO the second form to re-Show it, such as in the click on the second form...
this.PreservedForm.Show(); // re-show original form
this.Close(); // and CLOSE this second form...

Categories

Resources