[英]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.