通过调用(C#)访问组合框项
我正在调用表单中usercontrol的方法,但是usercontrol中的Combobox在调用的方法中不包含任何项。当我在不使用invoke的情况下运行相同的方法时(即,在usercontrol中直接放置一个按钮并运行相同的方法),组合框中会有相应的项。代码如下 格式1中的代码:通过调用(C#)访问组合框项,c#,winforms,combobox,C#,Winforms,Combobox,我正在调用表单中usercontrol的方法,但是usercontrol中的Combobox在调用的方法中不包含任何项。当我在不使用invoke的情况下运行相同的方法时(即,在usercontrol中直接放置一个按钮并运行相同的方法),组合框中会有相应的项。代码如下 格式1中的代码: Type type = ActiveModelControl.GetType(); var control = Activator.CreateInstance(type);
Type type = ActiveModelControl.GetType();
var control = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("RunSimulation");
object[] param = new object[] { modelinfo };
var results = method.Invoke(control, param);
usersontrol中的代码:
public List<double> RunSimulation(IModel model)
{
[other code]
List<string> tempSecurityList =
GetSecurityList(SecurityListComboBox.SelectedItem.ToString());
[other code]
}
公共列表运行模拟(IModel模型)
{
[其他守则]
列表临时安全列表=
GetSecurityList(SecurityListComboBox.SelectedItem.ToString());
[其他守则]
}
在新的Unshawn usercontrol上调用该方法。为什么您希望在Unshawn Usercontrol上选择某些内容。我不知道为什么我没有意识到我做错了什么,但这是一个简单的修复方法。感谢您指出这一点。使用Activator.CreateInstance()是完全错误的。您创建了一个全新的ComboBox对象,当然它是空的。你必须找出用户正在看的那个。