简体   繁体   English

更改不使用click at事件的动态创建按钮的颜色

[英]Changing the color of dynamically created buttons not using clicked at event

I have added buttons to a grid layout I created. 我在创建的网格布局中添加了按钮。 Here is the code for that. 这是代码。

        int nodeIndex = 0;

        for (i = 0; i < usedRows; i++)
        {
            for (j = 0; j < cols; j++)
            {
                this.tableLayoutPanel1.Controls.Add(nodes[nodeIndex++], j, i);
            }
        }

Later on in the application I want to be able to change the color of a button at a specified position. 稍后在应用程序中,我希望能够在指定位置更改按钮的颜色。 Basically change the back round color of the button at position i,j. 基本上更改位置i,j处按钮的后退颜色。 How would I get access to that specific button? 我将如何访问该特定按钮? I am using winforms. 我正在使用winforms。 Is there something like 是否有类似的东西

button = this.tableLayoutPanel1.Controls.GetChildAtPosition(j, i)

You can use something along these lines. 您可以按照以下方式使用某些东西。

button = this.tableLayoutPanel1.GetControlFromPosition(j, i);
button.BackColor = Color.BLACK; 

So first you want to able to find the control and conveniently there is a method called FindControl() that can do just that 因此,首先您要能够找到控件,并且方便地有一个名为FindControl()的方法可以做到这一点

MSDN link for reference: http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol(v=vs.110).aspx MSDN链接以供参考: http : //msdn.microsoft.com/zh-cn/library/system.web.ui.control.findcontrol(v=vs.110).aspx

Second you want to be able to change the color of the button once you find it. 其次,您希望能够在找到按钮后更改其颜色。

For buttons you probably want to use the BackColor property. 对于按钮,您可能想要使用BackColor属性。

Again MSDN link for reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.backcolor(v=vs.110).aspx 再次提供MSDN链接以供参考: http : //msdn.microsoft.com/zh-cn/library/system.windows.forms.control.backcolor(v=vs.110).aspx

The trick is going to be finding the control and then working with that control as an object to change the color. 诀窍是先找到控件,然后将该控件作为对象来更改颜色。 Don't forget that you can cast the control to a button type once you find it, which should give you access to the BackColor property. 不要忘记,一旦找到控件就可以将其转换为按钮类型,这应该使您可以访问BackColor属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM