C# 车位系统,通过车位状态的改变来改变相应button颜色,点击按钮改变button颜色,改变车位状态
C# SQL
在做一个车位系统
通过点击button改变button的颜色 ,红色表示"已占用",白色表示"未占用"
同时改变数据库车位状态.
下面是我的代码,是C01(button48表示C01车位)的代码,其他button都是类似的,
但是出现了所有按钮只对C01有效.比如说我按C01,C01改变为红色,我再按C02的时候,因为C01是红色,于是接下来应该变为白色,于是C02的颜色不改变.而且C01状态已经改为已占用,但是点击C02却会改变C01的状态.而C02 的状态不会改变.
还有一个问题是我每次调试他都是白色的,不管我前一次调试是已占用还是未占用.
是哪里有问题么?还是我的思路有问题?
我还有个思路是,先根据表里的状态改变button的颜色,然后在写一段代码改变状态.但是我不知道怎么写?
public static string textbox = string.Empty;
private void button48_Click(object sender, EventArgs e)
{
textbox = this.button48.Text;
//MessageBoxShow( this.button48.Text);
if (isTrue)
{
button48.BackColor = Color.Red;
INCAR();
}
else
{
button48.BackColor = SystemColors.Control;
OUTCAR();
}
isTrue = !isTrue;
}
private void INCAR()
{
DBHelper db = null;
try
{
db = new DBHelper();
string sql = "";
{
sql = string.Format("update PARK set text ='{0}' where PID = '{1}'", "已占用",textbox );
}
if (db.UpdateDeleteAdd(sql) > 0)
{
MessageBox.Show("操作成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("操作失败!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
db.GetClose();
}
}
private void OUTCAR()
{
DBHelper db = null;
try
{
db = new DBHelper();
string sql = "";
{
sql = string.Format("update PARK set text ='{0}' where PID = '{1}'", "未占用", textbox);
}
if (db.UpdateDeleteAdd(sql) > 0)
{
MessageBox.Show("操作成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("操作失败!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
db.GetClose();
}
}