我使用此代码,但它不起作用:
private void checkcheck()
{
if (checkBox1.Text == "ON")
{
checkBox1.Checked = true;
checkBox1.ForeColor = Color.DeepSkyBlue;
}
else
{
checkBox1.Checked = false;
checkBox1.ForeColor = Color.Silver;
}
}
我从表单加载事件中调用此方法。
private void Form3_Load(object sender, EventArgs e)
{
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM contacts WHERE id = '" + label2.Text + "' ", cn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox1.Text = reader[1].ToString();
textBox2.Text = reader[2].ToString();
textBox3.Text = reader[3].ToString();
textBox4.Text = reader[4].ToString();
checkBox1.Text = reader[5].ToString();
}
}
cn.Close();
checkcheck();
}
就是这样,表单加载,然后复选框根据数据更改,然后它会更改,当我运行它时它会完美获取数据,并将复选框更改为开/关但没有颜色或检查