Forum Discussion
Combo box programming with c#
Did you add the Microsoft.Office.Interop.Excel library to your project references?
- Pavel_Office935May 22, 2020Copper Contributor
Yes, at the beginning of the program is:
using Microsoft.Office.Interop.Excel;
using System.IO;
The last command executed successfully is:
var _contrl = wShtFrm.Shapes.Range["Drop Down 1"];
Command
_contrl.ListFillRange = $"List!$C$2:$C${iLast}";
throws this error message:
(System._ComObject does not contain a definition for ListFillRange)
The recorded Excel macro is:
ActiveSheet.Shapes.Range(Array("Drop Down 1")).Select
With Selection
.ListFillRange = "List!$C$2:$C$5"
.LinkedCell = "C4"
.DropDownLines = 8
.Display3DShading = False
End With
- erol sinan zorluMay 22, 2020Iron Contributor
when I check the both codes given, in VBA you "select" the shape and use FillListRange method of selection object. In the C# version you create a var _cntrl and try to call this objects FillListRange method which simply doesn't exists as the variant object is a "shape" object.
In fact when I try to assign the shape object to a VBA Shape variable, the FillListRange method doesn't exists also.
The easiest way is to do the same in C# and select the shape and use FillListRange method of the selection. However it is better to use the "ActiveX ComboBox" item, not the standard combobox, as this item has an object property which has a ListFillRange method.
- Pavel_Office935May 22, 2020Copper Contributor
Thank you, it's already working!
So the correct translation from VBA to C# is:
ShtFrm.Shapes.Range["Drop Down 1"].Select();
exApl.Selection.ListFillRange = $"List!$C$2:$C${iLast }";
exApl.Selection.LinkedCell = "C4";
exApl.Selection.DropDownLines = 4;
exApl.Selection.Display3DShading = false;
It is actually simple.
I thought that when the application is running as Visible = false ,I should not use the command Select()
Thank you again!