Manipulate control on other form - c#

I'm trying to manipulate a PictureBox(pBATalk) to show whenever I close a form(PAInput). So basically I want to show a picture, whenever the 2nd form is closed.
2nd Form
public void PAInput_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
fPAMain.PATalkActive(false);
e.Cancel = true;
}
MainForm:
public void PATalkActive(bool active)
{
//MessageBox.Show("");
if (active == true)
{
pBPATalk.Hide();
}
if (active == false)
{
pBPATalk.Show();
}
}
Whenever I close PAInput it will trigger PATalkActive, but will only fire the MessageBox.Show(""); and not the pBATalk.Hide(); or pBATalk.Show();

Well, I'm not so sure about what you are trying to accomplish here but first of all you're keeping PAInput open, with this line e.Cancel = true . Besides that, MessageBox.Show(""); will open a modal window displaying a text, until it is closed the rest of the method won't run.

Related

Check for changes on a windows form before closing

This should be an easy google search but I can't find the answer.
When I click the close button on my form I want to check and warn the user if information on the form has changed. For this my code is adequate.
But if the user clicks the X button on the top of the form no check is performed.
So I tried using the "FormClosing" event to execute the same code below but it goes into some weird loop, I think because i have used this.close which also triggers the same FormClosing event.
What is the correct way to check for form changes when either a button or the X is clicked.
private void buttonClose_Click(object sender, EventArgs e)
{
// Close the form and warn if record not saved
//Check for unsaved changes
if (formDataChanged == true)
{
DialogResult dr = new DialogResult();
dr = MessageBox.Show("Data on this form has changed, Click OK to discard changes", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (dr == DialogResult.OK)
{
this.Close();
}
}
else
{
this.Close();
}
}
Update
I think the linked example may be too simple?
When I looked at the CloseReason I received the same results when I closed the window either way so I'm not sure how I can use this to trigger different results.
CloseReason = UserClosing
Cancel = False
private void FormTelephoneLog_FormClosing(object sender, FormClosingEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
}
I tried adding a bool variable as was also suggested in the other post but it still loops. I know why because the this.close() command also triggers FormClosing event but I still can't figure out how I should do this correctly.
I see there are 6 possible scenarios.
No changes, Close Button -> Works
No changes, X Button -> Loops
Discard changes, Close Button -> Works
Discard changes, X Button -> Loops
Don't Discard changes, Close Button -> Works
Don't Discard changes, X button -> Fails ***
*** The form is still closed and the changes are discarded.
Here is the code as it is now. I'm going round and round including more and more conditions. This would seem like a very common thing to want to do. I'm missing something obvious.
private void buttonClose_Click(object sender, EventArgs e)
{
CloseButtonClicked = true;
checkFormChanges();
CloseButtonClicked = false;
}
private void FormTelephoneLog_FormClosing(object sender, FormClosingEventArgs e)
{
if (CloseButtonClicked == false)
{
checkFormChanges();
}
}
private void checkFormChanges()
{
// Close the form and warn if record not saved
//Check for unsaved changes
if (formDataChanged == true)
{
DialogResult dr = new DialogResult();
dr = MessageBox.Show("Data on this form has changed, Click OK to discard changes", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (dr == DialogResult.OK)
{
formDataChanged = false;
this.Close();
}
}
else
{
this.Close();
}
}
Update 2
I just kept adding conditions until all the possibilities worked.
I think it is a confusing solution but it works.
Working Code
private void buttonClose_Click(object sender, EventArgs e)
{
closeButtonClicked = true;
checkSaveChanges();
closeButtonClicked = false;
}
private void FormTelephoneLog_FormClosing(object sender, FormClosingEventArgs e)
{
if (closeButtonClicked == false)
{
if (formDataChanged == true)
{
checkSaveChanges();
}
e.Cancel = closeCancelled;
closeCancelled = false;
}
}
private void checkSaveChanges()
{
// Close the form and warn if record not saved
//Check for unsaved changes
if (formDataChanged == true)
{
DialogResult dr = new DialogResult();
dr = MessageBox.Show("Data on this form has changed, Click OK to discard changes", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
if (dr == DialogResult.OK)
{
formDataChanged = false;
this.Close();
}
else
{
closeCancelled = true;
}
}
else
{
this.Close();
}
}
One does not tell a closing form to close. You either let it close or cancel it, that's all there is to it.
Calling this.Close() will warp space-time around itself, cause an infinite loop, and annoy a certain Time lord. Pray that you don't get a Q, they're a bunch of pricks.
Here's an example:
bool UnsavedChanges; // Some bool your controls set true during change events.
// Set it false after saving and after loading things
// ie.: in a RichTextBox when loading a file, so doesn't count as a "change".
void FormClosingEvent(object sender, FormClosingEventArgs e) {
if (UnsavedChanges) {
var result = MessageBox.Show("You have unsaved changes!", "Quit without saving?", MessageBoxButtons.YesNo);
if (result == DialogResult.No) {
e.Cancel = true;
}
}
}
You were thinking too hard.
I ended up figuring it out.
Seems like an ugly solution to me but it works.
See Update 2 in my question above.

Validating form close infinite loop

I am creating a small project using winforms + c#, I have an issue in that I am using a second form in my project as a dialogue box. When the user attempts to close this I'd like to provide them with a confirmation screen to prevent any data loss in closing the form. This dialogue box form will also feature a 'home' button, closing the dialogue box form leaving them with the main window again. The problem arises when the windows X button is pressed at the top of the screen requiring me to set up an 'on form close' event to manage. This however creates an infinite loop with my current code shown below. Is there any way to avoid this?
private void frmCreateRoute_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult Safe_to_exit_check = MessageBox.Show("Are you sure you would like to go to the home screen? \n(Any entered data will be lost.)", "", MessageBoxButtons.YesNo);
if (Safe_to_exit_check == DialogResult.Yes)
{
this.Close();
}
}
and a simple:
private void Home_button_Click(object sender, EventArgs e)
{
this.Close();
}
for the home button.
Thanks
In your FormClosing, set:
e.Cancel = true;
to prevent closing the form. Don't use this.Close() there.
You need to set e.Cancel=true if the user select any thing different than DialogResult.Yes, otherwise let the form close:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show("Are you sure you want to close the form?",
"Close", MessageBoxButtons.YesNoCancel);
if (result != System.Windows.Forms.DialogResult.Yes)
e.Cancel = true;
}

C# - Override the Standard Windows Close Button to Pop-up my Custom Form

Yes, noob question. My apologies.
When users click on the red x button on the window, I want to pop up a message asking if they really would want to quit. I found a similar question on this site: Override standard close (X) button in a Windows Form.
The thing is, I want to customize the font and the MessageBoxIcon for the MessageBox, and sadly it can't be done (or will take a lot of effort to be done). So, I've decided to make my own form.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (txtID.Text != "" || txtPassword.Text != "")
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// Confirm user wants to close
new formConfirmExit().ShowDialog();
}
}
I added this code under the main form. However, when I run my code and I click on the standard close button, my pop up (the custom form I did) doesn't do what it's job. Suppose I click the "No" button, it terminates my entire program. With the "Yes" button, the pop-up shows up again, and then everything kinda stops (on Visual Studio) and ta-da! an exception.
BTW, these are the Yes and No button methods (from my Custom Form's class):
private void btnYes_Click(object sender, EventArgs e)
{
Application.Exit(); // terminate program (exception is in here)
}
private void btnNo_Click(object sender, EventArgs e)
{
this.Close(); // close this pop up window and go back to main window
}
Changing Application.Exit() to Environment.Exit(0) did the job for the Yes button, but my No button, well, terminates the program, still.
Edit: When I click on the Yes button, the pop-up/my custom form shows again (just one time). It'll stay on that state (I can click on the Yes button repeatedly yet nothing happens). The InvalidOperationException is thrown when I click the Yes button first (note the first sentence of this paragraph) then the No button.
Thank you.
Add this in your No_Click:
private void btnNo_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.No;
}
Then, change your forms closing event to the following:
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (txtID.Text != "" || txtPassword.Text != "")
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown
|| e.CloseReason == CloseReason.ApplicationExitCall)
return;
// Confirm user wants to close
using(var closeForm = new formConfirmExit())
{
var result = closeForm.ShowDialog();
if (result == DialogResult.No)
e.Cancel = true;
}
}
}
First, it checks if the form isn't closing through Application.Exit(), this may be triggered from your other form, so it will not reshow the custom MessageBox.
Second, you create a using statement around your custom form. This way you can preserve the values. You then set the dialogresult to no, if the user doesn't want to cancel. If this is the case, set e.Cancel = true to stop from exiting.

Validating user's input before continuing program execution

I have created a program that contains (for now) 2 forms.
In the first form I am asking the user for a file. After the user has selected a file, another form is called followed by closing of the current form.
An if statement indicates if the user has inserted a file when the Open File button is pressed, and if not, the second form will not be loaded.
The problem is that if the user clicks the Close button on the first form (the current one), the form closes and the next one is called.
The options of the next form are based on the user's input in the first form (where the user is asked to select a file), so if the second form is called when the user cancels the first form, it will create problems for methods in the second form.
Any ideas about how to handle the Close Button?
If you want to prevent closing the form you can handle the event
bool cancel = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = cancel;
base.OnFormClosing(e);
}
Remember to change cancel to false when it's done to close the form.
There is an event named "FormClosing" on forms.
A quick sample:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to quit?", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
I am assuming that you have an OpenfileDialog (to allow the user to select a file),and a button probably named Open File to pass the filename to next form.If this is the case,then you can try to to disable the open button if no file has been selected.
Consider the code below as the function where all logic takes place;
private void BrowseFile()
{
//dlgopenfile is the name of Openfiledialog that allows the user to browse for a file.
//string filename is the name of selected file if any.
//Form2 is the next form.
try
{
switch (dlgopenfile.ShowDialog())
{
case DialogResult.OK://If Ok(Yes) button is pressed on Openfiledialog.
filename = dlgopenfile.FileName;
break;
case DialogResult.Cancel://If Cancel button is pressed on Openfiledialog.
filename = "";
break;
}
if (filename.Length >= 1)
{
if (File.Exists(filename) == true)
{
ButtonOpenFile.Enabled = true;
}
else
{
ButtonOpenFile.Enabled = false;
throw new FileNotFoundException("The file you selected does not exist.");
}
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message, "Form1", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The next function occurs if user tries to close the form in mid session.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
switch (MessageBox.Show("Do you want to exit ?", "Form1", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
{
case DialogResult.Yes:
this.Close();
break;
case DialogResult.No:
e.Cancel = true;
break;
}
}
catch (Exception)
{
//Exception handling code goes here.
}
}
Finally the function below calls Form2's constructor with the selected file as argument.
private void ButtonOpenFile_Click(object sender, EventArgs e)
{
//This Button is enabled only if the file has been selected and if its exists.
Form2 form2 = new Form2(filename);//filename is the name of selected file,as decided in function BrowseFile().
this.Close();//Close Form1.
form2.ShowDialog();//Show Form2 as modal dialog.
}
Hope it would help you achieve what you need.Anything more,please let me know.
Proposally you start you form with form.ShowDialog(), which returns DialogResult. You should check whether it is DialogResult.Ok or whether form.DialogResult != DialogResult.None. In the form, if the user inserts the file you can set the form.DialogResult explicitely to DialogResult.Ok
Just you can handle the close event with your own logic
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show(text:"Are you sure you want to quit?",caption:string.Empty,buttons: MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}

c# cancel application from closing if hidden form has unsaved changes

after searching google and stackoverflow I'm unable to find an answer that helped me in my situation. I've got an application with a start menu(form). when the user presses the X(close) of a (any) form I reload the start menu. Now when that(menu) form gets closed I want to check and notify the user wheter any now hidden forms are in editing mode (with or without unsaved changes) if user presses cancel I want to show that form and stop the application from closing. Now my problem is how do i stop the application from stopping in the code of the other forms. I have an override of the dispose method of the subforms that calls this.close so the ok/cancel messagebox shows but after the MB the start menu closes anyway stopping the program. Should I look for a different method of handling these things or is there a method or eventhandler to modify so this /\ can work?
EDIT:
ok here's parts of the code in order of being called. Where does i go wrong?
private void Menu_FormClosing(object sender, FormClosingEventArgs e)
{
Global.Forms.Remove(this);
if (!Global.Clean_Forms())
{
e.Cancel = true;
Global.Forms.Add(this);
}
}
public static void Clean_Forms()
{
foreach (Form f in Forms)
{
if (f is Menu)
{
//do nothing
}
else
{
if (!f.IsDisposed)
{
f.Close();
}
}
}
if (Forms.Count != 0)
{
isClean = false;
/* String a = "";
foreach (Form f in Forms)
{
a += f.ToString() + ": ";
}
MessageBox.Show(a);*/
}
else
{
isClean = true;
}
}
Yet this doesnt work, application just closes down.
the closing event handler of random form
private void persoon_form_FormClosing(object sender, FormClosingEventArgs e)
{
if (editing)
{
DialogResult dr;
dr = MessageBox.Show("uw wijzigingen gaan verloren. Doorgaan?", "sluiten", MessageBoxButtons.OKCancel);
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else if (dr == DialogResult.OK)
{
Global.size = this.Size;
Global.position = this.Location;
Global.Forms.Remove(this);
Form f = Global.menu();
f.Show();
this.Dispose();
}
}//somethingelse}
EDIT: #cody gray changed onclose and clean_form still no effect, (shouldn't the messagebox in the Closing event of the subform be shown anyway? cause it doesnt)
You should not be doing this in the Dispose method. Instead, try handling the FormClosing event.
This event occurs before the form is closed, so you can check whatever state you need to, and cancel the close if necessary by setting e.Cancel to True.
For example:
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// Check to see if the user is allowed to close this form
if (!allowClose)
{
// Prevent this form from being closed
MessageBox.Show("This form cannot be closed yet!");
e.Cancel = true;
}
}
I think i figured it out! I changed two things. 1 I made sure that I didn't call this.dispose anywhere as that will result in disposing (not closing) any child forms hence no onclosing event is called. 2. the foreach loop when checking if main menu can close generated collection was modified errors. so with this I made sure it closes in the right order thnx Cody Gray for pointing me in the right direction:
Stack<Form> stack = new Stack<Form>();
foreach (Form f in Forms)
{
if (f is Menu)
{
//do nothing
}
else
{
if (!f.IsDisposed)
{
stack.Push(f);
}
}
}
for (int i = 0; i < stack.Count; i++)
{
Form temp = stack.Pop();
temp.Close();
}

Categories

Resources