vb.net 复选框无效转换异常

vb.net checkbox invalid cast exception

我不想承认,但我正在构建一个 vb.net winform,它需要使用读取文本字段、复选框或单选按钮来构建 sql 查询和 return 结果。 sql 查询部分工作得非常好,但是 select case 语句似乎没有正确评估复选框或单选检查状态。

Select Case True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text)
    dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtFirstName.Text.Length > 0 & chkInactive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(txtFirstName.Text, , , "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text)
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkActive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtLastName.Text.Length > 0 & chkInactive.CheckState
    dgvEmployees.DataSource = employess.FindEmployee(, txtLastName.Text, , "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & rdbActive.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text)
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkActive.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Active")
    chkInactive.Enabled = False
    dgvEmployees.Visible = True
Case txtEmpCode.Text.Length > 0 & chkInactive.CheckState = CheckState.Checked
    dgvEmployees.DataSource = employess.FindEmployee(, , txtEmpCode.Text, "Inactive")
    chkActive.Enabled = False
    dgvEmployees.Visible = True
Case Else
    MessageBox.Show("please enter an employee code, firstname or lastname")
End Select

每次调试时,我都会收到单选按钮或复选框的无效转换异常,我是否遗漏了什么?

在 Visual Basic 中,& performs a string concatenation. You want to use And or AndAlso,如 Plutonix 所述。

AndAlso 将是更好的选择,因为如果第一个条件失败,它会跳过评估第二个条件。例如,如果 txtFirstName.Text 的长度为 0,则程序不会检查 chkActive.CheckState 的状态。实际上,正因为如此,理论上先检查 chkActive.CheckState 可能会更有效 tiny 一点,因为这可能会更快地评估。