I have added a panel control at the design time and I want to add some buttons in the panel at the runtime, the code for adding the buttons in working perfectly, but the problem is with the Panel control, when the Buttons are added at the run time, the Panel hides the buttons behind itself, can anyone suggest the solution, I am pasting the code from designer as well the code that I have written. Please correct my mistake.

Code from Form3.Designer.cs:

namespace rough2

{

partial class Form3

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.btnPlatinum = new System.Windows.Forms.Button();

this.btnDiamond = new System.Windows.Forms.Button();

this.btnGold = new System.Windows.Forms.Button();

this.btnReset = new System.Windows.Forms.Button();

this.PlatinumPanel = new System.Windows.Forms.Panel();

this.SuspendLayout();

//

// btnPlatinum

//

this.btnPlatinum.Location = new System.Drawing.Point(29, 39);

this.btnPlatinum.Name = "btnPlatinum";

this.btnPlatinum.Size = new System.Drawing.Size(75, 23);

this.btnPlatinum.TabIndex = 0;

this.btnPlatinum.Text = "Platinum";

this.btnPlatinum.UseVisualStyleBackColor = true;

this.btnPlatinum.Click += new System.EventHandler(this.btnPlatinum_Click);

//

// btnDiamond

//

this.btnDiamond.Location = new System.Drawing.Point(147, 39);

this.btnDiamond.Name = "btnDiamond";

this.btnDiamond.Size = new System.Drawing.Size(75, 23);

this.btnDiamond.TabIndex = 1;

this.btnDiamond.Text = "Diamond";

this.btnDiamond.UseVisualStyleBackColor = true;

//

// btnGold

//

this.btnGold.Location = new System.Drawing.Point(267, 39);

this.btnGold.Name = "btnGold";

this.btnGold.Size = new System.Drawing.Size(75, 23);

this.btnGold.TabIndex = 2;

this.btnGold.Text = "Gold";

this.btnGold.UseVisualStyleBackColor = true;

//

// btnReset

//

this.btnReset.Location = new System.Drawing.Point(728, 39);

this.btnReset.Name = "btnReset";

this.btnReset.Size = new System.Drawing.Size(75, 23);

this.btnReset.TabIndex = 3;

this.btnReset.Text = "Reset";

this.btnReset.UseVisualStyleBackColor = true;

//

// PlatinumPanel

//

this.PlatinumPanel.Location = new System.Drawing.Point(56, 129);

this.PlatinumPanel.Name = "PlatinumPanel";

this.PlatinumPanel.Size = new System.Drawing.Size(400, 143);

this.PlatinumPanel.TabIndex = 4;

//

// Form3

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(1175, 588);

this.Controls.Add(this.btnReset);

this.Controls.Add(this.btnGold);

this.Controls.Add(this.btnDiamond);

this.Controls.Add(this.btnPlatinum);

this.Controls.Add(this.PlatinumPanel);

this.Name = "Form3";

this.Text = "Form3";

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button btnPlatinum;

private System.Windows.Forms.Button btnDiamond;

private System.Windows.Forms.Button btnGold;

private System.Windows.Forms.Button btnReset;

private System.Windows.Forms.Panel PlatinumPanel;

}

}

The code in the Form3.cs file that I have written:

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;
            PlatinumPanel.BringToFront();
            PlatinumPanel.Controls.Add(addPlatinumSeats());
            
        }

        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));
            }
        }

        private Button addPlatinumSeats()
        {
            DataTable table = GetDataFromDataBase();
            SetOccupiedSeats(table);
            Button newbtn = new Button();

            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;
        }
    }
}

I can't go through all your code but here are a few things you may wanna know.
>> if two controls are placed on the form in the same location which I assume it is in your case, the container control will be preferred. your best chance in this instance is to place those buttons inside the panel itself not on the same location or underneath.

I can't go through all your code but here are a few things you may wanna know.
>> if two controls are placed on the form in the same location which I assume it is in your case, the container control will be preferred. your best chance in this instance is to place those buttons inside the panel itself not on the same location or underneath.

I have solved this problem, but I am facing one problem, I have posted the new thread for that, you can have a look at that here http://www.daniweb.com/software-development/csharp/threads/378110 Please help me on that problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.