C# excel addin - Accessing Controls

℡╲_俬逩灬. 提交于 2019-12-11 04:01:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!