I have form where 4 buttons(Platinum, Diamond, Gold and Reset) are there, I have 3 Panel Control one for each of the first 3 buttons, what I want to do is that when I click on any one of the first three buttons the Panel for the respective buttons should appear on the screen and if any Panel is already on the screen it should not be visible. For e.g At any point of time when the application is running, consider that the Panel for the Platinum button is already on the screen and if I click on the Diamond button the Panel for the Diamond button should appear on the screen at the same place where Panel for the Platinum button was there, but the panel for the Platinum button which was already on the screen should disappear, I have a code written for this purpose, but however it is not performing as desired. Please help me find the error and also provide the solution.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace rough2
{
public partial class Form3 : Form
{
private List<Seat> list = new List<Seat>();
//private Button[] buttons = new Button[225];
//int i = 0;
string clickedButton;
private string connectionString = "Data Source=.\\SQLEXPRESS; Initial Catalog = RoughWork; Integrated Security = True;";
char[] rows = "ABCDEFGHIJKLMNO".ToCharArray();
int[] columns = Enumerable.Range(1, 15).ToArray();
int[] xy = new int[] { 0, 0 };
public Form3()
{
InitializeComponent();
}
private void btnPlatinum_Click(object sender, EventArgs e)
{
PlatinumPanel.Visible = true;
DiamondPanel.Visible = false;
GoldPanel.Visible = false;
this.PlatinumPanel.Controls.Add(this.addPlatinumSeats());
this.Controls.Add(this.PlatinumPanel);
}
private void btnDiamond_Click(object sender, EventArgs e)
{
PlatinumPanel.Visible = false;
DiamondPanel.Visible = true;
GoldPanel.Visible = false;
this.DiamondPanel.Controls.Add(this.addDiamondSeats());
this.Controls.Add(this.DiamondPanel);
}
private void btnGold_Click(object sender, EventArgs e)
{
PlatinumPanel.Visible = false;
DiamondPanel.Visible = false;
GoldPanel.Visible = true;
this.GoldPanel.Controls.Add(this.addGoldSeats());
this.Controls.Add(this.GoldPanel);
}
private Button addPlatinumSeats()
{
DataTable table = GetDataFromDataBase();
SetOccupiedSeats(table);
Button newbtn = new Button();
newbtn.Visible = false;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 15; j++)
{
// 1. find seat
var Number = String.Concat(rows[i], columns[j]);
var seat = list.Find(w => w.Number == Number);
// 2. create and position the button on form:
xy = PositioningSeat(j, i, xy[0], xy[1]);
seat.Name = String.Concat(rows[i], columns[j]);
seat.Text = String.Concat(rows[i], columns[j]);
seat.Size = new Size(36, 35);
seat.Location = new Point(xy[0], xy[1]);
seat.Click += new EventHandler(buttonSeats_Click);
seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
//seat.ForeColor = System.Drawing.Color.White;
//seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
//seat.FlatAppearance.BorderSize = 0;
//seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.Controls.Add(seat);
//newbtn = seat;
}
}
return newbtn;
}
private Button addDiamondSeats()
{
DataTable table = GetDataFromDataBase();
SetOccupiedSeats(table);
Button newbtn = new Button();
newbtn.Visible = false;
for (int i = 5; i < 10; i++)
{
for (int j = 0; j < 15; j++)
{
// 1. find seat
var Number = String.Concat(rows[i], columns[j]);
var seat = list.Find(w => w.Number == Number);
// 2. create and position the button on form:
xy = PositioningSeat(j, i, xy[0], xy[1]);
seat.Name = String.Concat(rows[i], columns[j]);
seat.Text = String.Concat(rows[i], columns[j]);
seat.Size = new Size(36, 35);
seat.Location = new Point(xy[0], xy[1]);
seat.Click += new EventHandler(buttonSeats_Click);
seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
//seat.ForeColor = System.Drawing.Color.White;
//seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
//seat.FlatAppearance.BorderSize = 0;
//seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.Controls.Add(seat);
//newbtn = seat;
}
}
return newbtn;
}
private Button addGoldSeats()
{
DataTable table = GetDataFromDataBase();
SetOccupiedSeats(table);
Button newbtn = new Button();
newbtn.Visible = false;
for (int i = 10; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
// 1. find seat
var Number = String.Concat(rows[i], columns[j]);
var seat = list.Find(w => w.Number == Number);
// 2. create and position the button on form:
xy = PositioningSeat(j, i, xy[0], xy[1]);
seat.Name = String.Concat(rows[i], columns[j]);
seat.Text = String.Concat(rows[i], columns[j]);
seat.Size = new Size(36, 35);
seat.Location = new Point(xy[0], xy[1]);
seat.Click += new EventHandler(buttonSeats_Click);
seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
//seat.ForeColor = System.Drawing.Color.White;
//seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
//seat.FlatAppearance.BorderSize = 0;
//seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.Controls.Add(seat);
//newbtn = seat;
}
}
return newbtn;
}
private void btnReset_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
try
{
cn.Open();
string ResetString = @"update SampleTable1 set IsBooked = 0 where SeatId<=225";
SqlCommand cmd = new SqlCommand(ResetString, cn);
cmd.ExecuteNonQuery();
Application.Restart();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
private int[] PositioningSeat(int column, int row, int x, int y)
{
if (column == 0 || column == 15)
{
x = 60; // starting X position or reseting X to 1st column
if (row == 0 || row == 5 || row == 10)
{
y = 100;
}
//else if (row == 5)
//{
// y = 290; //going to new sector of Y
//}
//else if (row == 10)
//{
// y = 479; //going to new sector of Y
//}
else
{
y = y + 37; // next seat for Y
}
}
else if (column % 5 == 0)
{
x = x + 70; //going to new sector of X
}
else
{
x = x + 37; // next seat for X
}
return new int[] { x, y };
}
private void buttonSeats_Click(object sender, EventArgs e)
{
Seat seat = sender as Seat;
clickedButton = seat.Number;
if (seat == null)
return;
if (seat.Occupied)
{
MessageBox.Show("Seat Number " + seat.Text + " has already been taken!",
"Reservation Attention",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
else
{
if (DialogResult.Yes ==
MessageBox.Show("Seat number " + seat.Text + " is free.\nDo you want to reserve it?",
"New reservation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question))
{
//seat is being reserved...
seat.Occupied = true;
seat.BackColor = System.Drawing.Color.Red;
seat.FlatAppearance.BorderColor = System.Drawing.Color.Red;
seat.FlatAppearance.BorderSize = 0;
seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
//MessageBox.Show("Reservation confirmed.\nSeat number " + seat.Text + " is now reserved.", "Reservation confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
using (SqlConnection cn = new SqlConnection(connectionString))
{
try
{
cn.Open();
string UpdateString = @"update SampleTable1 set IsBooked = 1 where SeatNo='" + clickedButton + "'";
SqlCommand cmd = new SqlCommand(UpdateString, cn);
cmd.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
}
}
}
public class Seat : Button
{
private readonly DataRow _row;
public Seat(DataRow row)
{
if (row == null)
throw new ArgumentNullException("row");
_row = row;
}
public int ID
{
get
{
return int.Parse(_row["SeatId"].ToString());
}
}
public string Number
{
get
{
return _row["SeatNo"] as string;
}
set
{
if (value.Length <= 3)
{
_row["SeatNo"] = value;
}
}
}
public bool Occupied
{
get
{
return int.Parse(_row["IsBooked"].ToString()) == 1;
}
set
{
_row["IsBooked"] = value ? 1 : 0;
}
}
}
private DataTable GetDataFromDataBase()
{
DataSet ds = new DataSet();
DataTable table;
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = new SqlCommand { CommandText = @"select * from SampleTable1", Connection = cn };
da.Fill(ds, "SampleTable1");
table = ds.Tables["SampleTable1"];
}
}
return table;
}
private void SetOccupiedSeats(DataTable table)
{
foreach (DataRow row in table.Rows)
{
list.Add(new Seat(row));
}
}
}
}