我使用了组合框:仅限两个选项:“珠宝”,“宝石”
这是我的代码:
private void combo_main_type_SelectedIndexChanged(object sender, EventArgs e)
{
if (combo_main_type.SelectedItem == "Jewelry")
{
txt_qty.Visible = true;
label6.Visible = true;
txt_qty.Location = new System.Drawing.Point(400, 400);
label6.Location = new System.Drawing.Point(550, 500);
}
else
{
txt_qty.Visible = false;
label6.Visible = false;
}
}
这一行用红色加下划线:if(combo_main_type.SelectedItem ==“Jewelry”)
答案 0 :(得分:1)
将其与SelectedText
进行比较:
if (combo_main_type.SelectedText == "Jewelry")
SelectedText
表示组合框中当前所选文本的字符串。如果DropDownStyle设置为DropDownList,则返回值为空字符串(“”)。
答案 1 :(得分:-1)
.SelectedItem
是object
,您正在与string
进行比较。在比较两者之前,将演员表添加到string
。它甚至在警告...cast the left hand side to type 'string'
if ((string)combo_main_type.SelectedItem == "Jewelry")