Dyanamic按钮颜色变化不起作用。 [英] Dyanamic button color change is not working.

查看:105
本文介绍了Dyanamic按钮颜色变化不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数据库状态包含yes,我尝试更改特定按钮颜色。但是它改变了所有按钮的颜色。



我尝试过:



i have try to change the specific button color,if the the database status contain yes. but it change all button color.

What I have tried:

Button newPanelcategory = new Button();

                newPanelcategory.Name = dt.Rows[i]["table_id"].ToString();
                newPanelcategory.Text = dt.Rows[i]["table_id"].ToString();
                newPanelcategory.Location = System.Drawing.Point.Add(new Point(0, 4 + i * 55), new Size(50, 50));
                newPanelcategory.Height = 50;
                newPanelcategory.Width = 75;
                newPanelcategory.FlatStyle = FlatStyle.Flat;
                newPanelcategory.Font = new Font("Times New Roman", 10, FontStyle.Bold);
                this.Controls.Add(newPanelcategory);
                flowLayoutPanel1.Controls.Add(newPanelcategory);
               newPanelcategory.BackColor = Color.Red;
                newPanelcategory.ForeColor = Color.White;
                newPanelcategory.Click += new EventHandler(newPanelcategory_Click);
                flowLayoutPanel1.AutoScroll = true;
                 DataTable dtms = blod.getallorder();
                for (int ia = 0; ia < dtms.Rows.Count; ia++)
                {
                    if ((dtms.Rows[ia]["active"]).ToString() == "yes")
                    {

                        newPanelcategory.BackColor =Color.Blue;
                
                    }
                    else 
                    {
                        newPanelcategory.BackColor = Color.Red;
                    
                    }
                }

            }

推荐答案

我假设你的意图是在表格中每行一个按钮。



如果这是正确的,你的for循环应该包含创建按钮和设置颜色的整个逻辑。



使用当前代码,您的按钮颜色将始终由表格的最后一行确定。
I am assuming your intent is to have one button per row in the table.

If that is correct, your for loop should contain the entire logic of creating button and setting the color.

With current code, your button color will always be determined by last row of the table.


这篇关于Dyanamic按钮颜色变化不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆