Reputation: 335
I have an array of Buttons.
When I try to change their back-Color it is not show at the web.
Why?
Button[] arr = new Button[2];
arr[0] = btn1;
arr[1] = btn2;
for (int i = 0 ; i < arr.length ; i++)
{
arr[i].backColor = System.Drawing.Color.Red;
}
but this is working good:
btn1.backColor = System.Drawing.Color.Red;
Added from his answer:
I am sorry, I wrote it from my head.
This is the problem code:
static Button[] arr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
arr = new Button[2];
arr[0] = Button1;
arr[1] = Button3;
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Blue;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Red;
}
}
When I press Button1 it is work, but when I press Button2 it does not work..
Why?
Thanks
Upvotes: 0
Views: 175
Reputation: 2389
Try this.
First of all decide when you want to change the backcolor of the button. On any button click event or page load, where ever you want to do just call below function. Your code is true. but just call set the button at second button click or always you have to click first button first.
public void ChangeColor()
{
Button[] arr = new Button[2];
arr[0] = btn1;
arr[1] = btn2;
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Red;
}
}
Update Answer as par your question.
Button[] arr;
protected void btnColor_Click(object sender, EventArgs e)
{
ChangeColorRed();
}
protected void btnColor2_Click(object sender, EventArgs e)
{
ChangeColorGreen();
}
public void ChangeColorRed()
{
SetButton();
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Red;
}
}
public void SetButton()
{
arr = new Button[2];
arr[0] = btn1;
arr[1] = btn2;
}
public void ChangeColorGreen()
{
SetButton();
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Green;
}
}
Regards AB Vyas
Upvotes: 1
Reputation: 12616
EDIT:
When you press Button2 before pressing Button1, the array of buttons is not created yet and therefore it can't set the color. Instantiate the array, for instace, in Page_Load()
;
Also, I would suggest not to use arrays unless you are really sure you need them. Try this.
var btns = new List<Button> { btn1, btn2 };
If you want to add a new button, just call
btns.Add(btn3);
Coloring then looks, for instance, simple like this
foreach (var btn in btns)
btn.BackColor = Color.Red;
Upvotes: 1
Reputation: 335
I am sorry, I wrote it from my head.
This is the problem code:
static Button[] arr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
arr = new Button[2];
arr[0] = Button1;
arr[1] = Button3;
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Blue;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i].BackColor = System.Drawing.Color.Red;
}
}
When I press Button1 it is work, but when I press Button2 it does not work..
Why?
Thanks
Upvotes: 0
Reputation: 1869
May be because you're showing btn1 and btn2?
Seems like you're creating copies of buttons.
How about this?
Button[] arr = new Button[2];
arr[0] = btn1;
arr[1] = btn2;
for (int i = 0 ; i < arr.length ; i++)
{
arr[i].backColor = System.Drawing.Color.Red;
arr[i].Show(); //add Show
}
Upvotes: 0