Closing Form C# - c#

Recently I had to edit my program code so that the form will close after creating a PDF. In FormClosing() there's a MessageBox.Show for closing or not, depending on the DialogResult. The problem is that when I try to Close(), it shows me the MessageBox, I need to close it without showing it. Thanks.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
// close pdf but skip MessageBox
}

You can stop listening to the event like so
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
this.FormClosing -= Form1_FormClosing
showPDf();
Close();
}

You can use the CloseReason property of the FormClosingEventArgs:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.ClosingReason == CloseReason.UserClosing && MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}

Use e.ClosingReason to find out if the formClosing event was fired by the user's attempt to close the form, or by something else.
for further reading, go to MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason(v=vs.110).aspx

You anyways want to close the form after pdf creation. So call Form's Dispose method just after pdf creation like below and no need of registering for the OnFormClosing event
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
this.Dispose();
}

Related

C# Is it possible to convert EventArgs to FormClosingEventArgs when calling method in a method?

I've made a form closing event when X is pressed, but I also want the 'Exit' button to call this same method yet it draws me error every time I change stuff.
--- This code below is the form closing event ---
// if user pressed 'Exit' button or red cross
private void TempConverterForm1_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult exitdialog = MessageBox.Show("Are you sure you want to quit?", "Quit?", MessageBoxButtons.YesNoCancel);
if (exitdialog == DialogResult.Yes) {
e.Cancel = false;
}
else {
e.Cancel = true;
}
}
--- This code below is the code I'm trying to solve ---
// if the 'exit' button is pressed
private void btn_Exit_Click(object sender, EventArgs e) {
TempConverterForm1_FormClosing(sender, (FormClosingEventArgs) e);
}
I've tried without FormClosingEventArgs first but on itself it says that EventArgs can't be converted to closing event. I put FormClosingEventArgs but now it tries to convert from MouseEventArgs to FormClosingEventArgs even though I relate to button click and not mouse click.
I tried to do research but the problem repeats and builds up with different error messages and I got lost and decided I need help with this.
Just do this.Close() in btn_Exit_Click. This will fire Form_Closing correctly with the right arguments, and your cancel will still work.
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}

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

Close button initiates main_load event

I have multiple forms in my WinForm application.
The first form (Form1) is used for authentication, while the second (Main) is used as main dashboard for the application that opens additional forms.
When I am in designer mode, I double click on the close button within the Main form to generate the closing event, but something weird occurs.
Instead of:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
The event that the click generates is:
private void Main_Load(object sender, EventArgs e)
I don't understand why.
I've tried to type the method manually as shown bellow:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
} else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
But its simply not linked to the close button.
Any ideas why?
You need to find and delete the event assignment for the Main form Close buttton's click event. That code is going to be in the Main.Designer.cs module. Look for all instances of this code:
this.FormClosing += ...
Delete that assignment (or those assignments, if more than one), then go back to the form designer, double-click the Main form's close button, and Visual Studio will create and take you to the method handler for the FormClosing event.
In the form constructor for Main() you can bind an event handler for the FormClosing event like so.
public Main()
{
// other startup tasks here
this.FormClosing += Main_FormClosing;
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
var dialogResult = MessageBox.Show("Do you really want to quit?", "Exit", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
e.Cancel = true;
return;
}
Application.Exit();
}

Disable closing of a certain WinForm

I'd like to know if there's a possible solution (I hope there is) to my problem. I have two forms, the Login Form and the Main Form. I'd like to know if there's a way to disable closing of the Main Form and only allow closing when I sign out (which redirects the user back to the Login Form) and only allow closing when Login Form is active. Sorry for my bad english.
I tried using the event below, yes it stops me from closing the main form but when I signed-out it did the same to my Login Form which I didn't want to happen. Is there any way to do this?
private void Form1_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
I suppose that you are using LoginForm like dialog (ShowDialog), Use DialogResult.Ok only when user logs successful
....
private voif logoutButton_click(object sender, EventArgs e)
{
_logged = false;
}
.....
private void loginButton_click(object sender, EventArgs e)
{
LoginForm _loginForm = new LoginForm();
if(_loginForm.ShowDialog() == DialogResult.Ok)
{
_logged = true;
}
}
......
private void Form1_Closing(object sender, CancelEventArgs e)
{
if(!_logged)
e.Cancel = true;
}

Interaction between forms in WinForms application, c#

guys!
I've got 2 forms in application - working form (frmMain) and form of settings (frmSettings).
There are two buttons on frmSettings - Save and Cancel. In frmMain I use the following approach to show the frmSettings:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
// ...
}
The problem is I don't know, how to detect, which button was pressed on the frmMain - Save or Cancel. The further logic of the program depends on this fact. I need something like this:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(/* frmSettings.SaveButton.WasClicked == true */)
{
InitializeServices();
}
// ...
}
Please, give me an advice, how to implement such kind of interaction between forms. Better without using global variables for saving buttons state.
Thanks beforehand.
ShowDialog returns a DialogResult object that allow you to know that. You have to:
On Save Button's click event, set this.DialogResult to DialogResult.OK
On Cancel Button's click event, set this.DialogResult to DialogResult.Cancel
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
if(frmSettings.ShowDialog() == DialogResult.OK)
{
InitializeServices();
}
//.......
}
Edited to manage the DialogResult as #tsiorn's answer: setting form's DialgoResult insted of setting that property on each button.
You chould use DialogResult to handle this. On your form settings window, you can set the result as so:
protected void btnSave_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.OK
this.close;
}
protected void btnCancel_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.close;
}
Then ...
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(frmSettings.DialogResult == DialogResult.OK)
{
// save
InitializeServices();
}
// ...
}
Start with an enumeration of the possible values:
public enum ExitMethod
{
Other, //this should be first, as a default value
Save,
Cancel,
Error
}
Then make a property on SettingsForm of that type:
public ExitMethod ExitMethod { get; private set; }
In SettingsForm's save/exit methods set that property to the appropriate enum value, and in the main form you can read that property value.
in the frmSettings window you handle the Click events on the buttons. Then set the dialog result:
void frmSettings_Save_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
void frmSettings_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
in the main form you do something like this to capture and evaluate the result:
DialogResult answer = frmSettings.ShowDialog();
if (answer == DialogResult.OK)
{
...
}
Additional information and usage can be found here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx

Categories