简体   繁体   English

如何让用户在 c# vsto excel 加载项中选择单元格(范围输入)?

[英]How to get user to select cells(Range input) in c# vsto excel add-in?

I need is to give the user control to select some cells which'll be used in the program.我需要的是让用户控制选择一些将在程序中使用的单元格。 The interface is easily seen in many of the excel functions like while inserting data to the graphs.该界面在许多 excel 功能中很容易看到,例如在向图形中插入数据时。

Pre-selected range can be easily accessed as Globals.ThisAddIn.Application.ActiveWindow.RangeSelection but this is not what I am looking into as I need multiple such ranges.预先选择的范围可以作为Globals.ThisAddIn.Application.ActiveWindow.RangeSelection轻松访问,但这不是我正在研究的,因为我需要多个这样的范围。 I am trying this on visual-studio, There is Interaction.InputBox function to take string in , which is said to be equivalent of Inputbox of vba, but inputbox of vba had parameter for input data type, which c# inputbox doesn't.我正在visual-studio上尝试这个,有Interaction.InputBox函数可以接收字符串,据说相当于vba的Inputbox ,但是vba的inputbox有输入数据类型的参数,而c#inputbox没有。

The VBA equivalent for this is与此等效的 VBA 是

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

the Type:=8 will make the inputbox to take range input. Type:=8 将使输入框接受范围输入。 but i need to do it in c# (using visual studio) and the Inputbox method of c# doesn't have input type like VBA.但我需要在 c# 中执行(使用 Visual Studio)并且 c# 的 Inputbox 方法没有像 VBA 这样的输入类型。

Range myRange;

myRange = Globals.ThisAddIn.Application.InputBox("Please select your range?", "Range Selector", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);

I tried a lot but couldn't find the exactly what I was looking for, but this workaround works for now.我尝试了很多但找不到我正在寻找的东西,但是这个解决方法现在有效。 I made the form modeless and then put whatever operation I needed to do with the range inside that form, so I couldn't actually send the range back from the form, but I could at least perform the task.我使表单无模式,然后在该表单中放置我需要对范围执行的任何操作,因此我实际上无法从表单发送范围,但我至少可以执行任务。

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

I still hope someone will give the way to have it work like the original VBA inputbox.我仍然希望有人能让它像原来的 VBA 输入框一样工作。

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

相关问题 C# Excel 的 VSTO 加载项 - Select 图表 object 按名称? - C# VSTO add-in for Excel - Select Chart object by name? 如何在 Excel VSTO 加载项中为 OnUndo 调用 C# 方法? - How to call a C# method for OnUndo in an Excel VSTO add-in? 当电子表格 window 最小化时,如何获取我的 Excel VSTO 插件以更新单元格? - How can I get my Excel VSTO Add-in to update cells when the spreadsheet window is minimized? 如何使用VSTO 2005 SE使用C#Excel加载项创建Excel 2003 UDF - How to create Excel 2003 UDF with a C# Excel add-in using VSTO 2005 SE C# VSTO 插件 - Excel 终止 HTTPS 连接 - C# VSTO Add-In - Excel Terminates HTTPS connection VSTO C# Excel 加载项未显示名称 - VSTO C# Excel add-in is not showing the name 如何在VSTO C#Excel 2013加载项中将Publisher从无更新为XYZ? - How to update Publisher from None to XYZ in VSTO C# Excel 2013 Add-in? 使用C#/ VSTO监视excel 2007内部的一系列单元格 - monitoring a range of cells inside of excel 2007 with C#/VSTO C#在VSTO加载项中获取正在运行的Outlook实例 - C# Get running Outlook instance in VSTO add-in 如何在Excel中仅读取具有特定范围内的值的单元格-使用VSTO C# - How to only read cells with values with in a specific range in Excel - using VSTO C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM