How to bind a TextBox on a Formregion to a custom property?

Viewed 59
3 Answers

In case of managed add-ins you need to set the property programmatically. There are no automatic bindings in case of VSTO add-ins.

You can read a property value at runtime using any standard Outlook mechanisms like UserProperties or CustomProperties and set the Text property of your control programmatically.

Ah ok, thank you Eugene.

So the following is not possible:

using Outlook = Microsoft.Office.Interop.Outlook;
using MSForms = Microsoft.Vbe.Interop.Forms;

namespace myHumbleAddin
{
  partial class frmDataTable
  {
    private void frmDataTable_FormRegionShowing(object s, EventArgs e) {
      Outlook.FormRegion mRegion = this.OutlookFormRegion;
      mRegion.SetControlItemProperty(mRegion.Form.mTextBox, "userField");
    }
  }
}

BUT the intellisense feature shows the SetControlItemProperty method.
Is there no chance to use this?

Intellisense

Würg, thank you Eugene. Life could be so easy if Micro'!%-&%"? ...

My solution so far (only for appointments):

using Outlook = Microsoft.Office.Interop.Outlook;

namespace myHumbleAddin
{
  partial class frmDataTable
  {
    private void frmDataTable_FormRegionShowing(object sender, System.EventArgs e) {
      Outlook.FormRegion currentRegion = this.OutlookFormRegion;
      Outlook.AppointmentItem currentItem = OutlookItem as Outlook.AppointmentItem;

      // Bind a control on the form to a UserPropertyField
      void bindControl (System.Windows.Forms.Control control, string customProperty) {

        string _value = currentItem.UserProperties[customProperty].Value.ToString();

        if (!string.IsNullOrEmpty(_value)) {
          control.Text = _value;
        }
      }

      if ( (currentItem != null) && (currentItem is Outlook.AppointmentItem) ) { 
        bindControl(tbBez, "Bezeichnung");
      }
    }

    private void frmDataTable_FormRegionClosed(object sender, System.EventArgs e) {
      Outlook.AppointmentItem currentItem = OutlookItem as Outlook.AppointmentItem;
      
      void setControlContents(System.Windows.Forms.Control control, string customProperty) {

        string _value = currentItem.UserProperties[customProperty].Value.ToString();

        if (control.Text.Length != 0 && control.Text != _value) {
          currentItem.UserProperties[customProperty].Value = control.Text;
        }
      }

      setControlContents(tbBez, "Bezeichnung");
    }
  }
}

Any improvements are appreciated since I'm a beginner in this Outlook world.

Related