Luci C
Luci C

Reputation: 199

Function for click events

I want to implement Nine Men's Morris Game. I have a board with 24 pictureboxes and on the left and right side, 9 red pictureboxes and 9 green pictureboxes.
I want to add them in a list:

 List<PictureBox> ls = new List<PictureBox>();
 private void Form1_Load(object sender, EventArgs e)
    {


        for (int i = 1; i <= 24; i++)
        {
            PictureBox p = new PictureBox();
            p.Name = "pictureBox" + i;
            ls.Add(p);
        }

    }

is it ok?

and is it possible to do something like this: I want to click on one of the 24 pictureboxes, and make the background of that picturebox to become one time green and one time red?
I mean recursive function or something like that that can recognize when i click on a picturebox, search in the list for that picturebox and changes his backcolor?

enter image description here

Upvotes: 0

Views: 490

Answers (3)

Mike Lippert
Mike Lippert

Reputation: 2261

I assume the list of 24 PictureBoxes is supposed to represent the points on a nine man morris board where the player's men can be positioned.

I4V is right that all you need to do is add a click handler to each picture box. If you want to have the background alternate between green and red, keep your original list, but add the click handler in it

for (int i = 1; i <= 24; i++)
{
    PictureBox p = new PictureBox();
    p.Name = "pictureBox" + i;
    p.Click += p_Click; // <----------
    ls.Add(p);
}

And modify i4v's click handler to use the current background color to determine the new background color.

void p_Click(object sender, EventArgs e)
{
    PictureBox p = (PictureBox)sender);
    p.BackColor = p.BackColor == Color.Green ? Color.Red : Color.Green;
}

A couple of other points.

  • You don't set an initial background color, so it will be the default color until clicked on, when it will be set to Green (as Green isn't the default background color).
  • Why name your pictureboxes w/ their List index + 1? Why not just use the List index and the natural C# iteration from 0: for (int i = 0; i < 24; i++)?

Upvotes: 2

I4V
I4V

Reputation: 35363

You don't need any pictureBox list here.

for (int i = 1; i <= 24; i++)
{
     PictureBox p = new PictureBox();
     p.Click += p_Click;
     //of course, somecontrol.Controls.Add(p);
     //for ex: this.Controls.Add(p);
}

-

void p_Click(object sender, EventArgs e)
{
    ((PictureBox)sender).BackColor = Color.Green;
}

EDIT

It seems you are trying to add an event handler to all pictureBoxes

**parentControl**.Controls.OfType<PictureBox>()
                 .ToList().ForEach(p => p.Click+=p_Click);

Upvotes: 4

Serdalis
Serdalis

Reputation: 10489

The method you have outlined would not work, however there is another way to do the same thing using the sender object passed into the event handler:

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 1; i <= 24; i++)
    {
        PictureBox p = new PictureBox();
        p.Name = "pictureBox" + i;
        p.Click += PictureBox_Click;
    }
}

void PictureBox_Click(object sender, EventArgs e)
{
    PictureBox event_picturebox = (PictureBox)sender;
    event_picturebox.BackColor = Color.White;
}

You just have to map every picture box you want to run this event to the same event, the event will then be able to perform actions on this pictureBox because a reference to it was passed in.

If you already have the picture boxes defined in the form, you just need to do something like:

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Click += PictureBox_Click;
    pictureBox2.Click += PictureBox_Click;
    // and keep going

    // OR
    // this is a bit dangerous if you don't want ALL 
    // your picture boxes to have this event
    // also assumes that you know picturebox1 exists.
    foreach (object f in this.Controls)
    {
        if (f.GetType().Equals(pictureBox1.GetType()))
        {
            ((PictureBox)f).Click += button_Click;
        }
    }
}

Upvotes: 0

Related Questions