繁体   English   中英

c#“此方法或属性不可用,因为当前选择部分覆盖了纯文本内容控件。”

[英]c# “this method or property is not available because the current selection partially covers a plain text content control.”

我在Microsoft Word文档级自定义中向表添加行(即过程)的方法(AddProcedure(Procedure procedure, Word.Table table, int procedureCol, int initialsCol, int dateCol) )存在问题。

每行在第1列中包含说明,在第7列中包含下拉列表内容控件,在第9列中包含日期选择器内容控件。要添加的过程应取决于字符串成员“ engagementType”的值。

当使用样本engagementType(即“ Audit”)样本初始化engagementType时,一切都将正常运行。 但是,当用一个空字符串初始化engagementType,然后在事件处理程序中为其分配值“ Audit”,然后该事件处理程序将调用SetupProcedures方法,然后调用AddProcedure方法(我认为是有问题的方法),在第一列中仅添加了一行描述,并且然后在尝试添加下拉列表内容控件时在第7列中失败。

有人知道为什么会这样吗?

这是我的代码:

public partial class ThisDocument
    {
        #region Fields

        private string engagementType = "Audit";

        #endregion Fields

        #region Events

        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            PartnerDropdownListControl.Validated += PartnerDropdownListControl_Validated;
            UpdateUI();
        }

        private void PartnerDropdownListControl_Validated(object sender, EventArgs e)
        {
            MessageBox.Show("Partner Dropdown list Validated event handler called.");

            if (PartnerDropdownListControl.Text == "JDI")
            {
                engagementType = "Audit";
                SetupProcedures(engagementType);
            }
            else
            {
                engagementType = "Review";
                SetupProcedures(engagementType);
            }
            // throw new NotImplementedException();
        }


        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {

        }


        #endregion Events

        #region Methods

        private void UpdateUI()
        {
            ActiveWindow.View.TableGridlines = false;
            if(PartnerDropdownListControl.DropDownListEntries.Count == 0) PopulatePartnerDropdownList();
            if(PreparerDropdownListControl.DropDownListEntries.Count == 0) PopulatePreparerDropdownList();
            SetupProcedures(engagementType);
        }

        private void PopulatePartnerDropdownList()
        {
            RouteManager routeSheet = new RouteManager();

            foreach (string partner in routeSheet.Partners()) 
            {
                PartnerDropdownListControl.DropDownListEntries.Add(partner, partner);
            }
        }

        private void PopulatePreparerDropdownList()
        {
            RouteManager routeSheet = new RouteManager();

            foreach(string preparer in routeSheet.Preparers())
            {
                PreparerDropdownListControl.DropDownListEntries.Add(preparer, preparer);
            }
        }

        private void SetupProcedures(string engType)
        {
            Procedures procedures = new Procedures();
            Word.Table proceduresTable = this.Tables[2];

            int proceduresColumn = 1;
            int initialsColumn = 7;
            int dateColumn = 9;

            switch(engType)
            {
                case "Audit":
                    foreach(Procedure procedure in procedures.AuditProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                case "Review":

                    foreach (Procedure procedure in procedures.ReviewProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                case "Compilation":

                    foreach (Procedure procedure in procedures.CompilationProcedures())
                    {
                        AddProcedureRow(procedure, proceduresTable, proceduresColumn, initialsColumn, dateColumn);
                    }
                    break;

                default:
                    break;
            }

            // Add botton border to each cell in the sign-off initials column & sign-off date column.
            for (int row = proceduresTable.Rows.Count; row > 0; row--)
            {
                proceduresTable.Cell(row, initialsColumn).Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                proceduresTable.Cell(row, dateColumn).Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            }

            // Set the table Column widths.
            proceduresTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow);
        }

        private void AddProcedureRow(Procedure procedure, Word.Table table, int procedureCol, int initialsCol, int dateCol)
        {
            table.Rows.Add();
            int row = table.Rows.Count;

            RouteManager routeSheet = new RouteManager();

            DropDownListContentControl signOffDropdownList;
            string signOffDropdownListName = $"signOffDropdownList{row}";
            DatePickerContentControl signOffDatePicker;
            string signOffDatePickerName = $"signOffDatePicker{row}";

            table.Cell(row, procedureCol).Range.Text = procedure.Description;

            try
            {
                MessageBox.Show("Try block called.");
                signOffDropdownList = Controls.AddDropDownListContentControl(table.Cell(row, initialsCol).Range, signOffDropdownListName);
            }
            catch (Exception)
            {
                MessageBox.Show("Catch block called.");
                throw;
            }
            signOffDropdownList.PlaceholderText = "Sign-Off";

            foreach (string employee in routeSheet.Employees())
            {
                signOffDropdownList.DropDownListEntries.Add(employee, employee);
            }

            signOffDatePicker = Controls.AddDatePickerContentControl(table.Cell(row, dateCol).Range, signOffDatePickerName);
            signOffDatePicker.PlaceholderText = "Sign-Off Date";
        }

        #endregion Methods

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(this.ThisDocument_Startup);
            this.Shutdown += new System.EventHandler(this.ThisDocument_Shutdown);

        }

        #endregion

    }
}

我知道了。 在调用setuprocedures方法之前,我需要在partnerdropdownlist内容控件之外选择一个范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM