can't change CustomTaskPane width
- Hello,
I've added an event handler to CustomTaskPane's DockPositionChanged Event, and inside the handler I've tried to change the taskpane width and height but I get the following exception
"This property cannot be set during the object's event handler"here is the code:Private Sub myCustomTaskPane_DockPositionChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles myCustomTaskPane.DockPositionChanged
Dim taskPane As Microsoft.Office.Tools.CustomTaskPane = _
TryCast(sender, Microsoft.Office.Tools.CustomTaskPane)
If taskPane IsNot Nothing Then
If taskPane.DockPosition = _
Office.MsoCTPDockPosition.msoCTPDockPositionFloating Then
taskPane.Height = 500
taskPane.Width = 300
End If
End If
End Sub
Could anyone kindly explain to me why I got this error and how to resolve the issue
Regards,
Raja
Odpovědi
Hi Raja,
It is by designed that Width and Height property of Custom Task Pane cannot be set in event handler. If you want your custom task pane appear by default size when floating, you need initialize its size in the startup codes, but not in DockPoisitionChanged event handler.
Codes like:
Code Blockprivate Microsoft.Office.Tools.CustomTaskPane ctp = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ctp = this.CustomTaskPanes.Add(new WordAddIn111.UserControl1(), "test");
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
ctp.Width = 400;
ctp.Height = 400;
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
ctp.Visible = true;
}
Thanks
Ji
Všechny reakce
Hi Raja,
It is by designed that Width and Height property of Custom Task Pane cannot be set in event handler. If you want your custom task pane appear by default size when floating, you need initialize its size in the startup codes, but not in DockPoisitionChanged event handler.
Codes like:
Code Blockprivate Microsoft.Office.Tools.CustomTaskPane ctp = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ctp = this.CustomTaskPanes.Add(new WordAddIn111.UserControl1(), "test");
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
ctp.Width = 400;
ctp.Height = 400;
ctp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
ctp.Visible = true;
}
Thanks
Ji
- The code listed in the original question was taken directly from the msdn page that describes this event. http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.customtaskpane.dockpositionchanged(VS.80).aspx
The text on that page describes the purpose of using this event as setting the height and width of the task pane. If it is by design that this is not possible, why does the documentation give that as an example?
I have discovered the workarround that you suggest, but it still does not work in all cases.
However, the width for DockPosition right and the height for DockPosition top are not maintained when the doc position is changed from right, to floating to top.
It would really help if I could just set the height and width within this event as it's listed in the documentation.
Here is the code I'm using. (My AddSection user control has a flowlayoutpanel that is used to change the layout based on if the task pane is docked vertically or horizontally. The user control exposes the FlowDirection property of that control as it's own FlowDirection property.)
Thanks,
Dennis
Code SnippetDim
AddSectionControl As AddSection = New AddSectionDim
asPane As CustomTaskPane = Me.CustomTaskPanes.Add(AddSectionControl, "Add Section", Me.Application.ActiveDocument.ActiveWindow)Me.Application.ActiveDocument.ActiveWindow)
asPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating
'these 2 lines set task pane size when floating:
asPane.Width = 216
asPane.Height = 184
asPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight
'this line should set task pane size when docked on rightasPane.Width = 210
asPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop
'this line should set task pane size when docked at top
asPane.Height = 60
asPane.Visible =
TrueAddHandler
asPane.DockPositionChanged, AddressOf AddSectionPaneDocPositionChanged
Public Sub AddSectionPaneDocPositionChanged(ByVal sender As Object, ByVal e As EventArgs)
With CType(sender, CustomTaskPane)
Select Case .DockPosition
Case Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating
CType(.Control, AddSection).FlowDirection = FlowDirection.TopDown Case Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft, _
CType(.Control, AddSection).FlowDirection = FlowDirection.TopDownMicrosoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight
.Width = 210
'This has no effect!!! Case Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionBottom, _
CType(.Control, AddSection).FlowDirection = FlowDirection.LeftToRightMicrosoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop
.Height = 60
'This has no effect!!! End Select End With End Sub The code example in the documentation topic you referenced is wrong, and it has been fixed for the next online refresh of the VSTO documentation. The underlying Office object model for custom task panes does not enable you to modify the Height or Width properties from inside the event handlers for the VisibleChanged or DockPositionChanged events. I apologize for any confusion caused by this error in the example.
As far as I know, the values you specify for Height and Width are retained by the task pane whenever it is dragged to the position that corresponds to the DockPosition value that was set before you set the Height or Width property. For example, if you set both Height and Width to 500 after setting DockPosition to Floating, then these will be the Height and Width values used every time the pane is dragged to a floating position.
There are two caveats that I'm aware of:
-
When the pane is right/left then the Height is always the full height of the application window, and when the pane is top/bottom then the Width is always the full width of the application window.
-
If the user manually resizes the pane when it is floating or docked, then the new user's setting is retained for the new Height/Width for that dock position. I believe this behavior is intentional on part of the Office designers, so that the end user is in control of the UI as much as possible.
Are you seeing different behavior than that described above?
McLean Schofield
-
Thank you, that makes sense.
However, I am seeing different behavior.
My experience has been that setting both height and width for floating, followed by the height for horizontal positioning and the width for vertical positioning when the task pane is created doesn't work as expected. Please see the code I posted above.
When my task pane is first made visible, it is docked at the top. It's height is correct. I can drag it to a floating position and it's size is as specified. The problem arrises when I dock the pane on the right. It's width appears to be only about 50 instead of the 210 that was specified in the code. If I then resize it to the proper width (as a user would) and then drag it to the top dock position, the height is wrong (looks like about 200). The size when floating remains correct.
My only workaround for this problem has been to restrict the user from docking the control vertically. When I do that, then the size when docked on top or bottom and the size when floating are as desired.
Thanks,
Dennis
Thanks, I see the behavior you're describing now (I had never tried setting explicit Height/Width values for both top/bottom and left/right dock types before). What actually appears to be happening is that whatever the Height is set to for top/bottom dock type, the Office application is automatically setting the Width to the same value when the pane is docked left/right, no matter what Width you specify (and vice versa). I have a suspicion this symmetrical ratio enforcement might be intentional behavior when you enable both docking types, but I can't say for sure, since the CustomTaskPane class in VSTO solutions just wraps the behavior of the underlying custom task pane feature in Office applications.
I'm afraid I can't be of much more help right now - I'll try to do some more research on this and reply back to the thread with what I can discover (it might take a few days). If anyone else reading this thread knows more about this behavior, please feel free to chime in.
Thanks,
McLean Schofield
I have received confirmation that this is "by design" behavior. The Office application keeps track of two different sets of task pane dimensions (floating and docked), rather than three (floating, docked top/bottom, and docked left/right). Because it doesn't maintain different sets of dimensions for top/bottom and left/right dock states, you can't specify one Height value for when the pane is top/bottom, and a completely different Width value for when the pane is left/right.
I hope this helps,
McLean Schofield

