I have 2 forms. I'm opening form2 with ShowDialog(), but then when I close it (by hiding it) form 1 disappears for few seconds, but if I use show to open form 2 then this does not happen.
I need to use ShowDialog(), how could I fix disappearance of form 1 after form 2 closes ?
I tried to use Form1.Show() right after I close form 2 with Hide() but does not work.
Form1
private void p0_igra2_Click(object sender, EventArgs e)
{
this.CenterToScreen();
imevislice.ShowDialog();
}
Form2
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
errorProvider1.SetError(textBox1, "Polje more biti izpolnjeno");
else
{
errorProvider1.Clear();
ime = textBox1.Text;
if (radioButton1.Checked)
izbrane_besede = "SLO";
else
izbrane_besede = "ENG";
this.Hide();
form1.Show();
form1.namehangman();
}
}
If you open a form with ShowDialog you need to close (Form.Close) it when you are done. ShowDialog starts a modal dialog; which means the calling function will not continue execution until the form is closed.
Using Show can fix this, but you didn't say what wasn't working with that approach.
In general, ShowDialog is pretty cheap; you should be able to just close the form correctly and you won't run into any issues.
Related
So I want to show a form and close the other form so I did this in form1:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
//didn't use this.close() cause then the whole program will close instead of
//just the current form
}
Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 x = new Form1();
x.Show();
}
Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background.
what should I do to make the form get closed when I exit it?
I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.
When you write:
Form1 x = new Form1();
you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.
Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:
// ... all in Form1 ...
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 x = new Form2();
x.FormClosed += X_FormClosed;
x.Show();
}
private void X_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
So then when you close Form2 your instance of Form1 will automatically re-appear.
Here's my question.. From form 1, i want when the user clicked on update items, Form 2 will open but form 1 will still remain open. At this point, I want the Form 1 to be enabled false and I've done it. But I want when the form 2 closes, the form 1 enable should be true again. I cannot do this... Here's my code:
In Form 1:
private void btnEditItem_Click(object sender, EventArgs e){
Form3 form3 = new Form3();
form3.Show();
this.Enabled = false;
}
In Form 2(which is for update items, after updating):
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1Here.Enabled = true;
}
In this situation, it will open another Form1. The question is, how am i suppose to "ENABLE BACK AGAIN THE FORM 1 WITHOUT OPENING IT?" HELP ME PLEASE PROVIDE SOME SAMPLE IF POSSIBLE. THANKS
Attach a handler to your form.
Form3 form3 = new Form3();
form3.FormClosed += new FormClosedEventHandler(frm3_FormClosed);
form3.Show();
private void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}
Or you can use ShowDialog
Opens a window and returns only when the newly opened window is closed.
From your description it seems you want a modal Dialog Box.
From MSDN Modal and Modeless Dialog Boxes:
A modal dialog box must be closed (hidden or unloaded) before you can
continue working with the rest of the application
This means that if Form 1 opens Form 2 as a modal dialog box, form 1 will remain visible, but it is disabled, you can't do anything with it until you've closed form 2. In your words: "form 1 is disabled and when form 2 closes form 1 is enabled again".
The code:
private void btnEditItem_Click(object sender, EventArgs e)
{
using (Form3 form3 = new Form3()}
{
var dlgResult = form3.ShowDialog(); // show form 3 as a modal dialog box
// halt this procedure until form3 is closed
// handle the result of form3:
if (dlgResult == DialogResult.OK)
{
var x = form3.SomePropery;
ProcessDialogOutput(x);
}
}
}
The using statement is not necessary, but it is neat programming, because Form3 implements IDisposable
When I click on About, a new form opens and the main form hides. I have a close button on the About button, that closes the About form, and it shows up the Main form. However, when I close the main form with the 'X' close button on top, the Main form closes, but the process is still running in the background. Everything is okay when I dont click on the About button. It kills the process.
In your Form2, you need to bring up the existing Form1 instance rather than creating a new one (that's why closing this newly created Form1 won't exit the application, as your old Form1 instance is still hiding...).
So you could get reference passed when creating aboutdialog, and show that instead when closing aboutdialog:
Form1:
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2(this);
aboutdialog.Show();
this.Hide();
}
Form2:
Form1 _parentform;
public Form2(Form1 parent)
{
_parentform=parent;
InitializeComponent();
}
private void CloseButton_Click(object sender, EventArgs e)
{
_parentform.Show();
this.Close();
}
In your "AboutButton_Click" method you hide the Form1. But when closing the About dialog, you do not show back the Form1, but create a new one. The original Form1 stays hidden and prevents your application from closing.
better to use showdialog.
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2();
aboutdialog.ShowDialog();
}
When form2 is closed a new instance of form1 is opened, instead of the original one. You could include a reference to Form1 inside of Form2, or use the default Ownerof a form, by showing the dialog with Form1 as its owner:
aboutdialog.Show(this);
in the about...click
and closing
private void CloseButton_Click(object sender, EventArgs e)
{
Owner.Show();
this.Close();
}
I have a MDI form. Within this MDI there is multiple button to open new forms. Let buttons are btn1, btn2, btn3, btn4.... When I press btn1, form1 is load. when I press btn2, form2 is load... Now I press btn1, And form1 is loaded. If I press again btn1 then another form1 is open. Simultaneously let form1 is open, if I press btn2 form2 is open. But I want to open a form at a time. How I prevent this?
all the answers you got are good so i'm not going to repeat them, just give you an example of the member and method you can use to prevent that from happening:
private Form frm;
private void button1_Clicked(object sender, EventArgs e)
{
if (frm != null)
{
frm.Close();
frm.Dispose();
}
frm = new Form1();
frm.Show();
}
private void button2_Clicked(object sender, EventArgs e)
{
if (frm != null)
{
frm.Close();
frm.Dispose();
}
frm = new Form2();
frm.Show();
}
You can read up about mutual exclusion http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx
It is a general solution to make sure you only have 1 thing (thread, process, form, whatever) of something at the same time. You can even use it inter application.
An example is shown here: http://www.dotnetperls.com/mutex
You can create multiple mutexes, one for each form. Or one for a set of forms, in what ever combination suits you.
Example Scenario:
Form1 creates a mutex with name X
Form2 is being loaded checks whether mutex X is created, if so it closes itself.
Of course you will need to make sure the mutex is Disposed / released when the creator (Form1 in this example) closes, to allow other forms to show.
You can use some flag for this purpose OK, like this:
bool formOpened;
private void buttons_Click(object sender, EventArgs e){
if(!formOpened){
//Show your form
//..............
formOpened = true;
}
}
//This is the FormClosed event handler used for all your child forms
private void formsClosed(object sender, FormClosedEventArgs e){
formOpened = false;
}
At least this is a simple solution which works.
In general case, you need a int variable to count the opened forms, like this:
int openedForms = 0;
//suppose we allow maximum 3 forms opened at a time.
private void buttons_Click(object sender, EventArgs e){
if(openedForms < 3){
//Show your form
//..............
openedForms++;
}
}
private void formsClosed(object sender, FormClosedEventArgs e){
openedForms--;
}
Does this mean while you have Form1 open, you want to still be able to open a Form2 and 3 and etc?
It you don't want that, you can use the form1Instance.SHowDialog() instead of Show()...
But that generally means you can't access the parent form while form1 is open...
But King King's anwser might be more useable to you.
I'm working on project with sign in feature
When I run the project there is a form (form1) run the sign in .
after i click on login button build another form (form2) - It's the form of my program .
and made the first form (form1) hide .
The problem is when I press at the X button in form2 it's close but the form1 it's still running .
I tried to close the form1 instead of hide ... but this will close form2 before launching
In form1:
this.Hide();
Form2 x = new Form2();
x.Show();
I think you have your forms around the wrong way.
Form1 sould be your app and shold show Form2 as a dialog when it first loads, then when it closes you can process the result and decide wether to continue or close the application.
Something like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
Form2 myDialog = new Form2();
if (myDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
// failed login
// exit application
}
// all good, continue
}
}
You could subscribe to the child forms FormClosed event and use that to call Close on the parent form.
x.FormClosed += new FormClosedEventHandler(x_FormClosed);
void x_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
try this, in the log in button if access is granted
private void logInBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
this.Hide();
}
then in form2 if you want to exit
private void exitBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
hope this helps.
You go to your form2 then in the event of the form look for FormClosed.
Put this code in your eventhandler:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
FormClosed is the event whenever the user closes the form. So when you close the form put a code that will exit your application that is -applicationn.exit();-
Hope this will work.