Hi,i want to use pictureboxes in different eventhandler. Here are my codes:
private void buttonclick(object sender,EventArgs e) {
int i;
Form newform=new Form(); //creating form2
for (i=0;i<7;i++) { // loop that creates 6 picturebox
Picturebox newpicturebox=new picturebox(); //creating picturebox
newpicturebox.Name="picturebox"+i; //defining name
form2.controls.add(newpicturebox); //adding controls in form2
}
Timer newtimer=new Timer(); //creating timer to conrol pictureboxes that i defined in for loop
newtimer.interval=1000; //setting timer interval
newtimer.Tick+=new EventHandler(newtimer_Tick);//seting Timer's Eventhander
I want to control pictureboxes that i defined in for loop via newtimer_tick. How can i do this?
Loading
AlperPosted Apr 24, 2007, 10:26 AM
Scott LyslePosted Apr 21, 2007, 2:30 PM
Compare this to your example and give this a shot in a form with a single button and a timer set to 250.
partial class Form1 : Formpublic
{
public PictureBox[] pb = new PictureBox[6];
public int seqNumber = 0; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form newform=new Form();
for (int i = 0; i < pb.Length; i++)
{
pb[i] = new PictureBox();
pb[i].Name = "picturebox" + i;
pb[i].Height = 20;
pb[i].Width = 20;
pb[i].Top = i * 25 + 10;
pb[i].Left = 10;
pb[i].BackColor = Color.Blue;
newform.Controls.Add(pb[i]);
}
newform.Show();
timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
BlueAll();
Color.Yellow; if (seqNumber < 5)pb[seqNumber].BackColor =
{
seqNumber += 1;
}
else
{
seqNumber = 0;
}
}
private void BlueAll()
{
for (int i = 0; i < pb.Length; i++)
{
pb[i].BackColor = Color.Blue;
}
}
}