0

I have a UserControl which by default is anchored at the bottom like this:

var customTaskPaneContent = new CustomTaskPaneContent(jobId, _ipcClient, document, AddCustomTaskPane);

var customTaskPane = CustomTaskPanes.Add(customTaskPaneContent, CustomTaskPaneTitle, document.ActiveWindow);

customTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionBottom;
customTaskPane.Height = 130;
customTaskPane.Visible = true;
customTaskPane.VisibleChanged += CustomTaskPane_VisibleChanged;

customTaskPane.DockPositionChanged += CustomTaskPane_DockPositionChanged;


private void CustomTaskPane_DockPositionChanged(object sender, EventArgs e)
{
        var customTaskPane = sender as Microsoft.Office.Tools.CustomTaskPane;

        if(customTaskPane != null)
        {
            if (customTaskPane.DockPosition == Office.MsoCTPDockPosition.msoCTPDockPositionFloating)
            {
                //ATTEMPT 1
                //customTaskPane.Width = 1000;

                //ATTEMPT 2
                var userControl = customTaskPane.Control;
                var size = new System.Drawing.Size(1500, 400);
                userControl.Size = size;
            }
        }
 }

When I drag the panel and its position changes to msoCTPDockPositionFloating the assigned dimensions are too small and I would like to change its width.

I have made several attempts but the dimensions are never changed. Which is the correct way to change the Width size?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

1 Answers1

-1

Try to change the Size property of the content as well in the following way:

private void myCustomTaskPane_DockPositionChanged(object sender, EventArgs e)
{
    Microsoft.Office.Tools.CustomTaskPane taskPane =
        sender as Microsoft.Office.Tools.CustomTaskPane;

    if (taskPane != null)
    {
        // Adjust sizes of user control and flow panel to fit current task pane size.
        MyUserControl userControl = taskPane.Control as MyUserControl;
        System.Drawing.Size paneSize = new System.Drawing.Size(taskPane.Width, taskPane.Height);
        userControl.Size = paneSize;
        userControl.FlowPanel.Size = paneSize;

        // Adjust flow direction of controls on the task pane.
        if (taskPane.DockPosition == 
            Office.MsoCTPDockPosition.msoCTPDockPositionTop ||
            taskPane.DockPosition ==
            Office.MsoCTPDockPosition.msoCTPDockPositionBottom)
        {
            userControl.FlowPanel.FlowDirection =
                System.Windows.Forms.FlowDirection.LeftToRight;
        }
        else
        {
            userControl.FlowPanel.FlowDirection =
                System.Windows.Forms.FlowDirection.TopDown;
        }
    }
}

This code example assumes that the task pane contains a UserControl named MyUserControl, and the UserControl contains a FlowLayoutPanel named FlowPanel.

See CustomTaskPane Interface for the full sample code.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Do you test this code? Don't help to work out this problem. – Lucas Aug 02 '23 at 01:51
  • oh sorry, sir. indeed, this code cannot be used to set the custom task pane width or height (at least for office 2016, 2019, 365). In my practice, only drag the frame of this custom task pane window can change its width or height. I tried to do this programmly by intercepting the messages for this ctp window, `SetWindowPos`, also failed. In this way, I can change the size of the ctp window, but the child window didn't auto-resize with the parent (the ctp window). – Lucas Aug 03 '23 at 01:40
  • I'd suggest posting your question in a new thread and describe your specific scenario instead of down-voting existing answers that may be not related to your scenario. – Eugene Astafiev Aug 03 '23 at 16:11