索引更改后启用和禁用组合框
Enabled and disabled combobox after index change
我正在开发一个软件,遇到了一些小麻烦。我有两个组合框,第一个填充第二个,但如果第一个组合框的值等于 -1,则第二个组合框不启用。这是我的代码的和平:
private void FillComboGroup()
{
try
{
//here I initialize an object
if (Convert.ToInt32(this.cboSede.SelectedValue) > -1)
{
//the code here just retrieves an object
}
else
{
this.cboEmpleado.Enabled = false;
this.cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
嗯,第一次没有问题。第一次加载表单时,未启用第二个组合框,然后我更改第一个组合框值,现在启用第二个组合框。但是,如果我再次将第一个组合框放入第一个值("Please select an option...",其值为 -1),则第二个组合框将启用。我尝试使用 SelectedIndexChanged 和 SelectionChangeCommited 但不起作用。关于如何解决它的任何想法?提前致谢。
private void cboSede_SelectedIndexChanged(object sender, EventArgs e)
{
this.FillComboGroup();
}
您在检查索引时使用 SelectedValue
而不是 SelectedIndex。
它应该是这样的:
private void FillComboGroup()
{
try
{
if (cboSede.SelectedIndex > 0)
{
//the code here just retrieves an object
// YOU'LL PROBABLY WANT TO ENABLE THE SECOND COMBO AGAIN HERE
}
else
{
cboEmpleado.Enabled = false;
cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
}
你的问题在于组合框 id 中的第一个对象是 0 而不是 -1,-1 是未选择的值你应该尝试这样的事情:
private void FillComboGroup()
{
try
{
if (cboSede.SelectedIndex > 0)
{
//the code here just retrieves an object
}
else
{
cboEmpleado.Enabled = false;
cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
}
我正在开发一个软件,遇到了一些小麻烦。我有两个组合框,第一个填充第二个,但如果第一个组合框的值等于 -1,则第二个组合框不启用。这是我的代码的和平:
private void FillComboGroup()
{
try
{
//here I initialize an object
if (Convert.ToInt32(this.cboSede.SelectedValue) > -1)
{
//the code here just retrieves an object
}
else
{
this.cboEmpleado.Enabled = false;
this.cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
嗯,第一次没有问题。第一次加载表单时,未启用第二个组合框,然后我更改第一个组合框值,现在启用第二个组合框。但是,如果我再次将第一个组合框放入第一个值("Please select an option...",其值为 -1),则第二个组合框将启用。我尝试使用 SelectedIndexChanged 和 SelectionChangeCommited 但不起作用。关于如何解决它的任何想法?提前致谢。
private void cboSede_SelectedIndexChanged(object sender, EventArgs e)
{
this.FillComboGroup();
}
您在检查索引时使用 SelectedValue
而不是 SelectedIndex。
它应该是这样的:
private void FillComboGroup()
{
try
{
if (cboSede.SelectedIndex > 0)
{
//the code here just retrieves an object
// YOU'LL PROBABLY WANT TO ENABLE THE SECOND COMBO AGAIN HERE
}
else
{
cboEmpleado.Enabled = false;
cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
}
你的问题在于组合框 id 中的第一个对象是 0 而不是 -1,-1 是未选择的值你应该尝试这样的事情:
private void FillComboGroup()
{
try
{
if (cboSede.SelectedIndex > 0)
{
//the code here just retrieves an object
}
else
{
cboEmpleado.Enabled = false;
cboEmpleado.BackColor = Color.White;
}
}
catch (Exception ex)
{
//here are some methods to save the exception in the log
}
}