C# 使用C访问excel工作表上的滚动条控件#

C# 使用C访问excel工作表上的滚动条控件#,c#,office-interop,excel,controls,scrollbar,C#,Office Interop,Excel,Controls,Scrollbar,我想使用将滚动条控件添加到excel工作表中。我不想使用Windows.Forms.Scrollbar,而是Excel提供的 添加控件没有问题-我的问题是访问创建的控件以定义最小值、最大值和单元格引用。这是我的密码: public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int Referenc

我想使用将滚动条控件添加到excel工作表中。我不想使用
Windows.Forms.Scrollbar
,而是Excel提供的

添加控件没有问题-我的问题是访问创建的控件以定义最小值、最大值和单元格引用。这是我的密码:

    public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int ReferenceRow, int ReferenceColumn, int MaxValue, int MinValue = 0, int CurrentValue = 0)
    {
        int Left, Top, Right, Bottom;
        if(StartCellRow > EndCellRow || StartCellColumn > EndCellColumn) { throw new System.Exception("EROR: Please check Start- and Endcell reference or flip it!"); }

        Left = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Left;
        Top = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Top;
        Right = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Left + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Width);
        Bottom = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Top + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Height);

        Ex.ControlFormat SB = ExSH.Shapes.AddFormControl(Ex.XlFormControl.xlScrollBar, Left, Top, Right - Left, Bottom - Top) as Ex.ControlFormat;
        SB.Value = CurrentValue; /*ERROR: Object reference not set to an instance of an object*/
        SB.Min = MinValue;
        SB.Max = MaxValue;
        SB.LinkedCell = (ExSH.Cells[ReferenceRow, ReferenceColumn] as Ex.Range).ToString();
    }
错误发生在第12行(SB.Value=…)
当我将行
SB.Value=…
注释为
SB.LindedCell=…
时,代码运行良好。打开创建的Excel工作簿并选择相应的工作表,我可以找到创建的滚动条。 当我在创建滚动条后中断时,SB是

有人能帮我吗?我对这些东西不太在行

提前谢谢大家,,

啊,我找到了。某人是个前妻。然后,您可以通过SB.ControlFormat.Min、Max、Value等访问参数

以下是工作代码:

    public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int ReferenceRow, int ReferenceColumn)
    {
        int Left, Top, Right, Bottom;
        if(StartCellRow > EndCellRow || StartCellColumn > EndCellColumn) { throw new System.Exception("EROR: Please check Start- and Endcell reference or flip it!"); }

        Left = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Left;
        Top = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Top;
        Right = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Left + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Width);
        Bottom = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Top + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Height);

        Ex.Shape SB = ExSH.Shapes.AddFormControl(Ex.XlFormControl.xlScrollBar, Left, Top, Right - Left, Bottom - Top);
        SB.ControlFormat.Value = CurrentValue;
        SB.ControlFormat.Min = MinValue;
        SB.ControlFormat.Max = MaxValue;
        SB.ControlFormat.LinkedCell = "A" + ReferenceRow.ToString();
    }

啊,我找到了。某人是个前妻。然后,您可以通过SB.ControlFormat.Min、Max、Value等访问参数

以下是工作代码:

    public static void AddScrollBar(Ex.Worksheet ExSH, int StartCellRow, int StartCellColumn, int EndCellRow, int EndCellColumn, int ReferenceRow, int ReferenceColumn)
    {
        int Left, Top, Right, Bottom;
        if(StartCellRow > EndCellRow || StartCellColumn > EndCellColumn) { throw new System.Exception("EROR: Please check Start- and Endcell reference or flip it!"); }

        Left = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Left;
        Top = (int)(ExSH.Cells[StartCellRow, StartCellColumn] as Ex.Range).Top;
        Right = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Left + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Width);
        Bottom = (int)((ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Top + (ExSH.Cells[EndCellRow, EndCellColumn] as Ex.Range).Height);

        Ex.Shape SB = ExSH.Shapes.AddFormControl(Ex.XlFormControl.xlScrollBar, Left, Top, Right - Left, Bottom - Top);
        SB.ControlFormat.Value = CurrentValue;
        SB.ControlFormat.Min = MinValue;
        SB.ControlFormat.Max = MaxValue;
        SB.ControlFormat.LinkedCell = "A" + ReferenceRow.ToString();
    }