简体   繁体   中英

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.

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. 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.

The VBA equivalent for this is

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. but i need to do it in c# (using visual studio) and the Inputbox method of c# doesn't have input type like 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM