Close event not working - c#

I want if user click the close button to close a WinForm, it will perform some work such as showing MessageBox message:
void frmMemberSearch_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("close message");
}
the about code does not work. Does anyone know what may be the problem?

just double click form closing event on Form Properties Tab.

Related

Open folder when a user clicks on a button of dialog box and prevent the dialog from closing

I have custom dialog box with a button and a link_label which i use as a button.
When the user clicks the label i want to open a certain folder at window explorer and keep the dialog open. I know which button the user clicks by determining what Dialog result to return.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.DialogResult = DialogResult.No;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
At another class i have a method which returns a bool value according to the dialogResult:
DialogResult res = dig.ShowDialog();
return res == DialogResult.No;
Then if this methods return true i need to open the win explorer and keep the dialog open until the button is clicked.
the problem is when im tring to prevent the dialog to closing with FormClosing event of MyDialog form, the explorer is not opened. And if i am not using FormClosing the explorer opens but the dialog is closed.
private void MyDialog_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.No)
{
e.Cancel = true;
}
}
EDIT: I cant use the Linkclicked event of the form because i want this dialog to be reusable and handle the result each time. (open different paths depends on where i use the dialog)
The reason why explorer is not opened is that when you reject the form close, the parent code doesnt continue to open explorer,
What you want to achieve is a bit unclear to me, but the way you do it is not possible, as a suggestion why not open the FolderBrowserDialog from inisde MyDialog based on label click and closing MyDialog on button click, rather than having the parent handle the decision.

Close about box

I'm trying do make a windows software. But when I added the About inside the Help button which is inside the menu stripe.
I created a new winform from the VS 2013 templates called "AboutBox", and it comes with a button OK. I want this button when clicked to close only this "About Box". I pasted down here only the button method, I didn't change anything at all from the code
private void okButton_Click(object sender, EventArgs e)
{
//close aboutBox;
}
Something like this:
this.DialogResult = DialogResult.Ok;
this.Close();
Or, as someone pointed out, just edit the resource in the IDE for the button and set the DialogResult property.:

System.InvalidOperationException when managing a dialog

I have encountered an error called System.InvalidOperationException.
Additional information: Specified element is already the logical child of another element. Disconnect it first.
I have searched through internet for the solutions but none of them solve my problem.
I created a button called "Open Dialog", to open a dialog. After I opened the dialog, there are two buttons showed in the dialog, "Save" and "Cancel". The function of "Cancel" is to close the dialog. However, after I clicked "Cancel", when I tried to reopen the dialog again by clicking the "Open Dialog", I have encountered the error stated above.
I will post my codes here as well for the event handling method.
In the class of MainWindow:
private void openDialogButton_Click(object sender, RoutedEventArgs e)
{
PersonIDTable.ShowDialog();
}
In the ViewModel:
public void ShowDialog()
{
PersonID.UserControls.PersonIDDialog dialog = new PersonID.UserControls.PersonIDDialog (this);
dialog.Show();
}
In the class of Dialog:
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
Do you guys have any idea to solve the problem? Your help will be much appreciated.
You pretty much told the answer in your question, you closed the dialog so you can't reopen it.
Insert the creation of PersonIDTable inside openDialogButton_Click, that should fix it.
Example:
Form1 form = new Form1();
form.ShowDialog();
Would have told you this through the comment, but I don't have enough reputation.

How to close C# process?

I wrote a simple program using Visual Studio(C#). When I close my program(click at Х) the form is closed, but the process remains. I had to close it from Task Manager.
What is the command to close process?
My code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
e.Cancel = true; means stop the form from closing. That's the main issue with your code.
You should use this parameter if you have a confirmation something like "Are you sure you want to close?" if the user selects "No" you set e.Cancel to true.
Just remove that code (or set Cancel to false) and your form will close, and if that's the last one of your application, it will end.
So the final solution for your problem is a simple
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
The e.Cancel=true will prevent your form from closing, so remove it.
if it still doesn't work, try this :
use the Application.Exit() method on the FormClosed event instead of FormClosing:
To do this, go into your form designer window : press maj + F7 or right click on your form in your solution explorer and click on Form designer (something like that)
Right Click on your Form, Select Properties, display the Events, then double-click on the Form Closed event and the designer will automaticaly register the event and generate your code.
You should have something like this without the Application.Exit() method generated :
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
Application.Exit();
}

How to stop textbox leave event firing on form close

using c# winforms vs2008
I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents.
My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired.
If the textbox does not have focus on form close then the form closes fine.
If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.
How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?
The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.
Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
if (closeButton.Focused)
return;
// Update the other text boxes here
}
}
The LostFocus event happens to fire after the new control receives focus.
Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.
Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.
Another approach:
Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to #Powerlord for pointing this out).
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.CausesValidation = false;
this.Close();
}
You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
}
you can check to see which control has just got focus.
private void textBox1_Leave(object sender, EventArgs e)
{
if (btnClose.Focused)
return;
// go from here
}
Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.
private void GuiltyTextBox_Leave(object sender, EventArgs e) {
Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;
.......
}
I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.
Write Following line of code in text box leave event on top
if me.closing then
return
end if

Categories