none
How does Custom Taskpane respond to events in which users add or delete sheets? RRS feed

  • Question

  • Hi all,
    According to this Walkthrough (https://docs.microsoft.com/zh-cn/visualstudio/vsto/walkthrough-synchronizing-a-custom-task-pane-with-a-ribbon-button), create a custom task pane (named FrmSheetsCTP.cs), then add the DataGridView control to the CTP and use it to display the sheet name.

    In FrmSheetsCTP.cs:

    public partial class FrmSheetsCTP : UserControl
        {
            public FrmSheetsCTP()
            {
                InitializeComponent();
            }
    
            private void MySheetsCTP_Load(object sender, EventArgs e)
            {
                SetDGView();
            }
    
            public void SetDGView()
            {
                Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
    
                if (wb.Worksheets.Count>1)
                {
                    foreach(Excel.Worksheet ws in wb.Worksheets)
                    {
                        DGViewSheetsName.Rows.Add();
                        DGViewSheetsName.Rows[ws.Index-1].Cells[1].Value = ws.Name;
                    }
                }
            }

    In ThisAddIn.cs:

      public partial class ThisAddIn
        {
            private CustomTaskPane frmSheetsCTP;
    
            public CustomTaskPane FrmSheetsCTP
            {
                get
                {
                    return frmSheetsCTP;
                }
            }
    
            private void AddSheetsCTP()
            {
                Application.ScreenUpdating = false;
                Excel.Application app = Globals.ThisAddIn.Application;
    
                if (app.Worksheets.Count>0)
                {
                    frmSheetsCTP = CustomTaskPanes.Add(new FrmSheetsCTP(),"Help",app.ActiveWindow);
    
                    frmSheetsCTP.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft;
    
                    frmSheetsCTP.VisibleChanged += new EventHandler(FrmSheetCTP_VisibleChanged);
                    frmSheetsCTP.Width = 282;
                    app.WorkbookNewSheet += new Excel.AppEvents_WorkbookNewSheetEventHandler(ThisWorkbook_WorkbookNewSheet);
                }
                app.ScreenUpdating = true;
            }
            
            private void ThisWorkbook_WorkbookNewSheet(Excel.Workbook Wb, object Sh)
            {
    
             ///////////////////////////
             // How does the DataGridView control on CTP respond to events in which a user adds or removes a worksheet?
             ///////////////////////////
    
            }
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                AddSheetsCTP();
            }
    Thanks in advance.

    Friday, June 1, 2018 3:53 AM

Answers

  • Hello a0a3,

    frmSheetsCTP is a CustomTaskPane object. We could try to get the true FrmSheetsCTP usercontrol from it and then call the SetDGView method.

    Here is the simple code.

     FrmSheetsCTP trueFrmSheetsCTP = (FrmSheetsCTP)frmSheetsCTP.Control;
                trueFrmSheetsCTP.SetDGView();

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by a0a3 Friday, June 1, 2018 12:26 PM
    Friday, June 1, 2018 7:19 AM

All replies

  • Hello a0a3,

    frmSheetsCTP is a CustomTaskPane object. We could try to get the true FrmSheetsCTP usercontrol from it and then call the SetDGView method.

    Here is the simple code.

     FrmSheetsCTP trueFrmSheetsCTP = (FrmSheetsCTP)frmSheetsCTP.Control;
                trueFrmSheetsCTP.SetDGView();

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by a0a3 Friday, June 1, 2018 12:26 PM
    Friday, June 1, 2018 7:19 AM
  • OK!

    Thanks again!

    Friday, June 1, 2018 10:32 AM