控件未在动态生成的面板中显示
我想要动态生成相同数量的面板,因为sqlcommand会读取不同值的数量,然后根据另一个sqlcommand读取的值在面板中动态显示控件,请参阅代码。
但是控件即按钮仅在一个面板中显示,而不是在所有相应的面板中显示。
生成结果的屏幕截图:
为什么即使控件绘制在正确的位置,代码也不会在所有面板中显示控件?
private void show_button()
{
//InitializeComponent();
con1.con.Open();
SqlCommand cmd = new SqlCommand("select distinct floors from tbl_TableMaster", con1.con);
SqlDataReader rd = cmd.ExecuteReader();
int panelLocationX = 10;
int panelLocationY = 30;
int panelWidth = 340;
int buttonWidth = 100;
int buttonHeight = 40;
int panelHeight = 300;
while (rd.Read())
{
Panel panel = new Panel();
panel.Location = new Point(panelLocationX, panelLocationY);
panel.Size = new Size(panelWidth, panelHeight);
panel.Name = rd[0].ToString();
panel.BackColor = Color.Tan;
Label label = new Label();
label.Location = new Point(panelLocationX, panelLocationY - 20);
label.ForeColor = Color.Red;
label.Text = rd[0].ToString();
this.Controls.Add(label);
int ButtonLocationX = panelLocationX;
int ButtonLocationY = panelLocationY ;
con2.con.Open();
SqlCommand cmd1 = new SqlCommand("select TableNo,IsActive,Date from tbl_TableMaster where floors=@floor", con2.con);
cmd1.Parameters.AddWithValue("@floor", rd[0].ToString());
SqlDataReader rd1 = cmd1.ExecuteReader();
int j = 0;
while (rd1.Read())
{
if (ButtonLocationX < (panelLocationX + panelWidth-10))
{
//MessageBox.Show("Button" + ButtonLocationX + "," + ButtonLocationY + "");
if (rd1[1].ToString() == "Available")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.Turquoise;
button.Tag = j;
button.Size = new System.Drawing.Size(buttonWidth,buttonHeight);
button.Font = new Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Order Running")
{
listBox1.Visible = false;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
System.TimeSpan diff2 = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss")) - Convert.ToDateTime(rd1[2].ToString());
button.Text = Convert.ToString(rd1[0].ToString() + "nn . " + Convert.ToString(diff2));
button.Text = rd1[1].ToString();
button.BackColor = Color.Crimson;
button.Tag = j;
button.Size = new System.Drawing.Size(buttonWidth,buttonHeight);
button.ForeColor = Color.Black;
button.Font = new Font("Microsoft Sans Serif", 7, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Bitmap bmp = new Bitmap(button.ClientRectangle.Width, button.ClientRectangle.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.Clear(button.BackColor);
string line1 = rd1[0].ToString();
string line2 = "nn ." + diff2.ToString();
StringFormat SF = new StringFormat();
SF.Alignment = StringAlignment.Center;
SF.LineAlignment = StringAlignment.Near;
using (Font arial = new Font("Microsoft Sans Serif", 15))
{
Rectangle RC = button.ClientRectangle;
RC.Inflate(-5, -5);
G.DrawString(line1, arial, Brushes.White, RC, SF);
}
using (Font courier = new Font("Microsoft Sans Serif", 10))
{
SF.LineAlignment = StringAlignment.Center;
G.DrawString(line2, courier, Brushes.Black, button.ClientRectangle, SF);
}
}
button.Image = bmp;
button.ImageAlign = ContentAlignment.MiddleCenter;
button.ForeColor = Color.Crimson;
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Hold")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.Yellow;
button.Tag = j;
button.Size = new System.Drawing.Size(ButtonLocationX, ButtonLocationY);
button.Font = new Font("Microsoft Sans Serif", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Amount")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.DarkGray;
button.Tag = j;
button.Size = new System.Drawing.Size(ButtonLocationX, ButtonLocationY);
button.Font = new Font("Microsoft Sans Serif", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
ButtonLocationX = ButtonLocationX + buttonWidth + 10;
count = count + 1;
j++;
}
else
{
ButtonLocationY = ButtonLocationY + buttonHeight+10;
ButtonLocationX = panelLocationX;
if (panelHeight < ButtonLocationY)
{
panel.AutoScroll = false;
panel.HorizontalScroll.Enabled = false;
panel.HorizontalScroll.Visible = false;
panel.HorizontalScroll.Maximum = 0;
panel.AutoScroll = true;
}
else
{
}
if (rd1[1].ToString() == "Available")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.Turquoise;
button.Tag = j;
button.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
button.Font = new Font("Microsoft Sans Serif", 12, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Order Running")
{
listBox1.Visible = false;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
System.TimeSpan diff2 = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss")) - Convert.ToDateTime(rd1[2].ToString());
button.Text = Convert.ToString(rd1[0].ToString() + "nn . " + Convert.ToString(diff2));
button.Text = rd1[1].ToString();
button.BackColor = Color.Crimson;
button.Tag = j;
button.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
button.ForeColor = Color.Black;
button.Font = new Font("Microsoft Sans Serif", 7, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Bitmap bmp = new Bitmap(button.ClientRectangle.Width, button.ClientRectangle.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.Clear(button.BackColor);
string line1 = rd1[0].ToString();
string line2 = "nn ." + diff2.ToString();
StringFormat SF = new StringFormat();
SF.Alignment = StringAlignment.Center;
SF.LineAlignment = StringAlignment.Near;
using (Font arial = new Font("Microsoft Sans Serif", 15))
{
Rectangle RC = button.ClientRectangle;
RC.Inflate(-5, -5);
G.DrawString(line1, arial, Brushes.White, RC, SF);
}
using (Font courier = new Font("Microsoft Sans Serif", 10))
{
SF.LineAlignment = StringAlignment.Center;
G.DrawString(line2, courier, Brushes.Black, button.ClientRectangle, SF);
}
}
button.Image = bmp;
button.ImageAlign = ContentAlignment.MiddleCenter;
button.ForeColor = Color.Crimson;
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Hold")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.Yellow;
button.Tag = j;
button.Size = new System.Drawing.Size(ButtonLocationX, ButtonLocationY);
button.Font = new Font("Microsoft Sans Serif", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
if (rd1[1].ToString() == "Amount")
{
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Location = new Point(ButtonLocationX, ButtonLocationY);
button.Text = rd1[0].ToString();
button.BackColor = Color.DarkGray;
button.Tag = j;
button.Size = new System.Drawing.Size(ButtonLocationX, ButtonLocationY);
button.Font = new Font("Microsoft Sans Serif", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel.Controls.Add(button);
//button.Visible = true;
//button.BringToFront();
button.FlatStyle = FlatStyle.Popup;
}
j++;
count = count + 1;
//if (panelHeight < 500)
//{
//panelHeight = panelHeight + 60;
////}
////else
////{
//// bar.Visible = true;
//// panel.Controls.Add(bar);
////}
//panel.Size = new Size(panelWidth, panelHeight);
//ButtonLocationY = ButtonLocationY + buttonHeight + 10;
//ButtonLocationX = panelLocationX + 10;
//Button button = new Button();
//button.Location = new Point(ButtonLocationX, ButtonLocationY);
//this.Controls.Add(button);
//button.BringToFront();
////MessageBox.Show("Button" + ButtonLocationX + "," + ButtonLocationY + "");
//button.Size = new Size(buttonWidth, buttonHeight);
//button.Name = rd1[0].ToString();
//button.Text = rd1[0].ToString();
//button.BackColor = Color.Brown;
ButtonLocationX = ButtonLocationX + buttonWidth + 10;
}
}
this.Controls.Add(panel);
con2.con.Close();
panelLocationX = panelLocationX + panelWidth + 50;
}
con1.con.Close();
}
链接地址: http://www.djcxy.com/p/61577.html
上一篇: Controls not shown in dynamically generated panel
下一篇: How to hide dynamically created controls in scrollable panel