locked
Incident Form onPreviewSubmit and cancelling RRS feed

  • Question

  • I have implemented a user control for incident and in the preview submit event I try to cancel form submission, the event is triggered, message is shown, e.Cancel = true is executed, yet the form is submitted and everything saves!

    [ContentProperty("SelectedItem")]
        public partial class IncidentUC : UserControl, INotifyPropertyChanged
        {
            EnterpriseManagementGroup mg;
            IDataItem instance = null;
    
            private bool loading = true;
            public IncidentUC()
            {
                InitializeComponent();
                loading = true;
            }
    
            
            public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(String),
                typeof(IncidentUC), new UIPropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemChanged)));
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public String SelectedItem
            {
                get
                {
                    return (String)base.GetValue(SelectedItemProperty);
                }
                set
                {
                    base.SetValue(SelectedItemProperty, value);
                    NotifyPropertyChanged("SelectedItem");
                }
            }
            
    
            private void IncidentUC_OnLoaded(object sender, RoutedEventArgs e)
            {
                if (loading)
                {
                    //FormView MyMainForm = FormUtilities.Instance.GetFormView(this);
                    //MyMainForm.Form.AddHandler(FormEvents.PreviewSubmitEvent, new EventHandler<PreviewFormCommandEventArgs>(this.OnPreviewSubmit));
                    FrameworkElement frameworkElement = this as FrameworkElement;
                    while (frameworkElement != null)
                    {
                        try
                        {
                            frameworkElement = frameworkElement.Parent as FrameworkElement;
                            frameworkElement.AddHandler(FormEvents.PreviewSubmitEvent, 
                                new EventHandler<PreviewFormCommandEventArgs>(this.OnPreviewSubmit));
                        }
                        catch (Exception)
                        {
                        }
                    }
    
                    loading = false;
    
                    
                }
    
                
            }
    
            // on preview submit event trigger calculation event
            private void OnPreviewSubmit(object sender, PreviewFormCommandEventArgs e)
            {
                // do custom processing here
                instance = (this.DataContext as IDataItem);
    
                if(instance["TierQueue"] == null)
                {
                    System.Windows.Forms.MessageBox.Show("Emty Sypport Group", "Support Group is Empty",
                        MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RtlReading);
                    e.Cancel = true;
                    return;
                }
            }
    
            private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                //System.Windows.Forms.MessageBox.Show("DataContext changed");
            }
    
        }

    My second question is about correct way of adding handler for preview submit event:

    if you look at my code, you will see two commented lines of code which actually triggers the vent as well.

    Thanks


    YSobhdel

    Thursday, June 16, 2016 10:50 AM

Answers

  • The problem lies in which control you are adding the event. We must add the event to main form, some thing like this will do the trick:

    DependencyObject IncidentFormControl = GetParentDependencyObject(this, "Microsoft.EnterpriseManagement.ServiceManager.Incident.Forms.IncidentFormControl");
                UserControl frmMain = (UserControl)IncidentFormControl;          
                // hook to handle form events
                frmMain.AddHandler(
                    FormEvents.PreviewSubmitEvent,
                    new EventHandler<PreviewFormCommandEventArgs>(this.OnPreviewSubmit));

    Hope this help some body!


    YSobhdel

    • Marked as answer by Yasser Sobhdel Saturday, November 9, 2019 7:39 AM
    Saturday, November 9, 2019 7:39 AM

All replies

  • Here is what I did to strip the error:

    As far as I could test within 2 days, e.Cancel does work when there is no class extension, I downloaded SCSMControl solution of Anton Gritsenko and added PreviewSubmit event, cancelling did work perfectly; But if I extend the incident class in the same or different management pack, cancelling does not work at all.

    I used to write the same code on SCSM 2010 and it worked just fine.

    I don't know what I am missing.


    YSobhdel

    Friday, June 17, 2016 8:28 PM
  • It didn't work in 2012 SP1 either.

    Here is the link to minimal solution:

    https://1drv.ms/u/s!Am9Uy77aJD3igWjBrTxT9INChZzC

    I have a MessageBox and e.Cancel=true in the PreviewSubmit event. To make the sample work, remove Incident class customization.


    YSobhdel


    Saturday, June 18, 2016 6:29 AM
  • Any updates? Would you please test my shared solution and see if it works in your environment? Can somebody share a working minimal sample?

    Thanks in advance


    YSobhdel

    Monday, June 20, 2016 6:17 PM
  • The problem lies in which control you are adding the event. We must add the event to main form, some thing like this will do the trick:

    DependencyObject IncidentFormControl = GetParentDependencyObject(this, "Microsoft.EnterpriseManagement.ServiceManager.Incident.Forms.IncidentFormControl");
                UserControl frmMain = (UserControl)IncidentFormControl;          
                // hook to handle form events
                frmMain.AddHandler(
                    FormEvents.PreviewSubmitEvent,
                    new EventHandler<PreviewFormCommandEventArgs>(this.OnPreviewSubmit));

    Hope this help some body!


    YSobhdel

    • Marked as answer by Yasser Sobhdel Saturday, November 9, 2019 7:39 AM
    Saturday, November 9, 2019 7:39 AM