C# c语言中的组合框反射#
我有以下代码:C# c语言中的组合框反射#,c#,combobox,C#,Combobox,我有以下代码: typeof(ComboBox).GetProperty("SelectedItem").SetValue("comboBox1", "", null); 它不起作用。以下是错误: “对象与目标类型不匹配” 怎么了 更新: 我有我的对象的名称(字符串形式的组合框。例如: string []s = new s[3]; s[0]="comboBox0"; s[1]="comboBox1"; s[2]="comboBox2"; 我想为属性SelectedItem设置一个值到我
typeof(ComboBox).GetProperty("SelectedItem").SetValue("comboBox1", "", null);
它不起作用。以下是错误:
“对象与目标类型不匹配”
怎么了
更新:
我有我的对象的名称(字符串形式的组合框。例如:
string []s = new s[3];
s[0]="comboBox0";
s[1]="comboBox1";
s[2]="comboBox2";
我想为属性SelectedItem设置一个值到我的comboBoxs[I]
。我怎么做?需要有comboBox
对象,而不是字符串“comboBox1”。我相信你的应用程序中有ComboxBox对象,假设:
ComboBox comboBox1 = ....;
所以你可以打电话:
typeof(ComboBox).GetProperty("SelectedItem")
.SetValue(comboBox1, "", null);
如果使用.NET 4.5,则可以删除最后一个输入:
typeof(ComboBox).GetProperty("SelectedItem")
.SetValue(comboBox1, "");
编辑:根据您的评论,您不需要使用反射,以下是您只需知道名称即可获得combox1:
var comboBox1 = Controls.OfType<ComboBox>()
.SingleOrDefault(c => c.Name == "Combobox1");
comboBox1.SelectedItem = "";
var comboBox1=Controls.OfType()
.SingleOrDefault(c=>c.Name==“Combobox1”);
comboBox1.SelectedItem=“”;
需要有ComboBox
对象,而不是字符串“comboBox1”。我相信您的应用程序中有一个ComboxBox对象,假设:
ComboBox comboBox1 = ....;
所以你可以打电话:
typeof(ComboBox).GetProperty("SelectedItem")
.SetValue(comboBox1, "", null);
如果使用.NET 4.5,则可以删除最后一个输入:
typeof(ComboBox).GetProperty("SelectedItem")
.SetValue(comboBox1, "");
编辑:根据您的评论,您不需要使用反射,以下是您只需知道名称即可获得combox1:
var comboBox1 = Controls.OfType<ComboBox>()
.SingleOrDefault(c => c.Name == "Combobox1");
comboBox1.SelectedItem = "";
var comboBox1=Controls.OfType()
.SingleOrDefault(c=>c.Name==“Combobox1”);
comboBox1.SelectedItem=“”;
发送到PropertyInfo.SetValue
的第一个参数是要设置值的对象。例如,如果将ComboBox对象命名为comboBox1
,则必须从SetValue(“comboBox1”,null);
更改为SetValue(comboBox1,null);
您可以阅读更多信息。发送到
PropertyInfo.SetValue
的第一个参数是要设置值的对象。例如,如果您将ComboBox对象命名为comboBox1
,则必须从SetValue(“comboBox1”,null);
更改为SetValue(Combox1,null);
您可以阅读更多。方法需要3个参数,第一个参数是实例对象,第二个是值,第三个是索引属性的索引,如果不使用,则设置为null 所以
var-box=;
typeof(ComboBox).GetProperty(“SelectedItem”).SetValue(box,”,null);
方法需要3个参数,第一个参数是实例对象,第二个是值,第三个是索引属性的索引,如果不使用,则设置为null
所以
var-box=;
typeof(ComboBox).GetProperty(“SelectedItem”).SetValue(box,”,null);
在组合框所在的表单中,添加以下方法:
public void SetComboBoxValue(string comboBoxName, string value)
{
ComboBox foundCombo = Controls.OfType<ComboBox>().SingleOrDefault(c => c.Name == comboBoxName) as ComboBox;
if (foundCombo == null)
return;
foundCombo.SelectedItem = value;
}
public void SetComboBoxValue(字符串comboBoxName,字符串值)
{
ComboBox foundCombo=Controls.OfType().SingleOrDefault(c=>c.Name==comboBoxName)作为ComboBox;
if(foundCombo==null)
回来
foundCombo.SelectedItem=值;
}
在组合框所在的表单中,添加以下方法:
public void SetComboBoxValue(string comboBoxName, string value)
{
ComboBox foundCombo = Controls.OfType<ComboBox>().SingleOrDefault(c => c.Name == comboBoxName) as ComboBox;
if (foundCombo == null)
return;
foundCombo.SelectedItem = value;
}
public void SetComboBoxValue(字符串comboBoxName,字符串值)
{
ComboBox foundCombo=Controls.OfType().SingleOrDefault(c=>c.Name==comboBoxName)作为ComboBox;
if(foundCombo==null)
回来
foundCombo.SelectedItem=值;
}
@Chanipoz:不明白,你能解释更多吗?我有一个组合框名称的字符串,如:s=“comboBox1”,我如何设置它的值?@Chanipoz:不明白,你能解释更多吗?我有一个组合框名称的字符串,如:s=“Combox1”,我如何设置它的值?