问题
I am creating an application level add-in for Word 2010 using C# VSTO. I created a User Control, placed controls within it, and then used the User Control to add a Custom Task Pane:
UserControl myUserControl;
myUserControl = new PageElementsPane();
mytaskPane = this.CustomTaskPanes.Add
(myUserControl, "Page Elements", doc.ActiveWindow);
So far so good. However, the User Control contains a listbox that I have not been able to access after the Custom Task Pane is added to the Custom Task Pane collection.
I have tried setting the Modifiers property on the listbox to Public. I have tried exposing the listbox on the User Control as a Public Property:
public partial class PageElementsPane: UserControl
{
public ListBox ElementsPaneListBox
{
get { return lbxListbox; }
}
}
Additionally, I looked at this SO post:
Working with ListBox elements in a user control
I hoped I could adapt it, but my listbox IntelliSense does not have FindControl
, offering FindForm
instead. Is there a way to access the listbox within the User Control by somehow interpreting the Custom Task Pane as a form? Any help is greatly appreciated.
回答1:
It appears my foreach
(and hence my cast) was not correct (per Eugene Astafiev's question in the comment above). I found this SO Post that suggesting looping with type Control
instead of UserControl
. I did this and all was good. Here is the code:
foreach (Control lbxControl in myUserControl.Controls)
{
if (lbxControl is ListBox)
{
((ListBox)lbxControl).SelectedIndex = 1;
}
}
来源:https://stackoverflow.com/questions/30631473/how-to-expose-listbox-in-usercontrol-in-customtaskpane-vsto-c-sharp