我无法禁用复选框?

时间:2016-02-06 20:23:04

标签: c# checkbox

我无法禁用复选框...

public  void seat_reser_Load(object sender, EventArgs e)
{
    string asd = "";

    DataTable dt = ob.dataview("Select * from seat_res where bus_id='"+ bus_select.Id+"'and date='"+bus_select.date +"'");

    foreach (DataRow d in dt.Rows)
    {
        asd += d[2].ToString();    // d[2] is seat column
        asd+=",";
    }

    box[0] = CheckBox1;
    box[1] = CheckBox2;
    box[2] = checkBox3;
    box[3] = checkBox4;
    box[4] = checkBox5;
    box[5] = checkBox6;
    box[6] = checkBox7;
    box[7] = checkBox8;
    box[8] = checkBox9;
    box[9] = checkBox10;
    box[10] = checkBox11;
    box[11] = checkBox12;
    box[12] = checkBox13;
    box[13] = checkBox14;
    box[14] = checkBox15;
    box[15] = checkBox16;
    box[16] = checkBox17;
    box[17] = checkBox18;
    box[18] = checkBox19;
    box[19] = checkBox20;
    box[20] = checkBox21;
    box[21] = checkBox22;
    box[22] = checkBox23;
    box[23] = checkBox24;
    box[24] = checkBox25;

    for (int h = 0; h < box.Length; h++)
    {
        box[h].Enabled = true;
    }

    string[] n = asd.Split(',');

    for (int i = 0; i < n.Length; i++)
    {
        for (int j = 0; j < box.Length; j++)
        {
            if (n[i] == box[j].Text)
            {
                box[j].Enabled = false;
                j++;
            }
            else if (!(box[j].Enabled == false))
            {
                box[j].Enabled = true;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码似乎很好,是否要将复选框的可见性已检查状态设置为false,而不是禁用它?你想让它变得不可见或者只是不加控制吗?

在这种情况下,您可能希望将Enabled的使用情况更改为VisibleChecked,例如,如下所示:

box[h].Visible = false;
box[h].Checked = false;

EnabledCheckedVisible之间有什么区别?

Enabled - 这将启用/禁用该复选框,如果禁用该复选框,则无法选中或取消选中该复选框,但是,该复选框仍会显示在屏幕上

Checked - 这将选中/取消选中该复选框,它与用户实际点击复选框相同

Visible - 这会使复选框显示/隐藏,这样用户就无法看到控件,也无法点击它或以任何方式与其互动