【问题标题】:How to get user to select cells(Range input) in c# vsto excel add-in?如何让用户在 c# vsto excel 插件中选择单元格(范围输入)?
【发布时间】:2019-12-15 21:21:11
【问题描述】:

我需要让用户控制选择一些将在程序中使用的单元格。该界面在许多 excel 函数中很容易看到,例如在向图表中插入数据时。

可以通过Globals.ThisAddIn.Application.ActiveWindow.RangeSelection 轻松访问预选范围,但这不是我正在研究的,因为我需要多个这样的范围。 我在visual-studio上尝试这个,有Interaction.InputBox函数来接受字符串,据说相当于vba的Inputbox,但是vba的输入框有输入数据类型的参数,而c#输入框没有.

VBA 等效于

Dim rngX as Range
Set rngX = Application.InputBox("Select a cell or a range", "Input Cells", Type:=8)

Type:=8 将使输入框接受范围输入。 但我需要在 c#(使用 Visual Studio)中完成,并且 c# 的 Inputbox 方法没有 VBA 之类的输入类型。

【问题讨论】:

    标签: c# excel vba vsto excel-addins


    【解决方案1】:
    Range myRange;
    
    myRange = Globals.ThisAddIn.Application.InputBox("Please select your range?", "Range Selector", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    • 感谢它的作用就像一个魅力,但正如前面的评论所说,您需要多解释一点,以便其他人可以从中受益。
    • 感谢您抽出宝贵时间回答。与其他评论员不同,我认为这不需要更多解释——这是做什么的很明显!有时,一个简短、中肯的答案正是我们所需要的。
    【解决方案2】:

    我尝试了很多,但找不到我正在寻找的确切内容,但此解决方法目前有效。 我将表单设置为无模式,然后在表单中对范围进行任何操作,因此我实际上无法从表单中将范围发送回,但我至少可以执行任务。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;
    using Microsoft.VisualBasic;
    
    namespace ExcelAddInZeroSofts
    {
        public partial class InterpolationForm : Form
        {
            Worksheet ws;
            public InterpolationForm(Range rng)
            {
                InitializeComponent();
                tbRange.Text = rng.Address;
            }
            private void SelectRangeForm_Load(object sender, EventArgs e)
            {
                ws = Globals.ThisAddIn.Application.ActiveSheet;
                ws.SelectionChange += ws_SelectionChange;
                // This one will add the event handling to worksheet.
            }
            void ws_SelectionChange(Range Target)
            {
                if (this.ActiveControl.Name.First() == 't')
                {
                    this.ActiveControl.Text = Target.Address;
                    // This one will add the address to the userform (I have 3 fields).
                }
            }
    
            private void SelectRangeForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                ws.SelectionChange -= ws_SelectionChange;
            }
    
            private void bSubmit_Click(object sender, EventArgs e)
            {
                // My main Logic Goes here
                this.Close();
            }
    
            private void bCancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    

    我仍然希望有人能够让其像原来的 VBA 输入框一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多