如何在CustomTaskPane VSTO C#中的UserControl中公开列表框 [英] How to Expose Listbox in UserControl in CustomTaskPane VSTO C#

查看:284
本文介绍了如何在CustomTaskPane VSTO C#中的UserControl中公开列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#VSTO创建Word 2010的应用程序级加载项.我创建了一个用户控件,在其中放置了控件,然后使用该用户控件添加了自定义任务窗格:

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.

我尝试将列表框上的Modifiers属性设置为Public.我尝试将用户控件上的列表框公开为公共属性:

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; }
    }
 }

另外,我看了这篇SO帖子:

Additionally, I looked at this SO post:

在用户控件中使用ListBox元素

我希望我可以适应它,但是我的列表框IntelliSense没有FindControl,而是提供了FindForm.是否可以通过某种方式将自定义任务窗格"解释为表单来访问用户控件中的列表框?任何帮助,我们将不胜感激.

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.

推荐答案

看来我的foreach(因此我的演员表)不正确(根据以上评论中Eugene Astafiev的问题).我发现了这个 SO Post 建议使用类型Control而不是UserControl循环.我做到了,一切都很好.这是代码:

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;
    }
}

这篇关于如何在CustomTaskPane VSTO C#中的UserControl中公开列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆