Cancelling the wrong EventArgs when setting the property Cancel true

Posted by: sandro.pegoraro on 27 January 2020, 2:21 am EST

    • Post Options:
    • Link

    Posted 27 January 2020, 2:21 am EST

    Hi there everyone,

    Lately my company have been updating our ComponentOne Studio for WinForms version in our Softwares, and one behavior that used to work in one of our C1FlexGrid, doesn’t work anymore.

    The main flow was: clicking in multiple rows in the C1FlexGrid and then clicking in a CellButton of a selected row, we have a call to a BeforeSelChange before the CellButton event:

            
    	/// <summary>
            /// After using CTRL to select multiple rows in grid (Exams)
            /// Click in MPP buttons doesn't change the current selection
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void fgExams_BeforeSelChange(object sender, [b]C1.Win.C1FlexGrid.RangeEventArgs e[/b])
            {
    	    // Cast flex grid to our MultiSelectFlexGrid (a C1FlexGrid with selection overrides)
                MultiSelectFlexGrid grd = sender as MultiSelectFlexGrid;
                if (grd == null) return;
    
                if (grd.SelectedCells.Count > 0)
                {
                    // The new selection should be of one specific row and one specific column (cell with a cellbutton)
                    if (e.NewRange.c1 != e.NewRange.c2 || e.NewRange.r1 != e.NewRange.r2)
                        return;
                    int iSelectedRow = e.NewRange.r1;
                    int iSelectedCol = e.NewRange.c1;
    
                    // Se linha selecionada estava na seleção anterior, e existe mais de uma linha selecionada
                    if (grd.SelectedRows.Contains(iSelectedRow) && grd.SelectedRows.Count > 1)
                    {
    		    // Check if the click is on the "MPP" column, if it is, maintains the selection cancelling the SelChange EventArgs
                        if (grd.Cols[iSelectedCol].Name.Equals("MPP"))
                            e.Cancel = true;
                    }
                }
            }
    
    

    The thing is that, after this BeforeSelChange event, the callstack triggers the CellButtonClick event with a RowColEventArgs, and the difference is that when cancelling the event in BeforeSelChange it is now cancelling also the call to this CellButtonClick event.

    When I removed the e.Cancel, the CellButtonClick is triggered correctly but that goes back to the original problem which led to the implementation of the BeforeSelChange that is losing the selection when clicking in a CellButton of a already selected cell.

    I want to know if have anything changed in regard of the event callstack or the manipulation of eventargs objects in event calls, or if anyone have an idea about what happened or how we could solve this…

    Thanks in Advance

    Specifications:

    .NET Framework version: 4.7.2

    Before Update

    C1 Studio for WinForms version: 4.0.20151.49

    After Update

    C1 Studio for WinForms version: 4.0.20193.393.

  • Posted 27 January 2020, 10:00 pm EST

    Hi,

    It is the default behavior that BeforeSelChange event will fire whenever the selection changes and cell button will be visible only when the cell with the button has focus so cancelling the BeforeSelChange event will not allow CellButtonClick event to fire because the button will not appear.

    As per your given code snippet we could see that you are some custom control so please share a stripped down sample of your application replicating the issue.

    If you also share a GIF of the issue you facing and your expected result then it will be helpful for us to solve the issue at your end.

    Regards,

    Prabhat Sharma.

  • Posted 28 January 2020, 12:18 am EST

    Hello Prabhat, thanks for answering.

    I solved the problem.

    The MultiSelectFlexGrid used in our project is/was one C1FlexGrid WinForms samples, it manages the SelectionRows, SelectionCols and SelectionCells.

    It also implements the event of OnAfterSelChange event, which in there we can properly control if the selection need to change or doesn’t. And for my problem, we validated it by calling the method C1FlexGrid.HitTest():

            
            override protected void OnAfterSelChange(RangeEventArgs e)
            {
                base.OnAfterSelChange(e);
    
                if (this._selections.Contains(e.NewRange))
                {                
                    if (this.HitTest().Type == HitTestTypeEnum.EditButton)
                        return;
                }
    ...
    
    

    This was enough for maintaining the selection after the CellButtonClick

  • Posted 28 January 2020, 5:41 pm EST

    Hi Sandro,

    We are happy to hear that the problem has been solved at your end.

    If you need any other help, please let us know.

    Regards,

    Prabhat sharma.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels