C# - 在运行时选择多个按钮,并立即将值保存到数据库

时间:2017-09-13 01:11:34

标签: c# sql sql-server winforms

我正在开发航空座位图应用程序。但我无法一次更改多个按钮值。

我搜索了我的解决方案,但找不到我想要的。

我的序列是

  1. 选择我想要封锁的座位
  2. 选中时突出显示按钮
  3. 我将添加" Save"按钮。此按钮将所有选定的按钮值保存到数据库。
  4. 那是
  5. 这是我的动态按钮代码:

         private void GenerateSeats()
        {
            const int seatSpacing = 6;
            const int middleRowWidth = 50;
            const int seatWidth = 40;
            const int seatHeight = 30;
            char[] Seatletter = { 'A', 'B', 'C', 'D', 'E', 'F' };
            //panel1.Width = 6 * (seatHeight + seatSpacing) + seatSpacing + middleRowWidth;
            //panel1.Width = 1200;
    
            var buttonSize = new Size(seatWidth, seatHeight);
    
            for (int i = 0; i <= 155; i++)
            {
                int SeatRow = i / 6;
                for (int j = 0; j <= 5; j++)
                {
                    int thisRow = i / 6;
                    int thisColumn = j % 6;
    
                    int seatTop = thisRow * (seatHeight + seatSpacing);
                    int seatLeft = thisColumn * (seatWidth + seatSpacing);
    
                    string SeatCode = (SeatRow + 1).ToString() + Seatletter[j];
    
                    if (thisColumn >= 3) seatLeft += middleRowWidth;
    
                    con.Open();
                    cmd = new SqlCommand("select * from Passengers where seat = @checkseat", con);
                    cmd.Parameters.AddWithValue("@checkseat", SeatCode);
                    SqlDataReader Reader = cmd.ExecuteReader();
    
                    Button SButton = new Button();
                    SButton.Click += new EventHandler(SButton_Click);
                    SButton.Size = buttonSize;
                    SButton.BackgroundImageLayout = ImageLayout.Stretch;
                    SButton.TextAlign = ContentAlignment.MiddleRight;
                    SButton.Text = SeatCode;
                    SButton.Location = new Point(seatTop, seatLeft);
                    SeatMapPanel.Controls.Add(SButton);
    
                    if (Reader.Read()) //Check seats (available or not)
                        {
                            SButton.BackgroundImage = Properties.Resources.CXV_blk_j;      //already reserved seats                      
                        }
                    else 
                        {
                            SButton.BackgroundImage = Properties.Resources.CXV_def_j;    //Available seats
                        }
                    con.Close();
                }
            }
        }
    

    结果:

    I've created these buttons dynamically

1 个答案:

答案 0 :(得分:1)

This way you can arrange your code and logic
  1. 获取所有可用座位

  2. 获取所有指定席位

  3. 获取所有不可用座位

  4. 获取所有被阻挡的席位

  5. 上述4个功能将为您提供有关例如座位号的信息,可以是

    例如

     List<int> GetAllTheAvailableSeats() //You can return anything which is suitable
       {
           //your logic of database
       } 
    

    同样,您可以编写其他方法。

    现在,一旦每个座位状态的数据可用,您就可以生成座椅面板。在这里,您不应该在代码中添加修改按钮。省略除生成之外的所有代码,例如

     private void GenerateSeats()
    {
        const int seatSpacing = 6;
        const int middleRowWidth = 50;
        const int seatWidth = 40;
        const int seatHeight = 30;
        char[] Seatletter = { 'A', 'B', 'C', 'D', 'E', 'F' };
        //panel1.Width = 6 * (seatHeight + seatSpacing) + seatSpacing + middleRowWidth;
        //panel1.Width = 1200;
    
        var buttonSize = new Size(seatWidth, seatHeight);
    
        for (int i = 0; i <= 155; i++)
        {
            int SeatRow = i / 6;
            for (int j = 0; j <= 5; j++)
            {
                int thisRow = i / 6;
                int thisColumn = j % 6;
    
                int seatTop = thisRow * (seatHeight + seatSpacing);
                int seatLeft = thisColumn * (seatWidth + seatSpacing);
    
                string SeatCode = (SeatRow + 1).ToString() + Seatletter[j];
    
                if (thisColumn >= 3) seatLeft += middleRowWidth;
    
                Button SButton = new Button();
                SButton.Click += new EventHandler(SButton_Click);
                SButton.Size = buttonSize;
                SButton.BackgroundImageLayout = ImageLayout.Stretch;
                SButton.TextAlign = ContentAlignment.MiddleRight;
                SButton.Text = SeatCode;
                SButton.Location = new Point(seatTop, seatLeft);
                SeatMapPanel.Controls.Add(SButton);
    
            }
        }
    }
    

    完成座位生成后,您可以设置其状态,例如

    public void SetSeatStatuses() 
    {
       foreach(var seatinfo in AvailableSeats) //available seat  can be int or anything
        {
          var button =  SeatMapPanel.Controls[seatInfo] as Button;
          //set background images here
        }
    }
    

    类似地,对于其他状态。

    注意: - 根据您的需要进行优化