Focus winform after application.run - c#

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();

Related

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.

New form disappear after creation

I'm facing a problem with form show. I have a main form where I have my GUI and I choose an option that creates an instance of a form. For example in my main form I have:
Form2 f2 = new Form2();
f2.Show();
The problem is that the form shows for about 1-2 secs and then goes behind the main form.
I tried some instructions in the main form below f2.Show() command like
f2.BringtoFront();
this.SendtoBack();
Also I added commands to the new form (Form2) load method:
this.BringtoFront();
this.Activate();
this.Focus();
Nothing of the above commands seems to be a solution for this. Only when I use f2.ShowDialog(); instruction in my main form but I don't want to do that because I need immediate access to both of these forms at the same time.
Any help? Thanks
If you don't want your second form to never go behind your main form then pass the owner in the overload of Show method that accepts the owner parameter
Form2 f2 = new Form2();
f2.Show(this); // Assuming this code runs inside the main form
If you remove or change it to comment this.SendtoBack(); :
f2.BringtoFront();
//this.SendtoBack();
it'll be OK!

How to open a closed windows form in c#.NET?

I would like to reopen a closed windows form in c#.
I actually actively close the form to allow autoIT scripts to run on a different windows application. However I would like to reopen the same form after the scripts are done.
So...
this.Close();
.
.
.
Application.Run(new FormTestPage());
However this gives me an error that says to use Form.ShowDialogue instead.
I'm unsure how that works.
Thanks in advance!!
If you close the main form and you started the application with
var frm = new MainForm();
Application.Run(frm);
Then closing the main form will also close the application. Start the application like this instead
var frm = new MainForm();
frm.Show();
Application.Run();
Now when the application has to close, you must call Exit (because it is not closed automatically anymore):
Application.Exit();
You can do this in the main form in the FormClosed event when you are not just closing the form temporarily.
Now you can reopen the form normaly with:
var frm = new MainForm();
frm.Show();
var item = new FormTestPage();
item.ShowDialog();

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();
}

About form closing at runtime in 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.

Categories

Resources