How do I get a reference to another control?

155
1
08-01-2018 11:51 PM
Highlighted
New Contributor II

Let's say I have the following simple case: 

I have a combobox and a button. When I press the button, I want to display the selected item from the combobox in a messagebox.

How do I, from the button click event handler, access the items and selected item from the combobox?

I have tried the pluginwrapper, but that only permits access to DAML properties (i.e. caption etc.). It is frustrating because when debugging, I can see a reference to the combobox, but it is private and cannot be accessed. I have tried casting the pluginwrapper and I have failed to find any way of unwrapping it.

I further tried to use DAML categories and Categories.GetComponentElements, however I don't get anything back there.

Full source attached (it's very simple code).

  • Am I using categories wrong? If so, how?
  • What is the right way to access elements of another control?  (either as e.g. ComboBox, or even better as the implementation ComboBox1 or whatever it may be called).
0 Kudos
1 Reply
Highlighted
Regular Contributor

Hi Daniel,

 One common pattern is to use the Module1 class to hold your selected values and allow other controls to access the value through the Module1 class (which is a singleton).  So in your example you need to add a public property, which I call 'MyComboboxValue', to the Module1 class in order to hold the combobox selection:

internal class Module1 : Module
{
  private static Module1 _this = null;

  /// <summary>
  /// Retrieve the singleton instance to this module here
  /// </summary>
  public static Module1 Current
  {
...
  }

  // Add your 'shared' properties here:
  public string MyComboboxValue { get; set; }

...
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Now you need to store the ComboBox selection by modifying the ComboBox1 class' OnSelectionChange method:

/// <summary>
/// The on comboBox selection change event.
/// </summary>
/// <param name="item">The newly selected combo box item</param>
protected override void OnSelectionChange(ComboBoxItem item)
{
  if (item == null) return;

  // save the selection in your module1 class
  Module1.Current.MyComboboxValue = item.Text;
}‍‍‍‍‍‍‍‍‍‍‍

Finally you can use the 'shared' property throughout your add-in by using the Modul1 class' Current member: Module1.Current.MyComboboxValue.  For example:

MessageBox.Show($@"From Combo Box: {Module1.Current.MyComboboxValue}");

If you need to access any values from another add-in you can use a Pro FrameworkApplication function to get the Module1 instance like this:

((Module1)FrameworkApplication.FindModule("ProAppModule1_Module")).MyComboboxValue‍

Hope this helps