我需要让用户控制选择一些将在程序中使用的单元格。可以在许多excel函数中轻松看到该界面,例如在将数据插入图形时。
可以容易地以Globals.ThisAddIn.Application.ActiveWindow.RangeSelection
的形式访问预选范围,但这不是我要研究的范围,因为我需要多个这样的范围。
我在visual-studio上尝试此操作,其中有Interaction.InputBox
个函数来接收字符串,据说它等效于vba的Inputbox
,但是vba的inputbox具有用于输入数据类型的参数,其中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这样的输入类型。
答案 0 :(得分:1)
if(type === 'exp'){
newItem = new Expense(type, ID, des, val);
}
else if(type === 'inc') {
newItem = new Income(type, ID, des, val);
}
答案 1 :(得分:0)
我做了很多尝试,但是找不到我想要的东西,但是这种解决方法目前可以使用。 我将表单设为无模式,然后对表单中的范围进行了所需的任何操作,因此我实际上无法将表单中的范围发送回去,但我至少可以执行任务。
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输入框一样工作。