C # excel addin - Access controls

I am working on an add-on for excel 2010 using C #. I have a worksheet that has some controls, namely a ComboBox. I am trying to write code that will put a specific value in a property of a text field, but it is difficult for me to access the control to do this.

The combo box is called "ComboBox1", but if I try something like ...

var combo = Controls["ComboBox1"]; 

I get an ArgumentOutOfRangeException exception.

Research approaches to figuring out what I should do also do not provide useful information. For example, if you were to write:

 MessageBox.Show(Controls[0].GetType()) 

The displayed message is "NamedRangeImpl", which does not look like a control at all. So my question is: how do I access the controls that are on my worksheet from my code?

+2
source share
1 answer

I'm not quite sure about the problem, but I made additions to the word, and if its window shapes should work well.

 foreach (Control c in Controls) if (c.Name == "comboBox1") { ComboBox box = (ComboBox)c; box.Items.Add("Thing added"); } 
+1
source

Source: https://habr.com/ru/post/1432367/


All Articles