Josué Carvajal
Josué Carvajal

Reputation: 112

How to update dynamically a textBox with the range when user makes a selection in Excel. C#

I'm working on add-in in C# using VSTO, Actually I want let the user select the range using the mouse, and while hes doing this, a text box is updating constantly with the range, to show the current selection. How can I do this?

Now, I can get the selected range, but only if they press a button and this update the text box, but I dont want this.

By the way, if someone knows how to make an input like excels does in "Solver" to select the range, it will work too.

I want to do something like this, without click buttons, just select and it updates the textfield This is how solver works when you want to select a range of cells

Upvotes: 0

Views: 111

Answers (1)

Sourabh Mishra
Sourabh Mishra

Reputation: 222

You can hook up a WorkbookBase.SheetSelectionChange Event, and in that you can query the range object to tell the range user has selected.

Sample Code:

private void ExcelAppEvents_SheetSelectionChange(object sender, object sheet, object range)
{
     Excel.Range xlRange = null;
     try
     {
          xlRange.MergeCells;
          var retVal = xlRange.get_Address(false, false, Excel.XlReferenceStyle.xlA1, true);
          // Here retvalue may contain your sheetname and cell range, for example [sheet.xlsx]Sheet1!D9:G13, you need to remove these extra characters using some regex.
          // TextBox.Text = retVal;


     }

}

Upvotes: 1

Related Questions