酒店管理系统根据数据库动态加载房间生成房态图的制作
先放一张效果图:
因为要动态加载房态图,需要遍历每一个小房间,所以我定义了一个自定义控件,大概是这个样子的,
首先创建一个自定义控件,然后我们来绘制一个panel容器作为我们的一个小房间,上面的label作为房间号的显示,下面的label作为房间状态的显示,中间的图片对应的是状态的图标喽。
附上自定义控件的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace AegeanHotel_management_system
{public partial class Userstate : UserControl{private int _roomNum;private List<User> _inUsers;private string _status;private string _remark;public Userstate(){InitializeComponent();}private void pnl_Back_Paint(object sender, PaintEventArgs e){//RoundPanel myPanel = new Panel();//边框线//ControlPaint.DrawBorder(e.Graphics,// pnl_Back.ClientRectangle,// Color.Blue,// 3,// ButtonBorderStyle.Solid,// Color.Red,// 3,// ButtonBorderStyle.Solid,// Color.Red,// 3,// ButtonBorderStyle.Solid,// Color.Red,// 3,// ButtonBorderStyle.Solid);}/// <summary>/// 房间号/// </summary>public int RoomNum{get { return _roomNum; }set{_roomNum = value;UpdateRoomNum();}}//房间号方法private void UpdateRoomNum(){if (_roomNum == 0)return;lbl_RoomNum.Text = _roomNum.ToString();var width = pnl_Back.Width - lbl_RoomNum.Width;if (width < 0)width = 0;lbl_RoomNum.Location = new Point(width / 2, lbl_RoomNum.Location.Y);}/// <summary>/// 入住人/// </summary>public List<User> InUsers{get { return _inUsers; }set{_inUsers = value;UpdateInUsers();}}//入住人方法private void UpdateInUsers(){if (_inUsers == null){lbl_RoomUsers.Text = _status.ToString();return;}lbl_RoomUsers.Text = string.Join(",", _inUsers.Select(p => p.UserName).ToList());var width = pnl_Back.Width - lbl_RoomUsers.Width;if (width < 0)width = 0;lbl_RoomUsers.Location = new Point(width / 2, lbl_RoomUsers.Location.Y);}/// <summary>/// 房间状态/// </summary>public string Status{get { return _status; }set{_status = value;UpdateStatus();}}//房间状态方法private void UpdateStatus(){switch (_status){case "空房已清洁":pnl_Back.BackColor = Color.FromArgb(135, 206, 250);pictureBox1.Image = Image.FromFile(@"F:\小图标\房子2.ico", true);break;case "已入住":pnl_Back.BackColor = Color.FromArgb(70, 130, 170);pictureBox1.Image = Image.FromFile(@"F:\小图标\用户4.png", true);break;case "维修房":pnl_Back.BackColor = Color.FromArgb(205, 92, 92);pictureBox1.Image = Image.FromFile(@"F:\小图标\维修1.png", true);break;case "空房未清洁":pnl_Back.BackColor = Color.FromArgb(205, 92, 92);pictureBox1.Image = Image.FromFile(@"F:\小图标\打扫2.png", true);break;}}/// <summary>/// 房间备注/// </summary>public string Remark { get; set; }#region 初始化#endregionpublic class User{public string UserName { get; set; }}}
}
到此为止我们的自定义控件就画好了,然后我们将他根据数据库展示出来就好了。
我们依然需要在显示的界面上绘制一个flowLayoutPanel容器,准备放入我们的自定义控件,也就是每一个小房间,
然后我们直接遍历显示就好啦
//房态图动态加载
string sql = "select RoomID from Room";
DataTable dt = DBHelper.GetDataTable(sql);
int RoomID = 0;
string RoomState = "";
string Sanname = "";
foreach (DataRow row in dt.Rows)
{RoomID = Convert.ToInt32(row["RoomID"]);string sql1 = string.Format("select RoomState from Room where RoomID={0}", RoomID);DataTable dt1 = DBHelper.GetDataTable(sql1);RoomState = dt1.Rows[0][0].ToString();string sql2 = "select Name from RoomState where Roomhao=" + RoomID;DataTable dt2 = DBHelper.GetDataTable(sql2);if (dt2.Rows.Count > 0){Sanname = dt2.Rows[0][0].ToString();}else{Sanname = RoomState;}Userstate roomInfo = new Userstate(){RoomNum = RoomID,Status = RoomState,InUsers = RoomID % 7 == 0 ? null : new List<Userstate.User>(){new Userstate.User(){ UserName = Sanname }}};roomInfo.Name ="LBL"+RoomID;flowLayoutPanel1.Controls.Add(roomInfo);
}
到此为止我们房间的动态图就加载完成了。快去看看效果吧!
感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹