改变编程方式创建按钮的特性 [英] Change properties of programmatically created buttons
问题描述
我创建在我的应用程序按钮:
I create buttons in my application by:
List<Button> btnslist = new List<Button>();
for (int i = 0; i < nbrofbtns; i++)
{
Button newButton = new Button();
btnslist.Add(newButton);
this.Controls.Add(newButton);
newButton.Width = btnsidelength;
newButton.Height = btnsidelength;
newButton.Top = btnsidelength
* Convert.ToInt32(Math.Floor(Convert.ToDouble(i / Form2.puzzlesize)));
newButton.Left = btnsidelength
* Convert.ToInt32(
Math.Floor(Convert.ToDouble(i))
- Math.Floor((Convert.ToDouble(i))
/ (Form2.puzzlesize)) * (Form2.puzzlesize));
newButton.BackgroundImage = Lights_out_.Properties.Resources.LightsOutBlack;
newButton.Tag = (i+1).ToString();
newButton.Click += new EventHandler(Any_Button_Click);
然后,我有被点击任何按钮时的方法。
Then I have a method for when any of the buttons are clicked.
void Any_Button_Click(object sender, EventArgs e)
{
//the variable b has all the insformation that the single button had itself.
Button b = (Button)sender;
if (b.BackgroundImage == Lights_out_.Properties.Resources.LightsOutBlack)
{
MessageBox.Show(b.Tag.ToString());
MessageBox.Show(btnslist[Convert.ToInt32(b.Tag)].BackgroundImage.ToString());
btnslist[Convert.ToInt32(b.Tag)].BackgroundImage =
Lights_out_.Properties.Resources.LightsOutWhite;
MessageBox.Show(btnslist[Convert.ToInt32(b.Tag)].BackgroundImage.ToString());
}
else
{
MessageBox.Show("b.backgroundimage != lightsoutblack. Backgroundimage = "
+ b.BackgroundImage.ToString());
}
}
我要如何改变实际的按钮中的数据(当时所说的按钮被点击)?我想特异性地改变和backgroundImage。我怎么能这样做? (我还需要改变由code创造了一些其他按钮和backgroundImage。)
How do I change the data in the actual button (then said button is clicked)? I want specificly to change the backgroundimage. How could I do this?? (I also need to change the backgroundimage of some other buttons created by the code.)
推荐答案
sender对象的是的按钮:
The sender object is the button:
Button b = (Button)sender;
...所以你应该能够在其上直接更改属性:
... so you should be able to change properties on it directly:
b.WhateverPropsToChange = yourSetting;
PS:我不认为这是必要的,但如果按钮没有直接更新,你可以尝试使用 b.Refresh()
来让它知道事情已经改变了。
PS: I don't think this is necessary, but if the button is not updated directly, you might try to using b.Refresh()
to let it know something has changed.
这篇关于改变编程方式创建按钮的特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!