C# 车位系统,通过车位状态的改变来改变相应button颜色,点击按钮改变button颜色,改变车位状态

shel11 2016-12-18 08:06:12
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();
}
}
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooolinux 2016-12-18
  • 打赏
  • 举报
回复
参考: http://bbs.csdn.net/topics/300208662
ooolinux 2016-12-18
  • 打赏
  • 举报
回复
你是不是所有Button的OnClick都指定为button48_Click这个函数? 虽然函数都是相同的,但是可以利用sender参数来区别点击的不同的button, 把sender转换为button类型,然后改变这个button的颜色。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧