【问题标题】:How to bind a TextBox on a Formregion to a custom property?如何将 Formregion 上的 TextBox 绑定到自定义属性?
【发布时间】:2021-11-27 14:32:52
【问题描述】:

C#、VSTO、Outlook 2016

我创建了一个带有 TextBox 的 Outlook Formregion。
我想将 TextBox 控件绑定到自定义属性。

我找到了 SetControlItemProperty 方法:
https://docs.microsoft.com/en-us/office/vba/api/outlook.formregion.setcontrolitemproperty

c#中是否有类似的方法以及如何使用此方法
应该在哪里调用这个方法?

【问题讨论】:

    标签: c# outlook vsto outlook-addin


    【解决方案1】:

    如果是托管加载项,您需要以编程方式设置属性。对于 VSTO 加载项,没有自动绑定。

    您可以在运行时使用任何标准 Outlook 机制(例如 UserPropertiesCustomProperties)读取属性值,并以编程方式设置控件的 Text 属性。

    【讨论】:

      【解决方案2】:

      好的,谢谢尤金。

      所以以下是不可能的:

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

      但智能感知功能显示了 SetControlItemProperty 方法。
      没有机会用这个吗?

      Intellisense

      【讨论】:

      • 在这种情况下,您是否为文本框控件正确设置了属性值?
      • SetControlItemProperty 调用中的属性名称部分正确。 mTextbox 控件也是。但我得到一个 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: System.__ComObject has no definition of mTextBox??!?不,没有错别字!
      • 这就是为什么我建议您自己以编程方式设置属性。我想这是为了兼容在 Outlook 中设计的非托管表单区域,而不是在 Visual Studio 中。
      【解决方案3】:

      Würg,谢谢尤金。 如果 Micro'!%-&%",生活会变得如此简单吗?...

      到目前为止我的解决方案(仅限约会):

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

      感谢任何改进,因为我是这个 Outlook 世界的初学者。

      【讨论】:

        猜你喜欢
        • 2018-09-03
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多