Reputation: 5082
Hi i have a tab load this usercontrol. when i wish to close this tab, i wish to call this cancelbutton_click event to pop up confirmation on closing, if OK, then close, if Cancel, the tab stays.
if i use Unloaded event, it will pop up twice before closing.
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
cancelbutton_click(sender,null);
}
Cancel button:
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
MessageBoxResult objResult = MessageBox.Show("\nAre you sure you want to cancel?", "Cancel Confirmation", MessageBoxButton.OKCancel);
if (objResult == MessageBoxResult.OK)
{
try
{
TabItem tabItem = parentWindow.FindTabItemByName(ControlType.BusinessesContractors.ToString(), false);
this.parentWindow.mainTabControl.Items.Remove(tabItem);
this.parentWindow.statusTextBlock.Text = "Ready";
}
catch (Exception ex)
{
}
}
how to resolve this? thanks
Upvotes: 1
Views: 4079
Reputation: 184376
Unloaded
is called when the control is already being removed, it's not an event you want to handle often, just create a button which is supposed to close the tab, handle it's click, check if the user wants to cancel via the dialogue and close the tab if he does not.
Upvotes: 2