|
I have an MDI parent form(frmMDI) with two Child Forms 9frmChild1 and frmChild2). I have a MenuStrip Control that have at the Top Level a File Menu; then below that I have a Save button. In each of the Child forms, I alson have a Save button as well as an Exit button.
In the Exit button of the Child forms, I have a code that checks if the user has unsaved changes in the text boxes; if yes, a MsgBox prompts that the user to save the changes or to exit without saving them. This works OK!
My problems are:
1. How do I Save the data in the child forms by clicking the SaveItem in my FileMenuStripItem?
2. How do I prompt the user to save unsaved data if the user clicks (a) the Child form's Close button or (b) the Parent Form's Close button? instead of the Exit button on the Child forms?
Only performance counts! | | Sylva Tuesday, March 31, 2009 8:34 PM | You must to implement a state management. i.e: adding, saving...ready, etc. This state is initialized when your mdichild form is started. The FormClosing event must to be programmed to check the reason of the close event (inclusive into the MDI parent form). You check the status propery for each mdichild form.. if this state is not equal to ready or iddle..., then you must to save the data, because this has been changed. I recommend to you uses a dictionary collection or array to track the items with changes.. on this way.. , only this items must to be affected in the save process.) Cheers from México! - Marked As Answer byLing WangMSFT, ModeratorWednesday, April 08, 2009 7:42 AM
- Proposed As Answer byTabas Thursday, April 02, 2009 12:22 AM
-
| | Tabas Thursday, April 02, 2009 12:22 AM | Hi Sylva,
1>
You can define a public method which saves the data on the child form. Then you can click the SaveItem to call the public save method. You can use ActiveMdiChild.Name to check which form is active.
2>
As @ México said, you need to handle form closing event to check if there is data have changed and unsaved.
void Form4_FormClosing(object sender, FormClosingEventArgs e)
{
if (factoryDataSet.GetChanges() != null)
{
DialogResult dr = MessageBox.Show("Do you want to save the change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
savemethod();
}
else if (dr == DialogResult.No)
{
}
else
{
e.Cancel = true;
}
}
}
If I misunderstood you, or you have other questions, please feel free to tell me.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorWednesday, April 08, 2009 7:42 AM
-
| | Ling Wang Monday, April 06, 2009 8:12 AM | You must to implement a state management. i.e: adding, saving...ready, etc. This state is initialized when your mdichild form is started. The FormClosing event must to be programmed to check the reason of the close event (inclusive into the MDI parent form). You check the status propery for each mdichild form.. if this state is not equal to ready or iddle..., then you must to save the data, because this has been changed. I recommend to you uses a dictionary collection or array to track the items with changes.. on this way.. , only this items must to be affected in the save process.) Cheers from México! - Marked As Answer byLing WangMSFT, ModeratorWednesday, April 08, 2009 7:42 AM
- Proposed As Answer byTabas Thursday, April 02, 2009 12:22 AM
-
| | Tabas Thursday, April 02, 2009 12:22 AM | Using the info I presented above, can I have a sample code snippet please? Only performance counts! | | Sylva Friday, April 03, 2009 9:48 PM | Hi Sylva,
1>
You can define a public method which saves the data on the child form. Then you can click the SaveItem to call the public save method. You can use ActiveMdiChild.Name to check which form is active.
2>
As @ México said, you need to handle form closing event to check if there is data have changed and unsaved.
void Form4_FormClosing(object sender, FormClosingEventArgs e)
{
if (factoryDataSet.GetChanges() != null)
{
DialogResult dr = MessageBox.Show("Do you want to save the change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
savemethod();
}
else if (dr == DialogResult.No)
{
}
else
{
e.Cancel = true;
}
}
}
If I misunderstood you, or you have other questions, please feel free to tell me.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorWednesday, April 08, 2009 7:42 AM
-
| | Ling Wang Monday, April 06, 2009 8:12 AM | Thanks Ling Wang. Could you pls put the code in VB. Second, I have addressed a part of my original question by calling the performClick event of the Save button of the ChildForm by the SaveMenuStripItem in the Parent Form. Third, I have removed the Exit button of the ChildForm and then used the Form_Closing Event of the ChildForm to test if there is any unsaved data. All are working fine.
What remain now is how to make the Form_Closing Event of the Parent Form to check if there is unsaved data on the ChildForm and prompt the user to save or discard it. I will appreciate valuable suggestions in this direction. Only performance counts! | | Sylva Monday, April 06, 2009 9:17 PM | Private Sub Form3_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not Me.FactoryDataSet.GetChanges() Is Nothing Then
Dim dr As DialogResult = MessageBox.Show("Do you want to save the change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If dr = DialogResult.Yes Then
Dt01BindingNavigatorSaveItem.PerformClick()
ElseIf dr = DialogResult.Cancel Then
e.Cancel = True
End If
End If
End Sub
You'd better use the child form closing event. When the parent form is closing, it will also close the child form andcall the child form closing event.
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. | | Ling Wang Tuesday, April 07, 2009 4:41 AM | Have you solved this issue? If not, I am pleasure to help you figure out.
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. | | Ling Wang Wednesday, April 08, 2009 5:40 AM | Not quite. I have been having connection (internet) issues. You'll hear from me ASAP.
Thanks for your kind concern. Only performance counts! | | Sylva Thursday, April 09, 2009 4:21 PM |
Private Sub Form3_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not Me.FactoryDataSet.GetChanges() Is Nothing Then
Dim dr As DialogResult = MessageBox.Show("Do you want to save the change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If dr = DialogResult.Yes Then
ElseIf dr = DialogResult.Cancel Then
True
End If
End If
End Sub
You'd better use the child form closing event. When the parent form is closing, it will also close the child form andcall the child form closing event.
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
I am sorry but what I want to do is this: Say, the user made some input in the MDI Child form but clicks the close button of the Parent Form or the Child form, I want the user to be alerted/prompted that he has some input not yet saved. If he clicks YES, then the Close action is cancelled and so that he can can go back and save the changes. Because this is a data input form, there are a number of validations required before the data is saved. I have all the validations in place and they're working ok. I have created a Sub that prompts the user to save changes:
Private Sub frmReceipts_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing
If (Not IsUnsavedData()) Then
e.Cancel = False
ElseIf IsUnsavedData() Then
Dim result As DialogResult _
= MessageBox.Show("Do you want to save the data you entered?", "Unsaved Data", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = Windows.Forms.DialogResult.No Then
e.Cancel = False
Else
e.Cancel = True
End If
End If
End Sub
The above code is attached to the Form_Closing event of the Child Form. It works fine therebut it does not fire when the user clicks the close button of the parent form. My problem is how to make it fire if the user clicks either the close button of the Child form or that of the parent form and possibly, if the user clicks Exit button on the Child Form. Thanks for assisting.
Only performance counts! - Edited bySylva Thursday, April 09, 2009 8:12 PMcorrection
-
| | Sylva Thursday, April 09, 2009 7:57 PM | Hi Sylva,
I guess this is becauseyou don’t set the MDIParent property to the child form.
You can put the similar function in the Parent form closing event:
Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not f3 Is Nothing Then
If f3.Visible = True Then
f3.Validate()
If Not f3.FactoryDataSet.GetChanges() Is Nothing Then
f3.Activate()
MessageBox.Show("some input not yet saved", "tip", MessageBoxButtons.OK, MessageBoxIcon.None)
e.Cancel = True
End If
End If
End If
End Sub
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. | | Ling Wang Friday, April 10, 2009 8:19 AM |
|