I have multiple form with the same design like:
In Form1{
Form2 = new Formtoopen();
Form2.Resize += new EventHandler(Form2_Resize);
Form2.FormClosing +=new FormClosingEventHandler(Form2_FormClosing);
}
and then the events:
In Form1{
protected virtual void Fly_Form2_Closing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Fly_Form2.Hide();
}
}
protected virtual void Fly_Form2_Visiblechanged(object sender, EventArgs e)
{
//some code
}
}
I would like to add the Form2 Type in the EventArgs that is empty from now.
I think it would make my code simpler as I have multiple Form sharing the same code.
How could I do that? I thought about the event custom arguments way but i'm not sure with Type...
Could you help me?
Thanks
Here is something that works:
In Form1{
Form2 = new Formtoopen();
Form3 = new Formdata();
Form2.FormClosing +=new FormClosingEventHandler(Form_FormClosing);
Form3.FormClosing += new FormClosingEventHandler(Form_FormClosing);
}
In Form1{
protected virtual void Form_Closing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
((Form)sender).Hide();
}
}
Retrieving the sender with cast Form from the object sender.
Thanks a lot M.Passant!
Related
I have 2 forms. Form1 and Form5. Form1 has 2 buttons which open Form5. I want to change the visibility of Form5's button after it is shown. I have set Form5 button's modifiers to public and I've also tried this code below but it's not working:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = true;
fr5.ShowDialog();
}
public void button2_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = false;
fr5.ShowDialog();
}
Edit: I've set button1.Visible = true; in Form5 Load event.
Thank you for your suggestions. :) I have solved the problem by changing the code like this:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = true;
}
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = false;
}
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
PongForm.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Can someone explain why I'm getting an error? I've had a look online and think it should work. I'm trying to change to a new form on button click.
In this function you should refer form, not PongForm:
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
Change "PongForm.Show();" to "form.Show().
To eloborate: you are attempting to call the class, not the instance you created.
to just add to what others have said. you probably don't want multiple of the same forms open. I cant comment or I would have done that instead. hope this solves your problem.
if (Application.OpenForms["PongForm"] != null)
{
Application.OpenForms["PongForm"].WindowState = FormWindowState.Normal;
Application.OpenForms["PongForm"].BringToFront();
}
else
{
PongForm form = new PongForm();
form.Show();
}
When I click a button a form is opened, but if the form is already open, then the app should display the message "Form already open!" and do nothing else.
My problem is, once I close the window [x] I can't open the form again.
Here's the code:
Form2 decript_form = new Form2();
private void button2_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
When the "Close" button is pressed, you want it to just "hide" the form...you need to use e.Cancel to stop it going on and closing.
If you really really want to close the Form2 window instead of hiding it while your application is running....then call ReallyClose....so that the close isn't prevent (then create a new decript_form or null it out).
(Alternatively decript_form.Dispose() would force real closure too)
public partial class Form2 : Form
{
private bool m_bReallyClose = false;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (!m_bReallyClose)
{
this.Visible = false;
e.Cancel = true;
}
}
public void ReallyClose()
{
m_bReallyClose = true;
this.Close();
}
}
public partial class Form1 : Form
{
Form2 decript_form = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
private void button2_Click(object sender, EventArgs e)
{
decript_form.Dispose(); // or .ReallyClose();
decript_form = new Form2();
}
}
I assume that you talk about the [x] on Form2 being pressed. It that's the case you should handle the Closing event in Form2() and add
this.Hide();
to the handler. Even a closed window is still 'shown' until it is hidden.
class Form2
{
override protected void OnClosing(CancelEventArgs e)
{
Hide();
}
}
I would like to make X control closing window to hide current display previous form.
In form1 I got:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Show(this);
Hide();
}
and then when I click X I would like to show previous and hide the current.
You should not override Form.OnFormClosing() for just this. The Form.FormClosing event provides this functionality for you:
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Prevent the user from closing this window, minimize instead.
if (e.CloseReason == CloseReason.UserClosing)
{
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
}
You can override OnFormClosing to do this.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// DO WHATEVER HERE
}
You have to keep track of your instanced forms.
// Program.cs
public static FormA Instance;
public static void Main()
{
Instance = new FormA();
Instance.Show();
}
Then:
// FormB.cs
private void button1_Click(object sender, EventArgs e)
{
Hide(); // Hide current...
Program.Instance.Show(); // Show previous...
}
I am trying to call one EventHandler method from another. For example, I would like to link Logout button with form exit, so I have this code:
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
and I want to call it from this event:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, 'not sure what goes here');
}
Try this:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, null);
}
or
private void button1_Click(object sender, EventArgs e)
{
Form1_FormClosing(
null,
new FormClosingEventArgs(CloseReason.UserClosing, false));
}
Even if my answer cover how to link event handlers part, this particular solution leads you to a problem: form won't close clicking button.
Correct solution is
private void button1_Click(object sender, EventArgs e)
{
Close();
}
Handling the event and asking for confirmation are seperate things:
private static bool UserConfirmedToLogout()
{
return MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !UserConfirmedToLogout();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
Close();
}
When Close() is called, the FormClosing event is fired too.