Maximizing the main form when you close one - c#

I have this project for school which requires me to use 2 forms, but I can't have both of them open at the same time.
This means when I open one form, the other one minimizes. When you close this one, I'd like to make it so that the minimized one is restored.
This is what I have now for minimizing it:
private void Mkbtn1_Click(object sender, EventArgs e)
{
var newForm = new form1();
newForm.Show();
this.WindowState = FormWindowState.Minimized;
}

In form1, which is opening the form2 do things like this
`private void button1_Click(object sender, EventArgs e)
{
Form2 newofrm = new Form2();
newofrm.parentForm = this;
newofrm.Show();
this.Hide();
}`
And then in form2 use this
`private void Form2_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized && parentForm != null)
{
// Do some stuff
parentForm.Show();
}
}`
Here parentForm is public member of form2 of type Form, also if you want this do be done when form is closed then add form closing event and add same code in the event handler

Related

How to stop windows form from closing but hide upon clicking the X?

I'm trying to have a click event that will open another form. I don't want the user to be able to close this window because I get the following exception when the click event is executed again.
System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: 'Form2'.'
I'm not sure if I'm implementing this correctly or there's a better way of doing this.
Form1
public Form2 f = new Form2();
private void Btnsearch_Click(object sender, EventArgs e)
{
f.Show();
}
Form2
private bool allowClose = false;
private void Btnclose_Click(object sender, EventArgs e)
{
allowClose = true;
this.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (!allowClose)
e.Cancel = true;
}
Subscribe to Form.OnClosing and set the Cancel property on the event args that are passed to the handler. This will tell the runtime to cancel the close event.
Since the event is getting canceled, you'll have to hide the form yourself (using Hide(), of course).
private void Form1_Closing(Object sender, CancelEventArgs e)
{
this.Hide();
e.Cancel = true;
}
The instance of form2 should be created within the event
private void Btnsearch_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
There are a couple of ways to approach this.
It's generally more efficient, in the FormClosing event, to hide the form and cancel the event, but this can require extra logic.
Unless you have some expensive code that needs to run when the form is created, this probably doesn't matter, and it'll be easier to simply allow the form to close normally.
Either way, all you particularly need to do, is throw some safeguards into the btnSearch handler, so that it can appropriately respond to the state of form f;
public Form2 f;
public void BtnSearch_Click(object sender, EventArgs e)
{
if (f == null || f.IsDisposed || f.Disposing) f = new Form2(...);
f.Show();
}

How to instantly call action in Form1 on closing Form2

So basically i have Form1
From Form1 I can open Form2 with this code:
private void btn_Komplexity_Click(object sender, EventArgs e)
{
Form2 kompleksaForma = new Form2();
kompleksaForma.ShowDialog();
}
When Form2 is opened there is something and at the end there is this.Close();
After this.Close(); (closing Form2) is it possible to call instant action on Form1?
If you're sticking to ShowDialog(), this function will block until the form is closed.
private void btn_Komplexity_Click(object sender, EventArgs e)
{
using (Form2 kompleksaForma = new Form2())
{
kompleksaForma.ShowDialog();
PutStuffHereAfterClose(); // (or outside the using block if it doesn't need
// to access properties of kompleksaForma)
}
}
If you're showing Form2 as a modal window using form2.ShowDialog() or form2.ShowDialog(this), then...
form2.ShowDialog(this);
if (form2.DialogResult == DialogResult.OK)
{
CallOtherStuffHere();
}
... as the ShowDialog() method will block execution until the closure of Form2, then continue.
I'm using DialogResult above to test for validity, but you could implement some other method, if you wish.
If you're showing Form2 as a non modal window, then you should pass a reference of Form1 to Form2 first. This could be done in its constructor...
var form2 = new Form2(form1);
Or, you can pass it in the Show() method, to set form1 as its parent...
var form2 = new Form2();
form2.Show(form1);
Then, you can access the parent form via form2.Parent. However, you may have to cast it to a Form1 instance before you call your methods explicitly. And this can be done in the Closing event handler of Form2.
Further info here regarding modal and modeless windows:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
There are some events triggered after the window is closed. You can subscribe to them and add your code to the handler method:
Form2 kompleksaForma = new Form2();
kompleksaForma.FormClosing += KompleksaForma_FormClosing;
kompleksaForma.FormClosed += KompleksaForma_FormClosed;
kompleksaForma.Deactivate += KompleksaForma_Deactivate;
kompleksaForma.ShowDialog();
And then implement one of the handlers like that:
private void KompleksaForma_FormClosing(object sender, FormClosingEventArgs e)
{
// Your code here
}
private void KompleksaForma_FormClosed(object sender, FormClosedEventArgs e)
{
// or here
}
private void KompleksaForma_Deactivate(object sender, EventArgs e)
{
// or here
}
First will trigger FormClosing, then FormClosed. Last one is Deactivate.

Hiding Form 1 and Show form 2 on Windows Form 1 Load

Am a newbie when it come to C# Programming. Here's my trouble:
I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.
Here's my current codes;
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Hide();
}
My en-counted Error with the current Code:
When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.
Can anyone kindly help me with this error.
The Error i thought is that you are showing the form2 before hiding the previous form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
form1.Visible=false;
Form2.Show();
}
You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.
modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx
Here's how I manage to make this work.
New Codes:
Form 1
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Properties.Settings.Default.Status == "Form2Visible")
{
this.Hide();
}
}
Explanation:
On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.
If their is a simplest way let me know.
You can use Visible property to hide your form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Visible=false;
}

Prevent form from showing multiple times

In windows form (c#), i am showing a form when user click on button, it is working fine form is visible to user, but if user click again on the same button the same form is opening again two forms are displaying. Is there any way to prevent this, please give me any reference for this thank you. This is my code....
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2();
obj.Show();
}
You are most likely doing something like this:
void button1_OnClick(object sender, EventArgs e) {
var newForm = new MyForm();
newForm.Show();
}
So you are showing a new instance of the form every time it is clicked. You want to do something like this:
MyForm _form = new MyForm();
void button1_OnClick(object sender, EventArgs e) {
_form.Show();
}
Here you have just one instance of the form you wish to show, and just Show() it.
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
i tried more than a ways to compare which one is better.
but i think this solution must be better than the answer.
You can try something like
private Form f;
private void button2_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form();
f.Closed += f_Closed;
f.Show();
}
}
void f_Closed(object sender, EventArgs e)
{
f = null;
}
You are most probably creating a new instance of the form every time in the Click handler of the Button.
So you ill need to move the Form object creation outside the Button_Click.
Here's a good example of a proven solution
This will open the form if it is not already open.
If it is already open, it will place it in the foreground.
namespace MainProgram
{
public partial class Form1 : Form
{
private Form formNew = new FormToShowSomething();
private void button1_Click(object sender, EventArgs e)
{
formNew.Show();
formNew.Activate();
}
}
}
the easiest solution to your problem is replacing the Show command with ShowDialog, that way you won't any problem when it comes to preventing a form to show up twice
Form2 obj = new Form2();
obj.ShowDialog();
the code: .ShowDialog(); is what we are currently looking for that will solve the issue
10 years after , like the band :p
Thought to share the code that works for me. Nothing fancy, just checking if the form instance exists. Also, I don't prefer the ShowDialog, because the user is 'trapped' in that form and I find it annoying. The user might want to check other info from another source, for example when filling an online form and needs to copy paste a field info.
private void button1_Click(object sender, EventArgs e)
{
var obj = Application.OpenForms.OfType<Form2>().Select(t => t).FirstOrDefault();
if (obj != null)
{
obj.BringToFront();
}
else
{
obj = new Form2();
obj.Show();
}
}

Close All application click X Winform

I'm trying to change the names of the datagrid and labels in Form2 from Form1 based on the selection.
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.dataGridView1.Columns["FirstName"].HeaderText = "Prenom";
frm.dataGridView1.Columns["LastName"].HeaderText = "Nom";
this.Hide();
frm.Show();
}
The above approach is working fine for me but I have a problem. When I click on X in the second/Form 2 it's just closing the Form2 not the Form1. How can close all the application when i click on the X.
Is there any better way of doing this??? The reason why i'm not using I'm using Telerik and I don't find any option to add resource file in that. Please correct me if i'm wrong. Thank you.
You simply need to attach an event handler to the Closed event of the newly created form so that it closes the main form when it's closed:
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.FormClosed += (_, args) => this.Close(); //Added this method
frm.Show();
}
Add that same method to the other click handler as well.
The easiest way to close an application is to call the static method: Application.Exit
Another method if you only want to close one single form would be to add a handler for the FormClosing event and close the main form there.

Categories