47313

How to Expose Listbox in UserControl in CustomTaskPane VSTO C#

Question:

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:

<a href="https://stackoverflow.com/questions/16695909/working-with-listbox-elements-in-a-user-control" rel="nofollow">Working with ListBox elements in a user control</a>

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.

Answer1:

It appears my foreach (and hence my cast) was not correct (per Eugene Astafiev's question in the comment above). I found this <a href="https://stackoverflow.com/questions/11223451/casting-new-system-windows-forms-control-object-to-system-windows-forms-textbox" rel="nofollow">SO Post</a> 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; } }

Recommend

  • Word VBA to find line starting with “Date:” and copy line to top of respective page
  • Qt::WindowStaysOnTopHint widget is always active
  • Excel 2007 VBA Zooming (without using select?)
  • Assign even odd page numbers in word
  • Button inside Update Panel is not triggered, in asp.net
  • Operation not supported on read-only collection
  • How to create a data template dependent on an XML Attribute?
  • Bind collection of objects to ListBox
  • Where these are stored?
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • How to define and use opencv mat of user type
  • How to attach a node.js readable stream to a Sendgrid email?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Cassandra Data Model
  • JSON with duplicate key names losing information when parsed
  • htaccess rewriting URLs with multiple forward slashes
  • When should I choose bucket sort over other sorting algorithms?
  • Display Images one by one with next and previous functionality
  • Do create extension work in single-user mode in postgres?
  • Web-crawler for facebook in python
  • Apache 2.4 - remove | delete | uninstall
  • Unanticipated behavior
  • retrieve vertices with no linked edge in arangodb
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • unknown Exception android
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?