问题
I am working on an addin for excel 2010 using C#. I have an existing worksheet that has some controls in it, namely a ComboBox. I am trying to write some code that will place a certain value in the combo box's text property, but I am having a hard time getting access to the control to do so.
The combo box is named 'ComboBox1' but if I try something like...
var combo = Controls["ComboBox1"];
I get an ArgumentOutOfRangeException.
Exploratory approaches to finding out what I am supposed to be doing aren't really providing useful information either. For example, if were to write;
MessageBox.Show(Controls[0].GetType())
The displayed message is 'NamedRangeImpl' which doesn't seem like a control at all. So my question is, how do I get access to the controls that are on my worksheet from my code?
回答1:
I'm not exactly sure about the problem but I've made addins for word and if its like windows forms this should work nicely.
foreach (Control c in Controls)
if (c.Name == "comboBox1") {
ComboBox box = (ComboBox)c;
box.Items.Add("Thing added");
}
来源:https://stackoverflow.com/questions/7152020/c-sharp-excel-addin-accessing-controls