I have two forms. I show my second form with this code:
Form2 f = new Form2();
f.ShowDialog();
I need to understand when focus returns to main form. I tried Activate event but that's not it.
The call to ShowDialog() is blocking the parent form.
When your code exit from ShowDialog() your current form become active again,
For example:
using(Form2 f = new Form2())
{
// At the moment of the call of ShowDialog, the Net Framework start its work to
// pass the focus to the first control in the Form2 instance
if(DialogResult.OK == f.ShowDialog())
{
// Form confirmed, do your stuff
}
}
// At the end of the using block the parent form is again the active form with the focus on
// the button that has started this process (or the last control that had focus)
When you type
Form2 f = new Form2();
f.ShowDialog();
it waits until you manually close the Form2 instance and then returns to the main form. This happens if you use ShowDialog() method.
In some cases, where you just want to pop-up another form for a fraction of a second and return to the main form you should use formInstance.Show() method. The Show() method does not wait for the second form to be closed, it executes immediately and passes the control to the next statement after the Show() statement.
Hope you understood it.
Related
I am working a WinForm Application. I have a couple of forms on it. I want to be able to access my main form from any child form. I was able to do that through a custom button function and capture the Form_Closing event. I have one problem though which I'll explain below.
The code on the main form is as follow:
ChildForm form = new ChildForm(); // Create new Child Form instance
form.Show(); // Show Child form
this.Hide(); // Hide Main form
Using "this.Hide();" means that the main form still exists in memory and is still working, it's just hidden which is what I want.
The code on the child form
MainForm form = new MainForm(); // Create new Main Form instance
form.Show(); // Show Main Form
this.Close(); // Close Child Form
This is all well except on my second code block (Child Form directly above), the first line of code, creates a new instance of the main form. That is my problem, I don't want to create a new instance of that form, I want to show the already existing hidden instance (The main form I hid in the first block of code above).
I tried the following code on the Child form:
this.Parent.Show();
But I got this runtime error message:
"System.NullReferenceException was unhandled: Message=Object reference not set to an instance of an object".
I understand what the error means, I just don't the code to create an object reference to that main form or how to reference it in any sort.
Any tips please?
Thanks ahead.
You can make a constructor for your other forms that takes in a window as a parameter
private Form MyParent { get; set; }
public Form1(Form parent)
{
MyParent = parent;
}
MyParent.Show();
where MyParent is a property of the form
you can call this via new ChildForm(this)
Edit
I just looked, not sure why I can't use a constructor for an IWin32Window but Show has an overload that takes one in which will set the Owner to a parent form
new ChildForm().Show(this);
ChildForm.Owner //returns MainForm (parent)
That should do it. Because these are single thread forms, the function will wait till you close the form before proceeding further.
ChildForm form = new ChildForm(); // Create new Child Form instance
this.Hide(); // Hide Main form
form.ShowDialog(); // Show Child form, wait for closing
this.Show();
You can also attach ChildForm closing event to function in MainForm.
public MainForm()
{
ChildForm form = new ChildForm();
form.FormClosed += OnClosed;
}
public void OnClosed(object sender, EventArgs e)
{
this.Show();
}
How do I focus a TextBox element on the main form after closing a second form which is opened by calling ShowDialog()?
I have tried to call MainForm.TextBox.Focus() in the closing and closed event of the second form, but this won't focus the textbox.
I am using System.Windows.Forms on the compact framework.
Thanks.
From your second form, make a button (or another control) return a DialogResult by going into Properties. When you want the second form to close (ie after you press a button) make it return a specific DialogResult. In your main form you can do this:
if(secondform.ShowDialog() == DialogResult.OK)
{
textBox.Focus();
...
}
Calling ShowDialog() will block until it is closed so you could simply do:
secondform.ShowDialog();
textbox.Focus()
However the first example is for when you only want to make the textbox have focus after you press a certain button or do an action on the second form.
ShowDialog means, it's a modal window and the focus won't go back to main form until you close second form. You can set focus back in the same code which you used to open the second form.
SecondFrm.ShowDialog();
Textbox.Focus();
ShowDialog() will only return when the second form is closed, so you can write MyTextBox.Select() right after the call.
SomeForm form1 = new SomeForm();
form1.ShowDialog();
here you're showing the new form.
When you close it, you will execute methods after that, so add
yourTextbox.Focus();
so, its:
SomeForm form1 = new SomeForm();
form1.ShowDialog(); // do what you want in your form, then close it
yourTextbox.Focus();
I have an application that has 2 forms. First one is where I do all the job and second one is just for displaying a progressbar.
I want to open the second one from the main form. if I use
Form2 newForm = new Form2();
newForm.Show();
Form2 opens and closes when it needs to open and close, but I cannot see the progress bar. I just can see a blank instead of it.
When I use
Form2 newForm = new Form2();
newForm.ShowDialog();
I can see the progressbar but Form2 doesn't close when it needs. It runs forever, what should I do?
I use a static public variable closeForm to close the second form. When I need to close the form I set
closeForm = true;
and in the second form, I have a timer
private void timer1_Tick(object sender, EventArgs e)
{
if (Form1.closeForm)
{
this.Dispose();
this.Close();
return;
}
else
{
progVal++;
progressBar1.Value = (progVal % 100);
}
}
this is where I put the ProgressBar value and close the form.
When I use show method, I only see blanks instead of the controls in form2. not just the progressbar, and I want form1 to close form2
first of all you need to report progress to progressbar
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
progressBar1.ReportProgress(iProgressPercentage);
try doing that first then call this.close();
As I said above in the comment, you need to check Modal dialog from here Form.ShowDialog Method, and I just quote the following form there:
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.
As why you can't see your ProgressBar on Form2 with Show(); you need to provide more information of how you handles it, as if I separate your program into two parts and use two button click to run them (Click button1 to show Form2; and click button2 to close it) I can see your expected result: the progressbar.
Without your further information, my best guess is something running prevents the Form2 to update its GUI.
In my program I will press a button, and it will load form2. I do not want the program in form1 to continue running until the user closes form2. Then program execution continues in form1 right after the line that loads form2.
i think you want to show a modalled dialog.
public class MyForm1 : Form
{
public void ShowDialog2()
{
MyForm2 form2 = new MyForm2();
form2.ShowDialog(this);
}
}
You can use
Form.ShowDialog Method (IWin32Window)
You call the .ShowDialog function on the form you want to show.
.Show simply shows the new form. Calling .ShowDialog causes the calling form to block until execution returns from the shown form.
The result returned from the call to .ShowDialog will tell you if the user hit 'Okay' or 'No' or cancelled the form by clicking the x in the corner.
When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).